[PATCH 3/3] Fix temperature rounding issues

Linus Torvalds torvalds at linux-foundation.org
Wed Jan 23 12:46:22 PST 2013


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Wed, 23 Jan 2013 12:33:26 -0800
Subject: [PATCH 3/3] Fix temperature rounding issues

Temperatures can actually be negative, which means that rounding by
adding 0.5 and casting to 'int' is not correct.

We could use '(int)(rint(val))' instead, but the only place we care
about might as well just print out the floating point representation
with a precision of two digits instead.  So if you have a dive computer
that gives you the precision, you might see '3.5˚C' as the temperature.

Remove the helper functions that nobody uses and that get the rounding
wrong anyway.

Reported-by: Henrik Brautaset Aronsen <subsurface at henrik.synth.no>
Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---

.. and this gives us the right temperature printout in the profile for 
negative temperatures.

 dive.h    | 21 ---------------------
 profile.c |  2 +-
 2 files changed, 1 insertion(+), 22 deletions(-)

diff --git a/dive.h b/dive.h
index e76b6dadfbda..fc82f5e43e1a 100644
--- a/dive.h
+++ b/dive.h
@@ -179,27 +179,6 @@ static inline unsigned long C_to_mkelvin(double c)
 	return c * 1000 + 273150.5;
 }
 
-static inline int to_C(temperature_t temp)
-{
-	if (!temp.mkelvin)
-		return 0;
-	return mkelvin_to_C(temp.mkelvin) + 0.5;
-}
-
-static inline int to_F(temperature_t temp)
-{
-	if (!temp.mkelvin)
-		return 0;
-	return mkelvin_to_F(temp.mkelvin) + 0.5;
-}
-
-static inline int to_K(temperature_t temp)
-{
-	if (!temp.mkelvin)
-		return 0;
-	return (temp.mkelvin + 499)/1000;
-}
-
 static inline double psi_to_bar(double psi)
 {
 	return psi / 14.5037738;
diff --git a/profile.c b/profile.c
index 18987c27474e..4aa70ad112ab 100644
--- a/profile.c
+++ b/profile.c
@@ -894,7 +894,7 @@ static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkel
 
 	deg = get_temp_units(mkelvin, &unit);
 
-	plot_text(gc, &tro, sec, mkelvin, "%d%s", (int)(deg + 0.5), unit);
+	plot_text(gc, &tro, sec, mkelvin, "%.2g%s", deg, unit);
 }
 
 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
-- 
1.8.1.1.271.g02f55e6



More information about the subsurface mailing list