Several float to int potential rounding errors/inconsistencies

Linus Torvalds torvalds at linux-foundation.org
Mon Mar 6 20:50:10 PST 2017


On Mon, Mar 6, 2017 at 8:27 PM, Linus Torvalds
<torvalds at linux-foundation.org> wrote:
>
> Hmm. I guess there's "-Wfloat-conversion". It might cause excessive
> warnings. I'll try.

Ugh, yes. Mainly because it warns even about the "cast rint() to an
integer" case:

core/units.h: In function ‘feet_to_mm’:
core/units.h:162:9: warning: conversion to ‘long unsigned int’ from
‘double’ may alter its value [-Wfloat-conversion]
  return rint(feet * 304.8);
         ^~~~~~~~~~~~~~~~~~

so we'd have to create our own "round_float_to_long()" function that
uses rint() together with an explicit cast to shut things up.

Trying to visually skip the rint() cases, it does find some other
missing cases. Like this:

core/dive.h: In function ‘gas_mnd’:
core/dive.h:503:40: warning: conversion to ‘int’ from ‘double’ may
alter its value [-Wfloat-conversion]
  rounded_depth.mm = rint(mbar_to_depth(maxambient, dive) / roundto) * roundto;
                                        ^~~~~~~~~~

where we do have one 'rint()', but the above complaint is about
'maxambient' being truncated to mbar without rounding.

There are other cases too. Some of them appended.

This is *not* a full list.

                    Linus

---
core/file.c: In function ‘add_sample_data’:
core/file.c:296:22: warning: conversion to ‘int32_t {aka int}’ from
‘double’ may alter its value [-Wfloat-conversion]
   sample->depth.mm = val * 0.5 *1000;
                      ^~~
core/file.c:302:27: warning: conversion to ‘uint16_t {aka short
unsigned int}’ from ‘double’ may alter its value [-Wfloat-conversion]
   sample->setpoint.mbar = val * 10;
                           ^~~
core/file.c:305:30: warning: conversion to ‘uint16_t {aka short
unsigned int}’ from ‘double’ may alter its value [-Wfloat-conversion]
   sample->o2sensor[0].mbar = val * 10;
                              ^~~
core/file.c:308:30: warning: conversion to ‘uint16_t {aka short
unsigned int}’ from ‘double’ may alter its value [-Wfloat-conversion]
   sample->o2sensor[1].mbar = val * 10;
                              ^~~
core/file.c:311:35: warning: conversion to ‘int32_t {aka int}’ from
‘double’ may alter its value [-Wfloat-conversion]
   sample->cylinderpressure.mbar = val * 1000;
                                   ^~~
core/file.c:314:37: warning: conversion to ‘int32_t {aka int}’ from
‘double’ may alter its value [-Wfloat-conversion]
   sample->o2cylinderpressure.mbar = val * 1000;
                                     ^~~
core/file.c:317:25: warning: conversion to ‘uint32_t {aka unsigned
int}’ from ‘double’ may alter its value [-Wfloat-conversion]
   sample->ndl.seconds = val * 60;
                         ^~~
core/file.c:320:26: warning: conversion to ‘int32_t {aka int}’ from
‘double’ may alter its value [-Wfloat-conversion]
   sample->stopdepth.mm = val * 1000;
                          ^~~
core/parse-xml.c: In function ‘divinglog_dive’:
core/parse-xml.c:3297:30: warning: conversion to ‘int32_t {aka int}’
from ‘double’ may alter its value [-Wfloat-conversion]
   cur_dive->dc.maxdepth.mm = atof(data[5]) * 1000;
                              ^~~~
core/gaspressures.c: In function ‘fill_missing_segment_pressures’:
core/gaspressures.c:140:18: warning: conversion to ‘int’ from ‘double’
may alter its value [-Wfloat-conversion]
      pressure -= (start - end) * (double)pt / pt_sum;
                  ^
core/save-html.c: In function ‘put_cylinder_HTML’:
core/save-html.c:130:15: warning: conversion to ‘int’ from ‘double’
may alter its value [-Wfloat-conversion]
     volume *= bar_to_atm(cylinder->type.workingpressure.mbar / 1000.0);
               ^~~~~~~~~~
core/statistics.c: In function ‘process_dive’:
core/statistics.c:71:25: warning: conversion to ‘int32_t {aka int}’
from ‘double’ may alter its value [-Wfloat-conversion]
   stats->avg_depth.mm = (1.0 * old_tadt * stats->avg_depth.mm +
                         ^
core/statistics.c:77:27: warning: conversion to ‘int’ from ‘double’
may alter its value [-Wfloat-conversion]
   stats->avg_sac.mliter = (1.0 * stats->total_sac_time *
stats->avg_sac.mliter +


More information about the subsurface mailing list