mirror of
https://github.com/subsurface/subsurface.git
synced 2024-11-28 05:00:20 +00:00
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>
This commit is contained in:
parent
7ae05b4f71
commit
23baf20f56
8 changed files with 38 additions and 38 deletions
2
dive.c
2
dive.c
|
@ -549,7 +549,7 @@ static void sanitize_cylinder_type(cylinder_type_t *type)
|
|||
volume_of_air = cuft_to_l(type->size.mliter);
|
||||
/* milliliters at 1 atm: "true size" */
|
||||
volume = volume_of_air / surface_volume_multiplier(type->workingpressure);
|
||||
type->size.mliter = volume + 0.5;
|
||||
type->size.mliter = rint(volume);
|
||||
}
|
||||
|
||||
/* Ok, we have both size and pressure: try to match a description */
|
||||
|
|
18
dive.h
18
dive.h
|
@ -155,7 +155,7 @@ static inline double grams_to_lbs(int grams)
|
|||
|
||||
static inline int lbs_to_grams(double lbs)
|
||||
{
|
||||
return lbs * 453.6 + 0.5;
|
||||
return rint(lbs * 453.6);
|
||||
}
|
||||
|
||||
static inline double ml_to_cuft(int ml)
|
||||
|
@ -175,12 +175,12 @@ static inline double mm_to_feet(int mm)
|
|||
|
||||
static inline unsigned long feet_to_mm(double feet)
|
||||
{
|
||||
return feet * 304.8 + 0.5;
|
||||
return rint(feet * 304.8);
|
||||
}
|
||||
|
||||
static inline int to_feet(depth_t depth)
|
||||
{
|
||||
return mm_to_feet(depth.mm) + 0.5;
|
||||
return rint(mm_to_feet(depth.mm));
|
||||
}
|
||||
|
||||
static inline double mkelvin_to_C(int mkelvin)
|
||||
|
@ -195,12 +195,12 @@ static inline double mkelvin_to_F(int mkelvin)
|
|||
|
||||
static inline unsigned long F_to_mkelvin(double f)
|
||||
{
|
||||
return (f-32) * 1000 / 1.8 + ZERO_C_IN_MKELVIN + 0.5;
|
||||
return rint((f-32) * 1000 / 1.8 + ZERO_C_IN_MKELVIN);
|
||||
}
|
||||
|
||||
static inline unsigned long C_to_mkelvin(double c)
|
||||
{
|
||||
return c * 1000 + ZERO_C_IN_MKELVIN + 0.5;
|
||||
return rint(c * 1000 + ZERO_C_IN_MKELVIN);
|
||||
}
|
||||
|
||||
static inline double psi_to_bar(double psi)
|
||||
|
@ -210,12 +210,12 @@ static inline double psi_to_bar(double psi)
|
|||
|
||||
static inline long psi_to_mbar(double psi)
|
||||
{
|
||||
return psi_to_bar(psi)*1000 + 0.5;
|
||||
return rint(psi_to_bar(psi)*1000);
|
||||
}
|
||||
|
||||
static inline int to_PSI(pressure_t pressure)
|
||||
{
|
||||
return pressure.mbar * 0.0145037738 + 0.5;
|
||||
return rint(pressure.mbar * 0.0145037738);
|
||||
}
|
||||
|
||||
static inline double bar_to_atm(double bar)
|
||||
|
@ -446,7 +446,7 @@ static inline int calculate_depth_to_mbar(int depth, pressure_t surface_pressure
|
|||
if (!salinity)
|
||||
salinity = SEAWATER_SALINITY;
|
||||
specific_weight = salinity / 10000.0 * 0.981;
|
||||
mbar += depth / 10.0 * specific_weight + 0.5;
|
||||
mbar += rint(depth / 10.0 * specific_weight);
|
||||
return mbar;
|
||||
}
|
||||
|
||||
|
@ -471,7 +471,7 @@ static inline int rel_mbar_to_depth(int mbar, struct dive *dive)
|
|||
if (dive->dc.salinity)
|
||||
specific_weight = dive->dc.salinity / 10000.0 * 0.981;
|
||||
/* whole mbar gives us cm precision */
|
||||
cm = mbar / specific_weight + 0.5;
|
||||
cm = rint(mbar / specific_weight);
|
||||
return cm * 10;
|
||||
}
|
||||
|
||||
|
|
|
@ -226,7 +226,7 @@ static int calculate_otu(struct dive *dive)
|
|||
if (po2 >= 500)
|
||||
otu += pow((po2 - 500) / 1000.0, 0.83) * t / 30.0;
|
||||
}
|
||||
return otu + 0.5;
|
||||
return rint(otu);
|
||||
}
|
||||
/* calculate CNS for a dive - this only takes the first divecomputer into account */
|
||||
int const cns_table[][3] = {
|
||||
|
|
|
@ -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);
|
||||
|
|
16
parse-xml.c
16
parse-xml.c
|
@ -326,7 +326,7 @@ static void pressure(char *buffer, void *_press)
|
|||
break;
|
||||
}
|
||||
if (mbar > 5 && mbar < 500000) {
|
||||
pressure->mbar = mbar + 0.5;
|
||||
pressure->mbar = rint(mbar);
|
||||
break;
|
||||
}
|
||||
/* fallthrough */
|
||||
|
@ -341,7 +341,7 @@ static void salinity(char *buffer, void *_salinity)
|
|||
union int_or_float val;
|
||||
switch (integer_or_float(buffer, &val)) {
|
||||
case FLOAT:
|
||||
*salinity = val.fp * 10.0 + 0.5;
|
||||
*salinity = rint(val.fp * 10.0);
|
||||
break;
|
||||
default:
|
||||
printf("Strange salinity reading %s\n", buffer);
|
||||
|
@ -357,7 +357,7 @@ static void depth(char *buffer, void *_depth)
|
|||
case FLOAT:
|
||||
switch (xml_parsing_units.length) {
|
||||
case METERS:
|
||||
depth->mm = val.fp * 1000 + 0.5;
|
||||
depth->mm = rint(val.fp * 1000);
|
||||
break;
|
||||
case FEET:
|
||||
depth->mm = feet_to_mm(val.fp);
|
||||
|
@ -378,7 +378,7 @@ static void weight(char *buffer, void *_weight)
|
|||
case FLOAT:
|
||||
switch (xml_parsing_units.weight) {
|
||||
case KG:
|
||||
weight->grams = val.fp * 1000 + 0.5;
|
||||
weight->grams = rint(val.fp * 1000);
|
||||
break;
|
||||
case LBS:
|
||||
weight->grams = lbs_to_grams(val.fp);
|
||||
|
@ -472,7 +472,7 @@ static void percent(char *buffer, void *_fraction)
|
|||
|
||||
/* Then turn percent into our integer permille format */
|
||||
if (val >= 0 && val <= 100.0) {
|
||||
fraction->permille = val * 10 + 0.5;
|
||||
fraction->permille = rint(val * 10);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
@ -502,7 +502,7 @@ static void cylindersize(char *buffer, void *_volume)
|
|||
|
||||
switch (integer_or_float(buffer, &val)) {
|
||||
case FLOAT:
|
||||
volume->mliter = val.fp * 1000 + 0.5;
|
||||
volume->mliter = rint(val.fp * 1000);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -549,7 +549,7 @@ static void get_rating(char *buffer, void *_i)
|
|||
static void double_to_permil(char *buffer, void *_i)
|
||||
{
|
||||
int *i = _i;
|
||||
*i = ascii_strtod(buffer, NULL) * 1000.0 + 0.5;
|
||||
*i = rint(ascii_strtod(buffer, NULL) * 1000.0);
|
||||
}
|
||||
|
||||
static void hex_value(char *buffer, void *_i)
|
||||
|
@ -636,7 +636,7 @@ static void psi_or_bar(char *buffer, void *_pressure)
|
|||
if (val.fp > 400)
|
||||
pressure->mbar = psi_to_mbar(val.fp);
|
||||
else
|
||||
pressure->mbar = val.fp * 1000 + 0.5;
|
||||
pressure->mbar = rint(val.fp * 1000);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Crazy Diving Log PSI reading %s\n", buffer);
|
||||
|
|
|
@ -627,7 +627,7 @@ static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi,
|
|||
magic = (interpolate.end - interpolate.start) / (double) interpolate.pressure_time;
|
||||
|
||||
/* Use that overall pressure change to update the current pressure */
|
||||
cur_pr[cyl] = interpolate.start + magic * interpolate.acc_pressure_time + 0.5;
|
||||
cur_pr[cyl] = rint(interpolate.start + magic * interpolate.acc_pressure_time);
|
||||
}
|
||||
INTERPOLATED_PRESSURE(entry) = cur_pr[cyl];
|
||||
}
|
||||
|
|
|
@ -65,7 +65,7 @@ static void uemis_ts(char *buffer, void *_when)
|
|||
/* float minutes */
|
||||
static void uemis_duration(char *buffer, duration_t *duration)
|
||||
{
|
||||
duration->seconds = ascii_strtod(buffer, NULL) * 60 + 0.5;
|
||||
duration->seconds = rint(ascii_strtod(buffer, NULL) * 60);
|
||||
}
|
||||
|
||||
/* int cm */
|
||||
|
|
4
uemis.c
4
uemis.c
|
@ -329,9 +329,9 @@ void uemis_parse_divelog_binary(char *base64, void *datap) {
|
|||
* we store the incorrect working pressure to get the SAC calculations "close"
|
||||
* but the user will have to correct this manually
|
||||
*/
|
||||
dive->cylinder[i].type.size.mliter = volume;
|
||||
dive->cylinder[i].type.size.mliter = rint(volume);
|
||||
dive->cylinder[i].type.workingpressure.mbar = 202600;
|
||||
dive->cylinder[i].gasmix.o2.permille = *(uint8_t *)(data+120+25*(gasoffset + i)) * 10 + 0.5;
|
||||
dive->cylinder[i].gasmix.o2.permille = *(uint8_t *)(data+120+25*(gasoffset + i)) * 10;
|
||||
dive->cylinder[i].gasmix.he.permille = 0;
|
||||
}
|
||||
/* first byte of divelog data is at offset 0x123 */
|
||||
|
|
Loading…
Reference in a new issue