diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2013-02-09 10:23:15 +1100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2013-02-08 18:15:51 -0800 |
commit | b286ea638c8ce468cf9c1a1b5037867acb5d6fe4 (patch) | |
tree | e6d593baf90d399b0532e0b6033905f219692483 /dive.c | |
parent | ff969cd5c84824170b382901b49a97564bf72bfa (diff) | |
download | subsurface-b286ea638c8ce468cf9c1a1b5037867acb5d6fe4.tar.gz |
Simplify/clarify the get_surface_pressure_in_mbar() function
Instead of maintaining a rolling average and re-calculating it at each
stage, just calculate the surface_pressure average the natural way: as
the sum divided by the number of entries.
This results in a single rounding, rather than doing rounding multiple
times and possibly rounding wrong as a result.
Not that we care all that deeply about the LSB of the mbar value, but
the code is simpler and more obvious this way too.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'dive.c')
-rw-r--r-- | dive.c | 25 |
1 files changed, 15 insertions, 10 deletions
@@ -232,18 +232,23 @@ int get_duration_in_sec(struct dive *dive) int get_surface_pressure_in_mbar(const struct dive *dive, gboolean non_null) { - int count = 0, pressure = 0; + unsigned int count = 0, sum = 0; const struct divecomputer *dc = &dive->dc; + do { - if (dc->surface_pressure.mbar) { - pressure = (double)(count * pressure + dc->surface_pressure.mbar) / (count + 1) + 0.5; - count++; - } - dc = dc->next; - } while (dc); - if (!pressure && non_null) - pressure = SURFACE_PRESSURE; - return pressure; + if (!dc->surface_pressure.mbar) + continue; + sum += dc->surface_pressure.mbar; + count++; + } while ((dc = dc->next) != NULL); + + /* Did we have any dive computers with surface pressure information */ + if (count) + return (sum + count/2) / count; + + if (non_null) + return SURFACE_PRESSURE; + return 0; } static void update_temperature(temperature_t *temperature, int new) |