From 23baf20f569f1f34ab4efa68996f24c3ad49fe05 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Wed, 12 Feb 2014 14:19:53 -0800 Subject: 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 Signed-off-by: Dirk Hohndel --- parse-xml.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'parse-xml.c') diff --git a/parse-xml.c b/parse-xml.c index 3c536ed4b..2a98d3904 100644 --- a/parse-xml.c +++ b/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); -- cgit v1.2.3-70-g09d2