From 4033625567eafbb2c3c69da9189a0b0c87b74abc Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Sat, 30 Jun 2012 20:12:11 -0700 Subject: [PATCH] Fix a couple of possible divide-by-zero conditions in statistics Several people reported the average time problem, but there's another one lurking there too: if the dive duration is zero, you get bogus average depth information too (but because that one was a floating point divide, and by default they are unsignalling on x86, it didn't crash, it just resulted in bogus results). Signed-off-by: Linus Torvalds --- statistics.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/statistics.c b/statistics.c index 34f487b..1910565 100644 --- a/statistics.c +++ b/statistics.c @@ -91,6 +91,18 @@ static void process_dive(struct dive *dp, stats_t *stats) stats->max_depth.mm = dp->maxdepth.mm; if (stats->min_depth.mm == 0 || dp->maxdepth.mm < stats->min_depth.mm) stats->min_depth.mm = dp->maxdepth.mm; + if (dp->watertemp.mkelvin) { + if (stats->min_temp == 0 || dp->watertemp.mkelvin < stats->min_temp) + stats->min_temp = dp->watertemp.mkelvin; + if (dp->watertemp.mkelvin > stats->max_temp) + stats->max_temp = dp->watertemp.mkelvin; + stats->combined_temp += get_temp_units(dp->watertemp.mkelvin, &unit); + stats->combined_count++; + } + + /* Maybe we should drop zero-duration dives */ + if (!dp->duration.seconds) + return; stats->avg_depth.mm = (1.0 * old_tt * stats->avg_depth.mm + dp->duration.seconds * dp->meandepth.mm) / stats->total_time.seconds; if (dp->sac > 2800) { /* less than .1 cuft/min (2800ml/min) is bogus */ @@ -103,14 +115,6 @@ static void process_dive(struct dive *dp, stats_t *stats) stats->min_sac.mliter = dp->sac; stats->total_sac_time = sac_time; } - if (dp->watertemp.mkelvin) { - if (stats->min_temp == 0 || dp->watertemp.mkelvin < stats->min_temp) - stats->min_temp = dp->watertemp.mkelvin; - if (dp->watertemp.mkelvin > stats->max_temp) - stats->max_temp = dp->watertemp.mkelvin; - stats->combined_temp += get_temp_units(dp->watertemp.mkelvin, &unit); - stats->combined_count++; - } } static void process_all_dives(struct dive *dive, struct dive **prev_dive) @@ -266,7 +270,7 @@ static void show_single_dive_stats(struct dive *dive) static void show_total_dive_stats(struct dive *dive) { double value; - int decimals; + int decimals, seconds; const char *unit; stats_t *stats_ptr; @@ -287,7 +291,10 @@ static void show_total_dive_stats(struct dive *dive) set_label(stats_w.max_temp, "%.1f %s", value, unit); } set_label(stats_w.total_time, get_time_string(stats_ptr->total_time.seconds, 0)); - set_label(stats_w.avg_time, get_time_string(stats_ptr->total_time.seconds / stats_ptr->selection_size, 0)); + seconds = stats_ptr->total_time.seconds; + if (stats_ptr->selection_size) + seconds /= stats_ptr->selection_size; + set_label(stats_w.avg_time, get_time_string(seconds, 0)); set_label(stats_w.longest_time, get_time_string(stats_ptr->longest_time.seconds, 0)); set_label(stats_w.shortest_time, get_time_string(stats_ptr->shortest_time.seconds, 0)); value = get_depth_units(stats_ptr->max_depth.mm, &decimals, &unit); -- 2.43.0