summaryrefslogtreecommitdiffstats
path: root/core/libdivecomputer.c
diff options
context:
space:
mode:
authorGravatar Jeremie Guichard <djebrest@gmail.com>2017-03-09 23:07:30 +0700
committerGravatar Jeremie Guichard <djebrest@gmail.com>2017-03-09 23:07:30 +0700
commit2b06a0b2234cf2779f80e87038011067be282bcb (patch)
tree7532b11736a5eaedb3ceddf3e85ee423948d47ce /core/libdivecomputer.c
parent406e4287eb96e10ddfd22163f0e863e353470c68 (diff)
downloadsubsurface-2b06a0b2234cf2779f80e87038011067be282bcb.tar.gz
Fix potential double/float to int rounding errors
Not using lrint(f) when converting double/float to int creates rounding errors. This error was detected by TestParse::testParseDM4 failure on Windows. It was creating rounding inconsistencies on Linux too, see change in TestDiveDM4.xml. Enable -Wfloat-conversion for gcc version greater than 4.9.0 Signed-off-by: Jeremie Guichard <djebrest@gmail.com>
Diffstat (limited to 'core/libdivecomputer.c')
-rw-r--r--core/libdivecomputer.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/core/libdivecomputer.c b/core/libdivecomputer.c
index 2d8c79b4c..799846fc2 100644
--- a/core/libdivecomputer.c
+++ b/core/libdivecomputer.c
@@ -166,10 +166,11 @@ static int parse_gasmixes(device_data_t *devdata, struct dive *dive, dc_parser_t
* First, the pressures are off by a constant factor. WTF?
* Then we can round the wet sizes so we get to multiples of 10
* for cuft sizes (as that's all that you can enter) */
- dive->cylinder[i].type.workingpressure.mbar *= 206.843 / 206.7;
+ dive->cylinder[i].type.workingpressure.mbar = lrint(
+ dive->cylinder[i].type.workingpressure.mbar * 206.843 / 206.7 );
char name_buffer[9];
- int rounded_size = ml_to_cuft(gas_volume(&dive->cylinder[i],
- dive->cylinder[i].type.workingpressure));
+ int rounded_size = lrint(ml_to_cuft(gas_volume(&dive->cylinder[i],
+ dive->cylinder[i].type.workingpressure)));
rounded_size = (int)((rounded_size + 5) / 10) * 10;
switch (dive->cylinder[i].type.workingpressure.mbar) {
case 206843:
@@ -189,8 +190,8 @@ static int parse_gasmixes(device_data_t *devdata, struct dive *dive, dc_parser_t
break;
}
dive->cylinder[i].type.description = copy_string(name_buffer);
- dive->cylinder[i].type.size.mliter = cuft_to_l(rounded_size) * 1000 /
- mbar_to_atm(dive->cylinder[i].type.workingpressure.mbar);
+ dive->cylinder[i].type.size.mliter = lrint(cuft_to_l(rounded_size) * 1000 /
+ mbar_to_atm(dive->cylinder[i].type.workingpressure.mbar));
}
}
if (tank.gasmix != i) { // we don't handle this, yet
@@ -203,8 +204,8 @@ static int parse_gasmixes(device_data_t *devdata, struct dive *dive, dc_parser_t
// this new API also gives us the beginning and end pressure for the tank
if (!IS_FP_SAME(tank.beginpressure, 0.0) && !IS_FP_SAME(tank.endpressure, 0.0)) {
- dive->cylinder[i].start.mbar = tank.beginpressure * 1000;
- dive->cylinder[i].end.mbar = tank.endpressure * 1000;
+ dive->cylinder[i].start.mbar = lrint(tank.beginpressure * 1000);
+ dive->cylinder[i].end.mbar = lrint(tank.endpressure * 1000);
}
}
#endif