summaryrefslogtreecommitdiffstats
path: root/libdivecomputer.c
diff options
context:
space:
mode:
authorGravatar Linus Torvalds <torvalds@linux-foundation.org>2014-02-12 14:19:53 -0800
committerGravatar Dirk Hohndel <dirk@hohndel.org>2014-02-12 17:41:49 -0800
commit23baf20f569f1f34ab4efa68996f24c3ad49fe05 (patch)
treedc10775b82b6f0e69d36bafeec54430bdec8126b /libdivecomputer.c
parent7ae05b4f710fc0d03a2198397e7dd779f477fd79 (diff)
downloadsubsurface-23baf20f569f1f34ab4efa68996f24c3ad49fe05.tar.gz
Use "rint()" instead of rounding manually with "+ 0.5"
rint() is "round to nearest integer", and does a better job than +0.5 (followed by the implicit truncation inherent in integer casting). We already used 'rint()' for values that could be negative (where +0.5 is actively wrong), let's just make it consistent. Of course, as is usual for the messy C math functions, it depends on the current rounding mode. But the default round-to-nearest is what we want and use, and the functions that explicitly always round to nearest aren't standard enough to worry about. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'libdivecomputer.c')
-rw-r--r--libdivecomputer.c30
1 files changed, 15 insertions, 15 deletions
diff --git a/libdivecomputer.c b/libdivecomputer.c
index f93b2c0fc..7cc3894f9 100644
--- a/libdivecomputer.c
+++ b/libdivecomputer.c
@@ -77,13 +77,13 @@ static bool get_tanksize(device_data_t *devdata, const unsigned char *data, cyli
case COBALT_CFATPSI:
airvolume = cuft_to_l(atomics_gas_info[idx].tanksize) * 1000.0;
mbar = psi_to_mbar(atomics_gas_info[idx].workingpressure);
- cyl[idx].type.size.mliter = airvolume / bar_to_atm(mbar / 1000.0) + 0.5;
+ cyl[idx].type.size.mliter = rint(airvolume / bar_to_atm(mbar / 1000.0));
cyl[idx].type.workingpressure.mbar = mbar;
break;
case COBALT_CFATBAR:
airvolume = cuft_to_l(atomics_gas_info[idx].tanksize) * 1000.0;
mbar = atomics_gas_info[idx].workingpressure * 1000;
- cyl[idx].type.size.mliter = airvolume / bar_to_atm(mbar / 1000.0) + 0.5;
+ cyl[idx].type.size.mliter = rint(airvolume / bar_to_atm(mbar / 1000.0));
cyl[idx].type.workingpressure.mbar = mbar;
break;
case COBALT_WETINDL:
@@ -112,8 +112,8 @@ static int parse_gasmixes(device_data_t *devdata, struct dive *dive, dc_parser_t
if (i >= MAX_CYLINDERS)
continue;
- o2 = gasmix.oxygen * 1000 + 0.5;
- he = gasmix.helium * 1000 + 0.5;
+ o2 = rint(gasmix.oxygen * 1000);
+ he = rint(gasmix.helium * 1000);
/* Ignore bogus data - libdivecomputer does some crazy stuff */
if (o2 + he <= O2_IN_AIR || o2 >= 1000)
@@ -204,11 +204,11 @@ sample_cb(dc_sample_type_t type, dc_sample_value_t value, void *userdata)
finish_sample(dc);
break;
case DC_SAMPLE_DEPTH:
- sample->depth.mm = value.depth * 1000 + 0.5;
+ sample->depth.mm = rint(value.depth * 1000);
break;
case DC_SAMPLE_PRESSURE:
sample->sensor = value.pressure.tank;
- sample->cylinderpressure.mbar = value.pressure.value * 1000 + 0.5;
+ sample->cylinderpressure.mbar = rint(value.pressure.value * 1000);
break;
case DC_SAMPLE_TEMPERATURE:
sample->temperature.mkelvin = C_to_mkelvin(value.temperature);
@@ -235,28 +235,28 @@ sample_cb(dc_sample_type_t type, dc_sample_value_t value, void *userdata)
#if DC_VERSION_CHECK(0, 3, 0)
case DC_SAMPLE_SETPOINT:
/* for us a setpoint means constant pO2 from here */
- sample->po2 = po2 = value.setpoint * 1000 + 0.5;
+ sample->po2 = po2 = rint(value.setpoint * 1000);
break;
case DC_SAMPLE_PPO2:
- sample->po2 = po2 = value.ppo2 * 1000 + 0.5;
+ sample->po2 = po2 = rint(value.ppo2 * 1000);
break;
case DC_SAMPLE_CNS:
- sample->cns = cns = value.cns * 100 + 0.5;
+ sample->cns = cns = rint(value.cns * 100);
break;
case DC_SAMPLE_DECO:
if (value.deco.type == DC_DECO_NDL) {
sample->ndl.seconds = ndl = value.deco.time;
- sample->stopdepth.mm = stopdepth = value.deco.depth * 1000.0 + 0.5;
+ sample->stopdepth.mm = stopdepth = rint(value.deco.depth * 1000.0);
sample->in_deco = in_deco = false;
} else if (value.deco.type == DC_DECO_DECOSTOP ||
value.deco.type == DC_DECO_DEEPSTOP) {
sample->in_deco = in_deco = true;
- sample->stopdepth.mm = stopdepth = value.deco.depth * 1000.0 + 0.5;
+ sample->stopdepth.mm = stopdepth = rint(value.deco.depth * 1000.0);
sample->stoptime.seconds = stoptime = value.deco.time;
ndl = 0;
} else if (value.deco.type == DC_DECO_SAFETYSTOP) {
sample->in_deco = in_deco = false;
- sample->stopdepth.mm = stopdepth = value.deco.depth * 1000.0 + 0.5;
+ sample->stopdepth.mm = stopdepth = rint(value.deco.depth * 1000.0);
sample->stoptime.seconds = stoptime = value.deco.time;
}
#endif
@@ -451,7 +451,7 @@ static int dive_cb(const unsigned char *data, unsigned int size,
dc_parser_destroy(parser);
return false;
}
- dive->dc.maxdepth.mm = maxdepth * 1000 + 0.5;
+ dive->dc.maxdepth.mm = rint(maxdepth * 1000);
// Parse the gas mixes.
unsigned int ngases = 0;
@@ -474,7 +474,7 @@ static int dive_cb(const unsigned char *data, unsigned int size,
dc_parser_destroy(parser);
return false;
}
- dive->dc.salinity = salinity.density * 10.0 + 0.5;
+ dive->dc.salinity = rint(salinity.density * 10.0);
double surface_pressure = 0;
rc = dc_parser_get_field(parser, DC_FIELD_ATMOSPHERIC, 0, &surface_pressure);
@@ -483,7 +483,7 @@ static int dive_cb(const unsigned char *data, unsigned int size,
dc_parser_destroy(parser);
return false;
}
- dive->dc.surface_pressure.mbar = surface_pressure * 1000.0 + 0.5;
+ dive->dc.surface_pressure.mbar = rint(surface_pressure * 1000.0);
#endif
rc = parse_gasmixes(devdata, dive, parser, ngases, data);