[PATCH] Fix average temperature statistics

Linus Torvalds torvalds at linux-foundation.org
Sun Nov 11 09:37:54 PST 2012


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Sun, 11 Nov 2012 18:32:31 +0100
Subject: [PATCH] Fix average temperature statistics

We generate the average temperature statistics by adding up the
(converted to user unites - not in millikelvin) temperatures and then
dividing by the number of dives we've added up over.

HOWEVER.

We did that summing of the temperatures into an integer variable, even
though the converted temperatures are floating point.  So things got
rounded down to integers and the average temperature was just bogus
(although reasonably close).

We could do the summing of the temperatures in millikelvin and only
doing the conversion to the user at the very end.  But the smaller patch
is to just change the accumulator to a double value.

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---

diff --git a/statistics.c b/statistics.c
index 08fb7381454d..a946d500b7d0 100644
--- a/statistics.c
+++ b/statistics.c
@@ -71,7 +71,7 @@ typedef struct {
 	volume_t avg_sac;
 	int max_temp;
 	int min_temp;
-	unsigned int combined_temp;
+	double combined_temp;
 	unsigned int combined_count;
 	unsigned int selection_size;
 	unsigned int total_sac_time;
@@ -286,7 +286,7 @@ static void process_interval_stats(stats_t stats_interval, GtkTreeIter *parent,
 	/* Average water temperature */
 	value = get_temp_units(stats_interval.min_temp, &unit);
 	if (stats_interval.combined_temp && stats_interval.combined_count) {
-		snprintf(value_str, sizeof(value_str), "%.1f %s", stats_interval.combined_temp / (stats_interval.combined_count * 1.0), unit);
+		snprintf(value_str, sizeof(value_str), "%.1f %s", stats_interval.combined_temp / stats_interval.combined_count, unit);
 		add_cell_to_tree(store, value_str, 12, row);
 	} else {
 		add_cell_to_tree(store, "", 12, row);
@@ -611,7 +611,7 @@ static void show_total_dive_stats(struct dive *dive)
 		set_label(stats_w.min_temp, "%.1f %s", value, unit);
 	}
 	if (stats_ptr->combined_temp && stats_ptr->combined_count)
-		set_label(stats_w.avg_temp, "%.1f %s", stats_ptr->combined_temp / (stats_ptr->combined_count * 1.0), unit);
+		set_label(stats_w.avg_temp, "%.1f %s", stats_ptr->combined_temp / stats_ptr->combined_count, unit);
 	if (stats_ptr->max_temp) {
 		value = get_temp_units(stats_ptr->max_temp, &unit);
 		set_label(stats_w.max_temp, "%.1f %s", value, unit);


More information about the subsurface mailing list