Skip to content

Commit

Permalink
EAD and END: Treat trivial cases separately
Browse files Browse the repository at this point in the history
If there is no helium the breathing mix, the END is just the current
depth. Simiarly for air and the EAD. Treating these trivial cases
separately makes sure there are no rounding problems happening
when doing consecutive multiplication and division by the same
floating point number.

Signed-off-by: Robert C. Helling <helling@atdotde.de>
  • Loading branch information
atdotde committed Dec 5, 2021
1 parent 315ff9d commit bccce0f
Show file tree
Hide file tree
Showing 3 changed files with 27,873 additions and 27,865 deletions.
16 changes: 12 additions & 4 deletions core/profile.c
Expand Up @@ -1243,8 +1243,8 @@ static void calculate_gas_information_new(const struct dive *dive, const struct
amb_pressure = depth_to_bar(entry->depth, dive);
current_divemode = get_current_divemode(dc, entry->sec, &evd, &current_divemode);
fill_pressures(&entry->pressures, amb_pressure, gasmix, (current_divemode == OC) ? 0.0 : entry->o2pressure.mbar / 1000.0, current_divemode);
fn2 = (1000.0 * entry->pressures.n2 / amb_pressure);
fhe = (1000.0 * entry->pressures.he / amb_pressure);
fn2 = 1000.0 * entry->pressures.n2 / amb_pressure;
fhe = 1000.0 * entry->pressures.he / amb_pressure;
if (dc->divemode == PSCR) { // OC pO2 is calulated for PSCR with or without external PO2 monitoring.
struct gasmix gasmix2 = get_gasmix(dive, dc, entry->sec, &evg, gasmix);
entry->scr_OC_pO2.mbar = (int) depth_to_mbar(entry->depth, dive) * get_o2(gasmix2) / 1000;
Expand All @@ -1256,8 +1256,16 @@ static void calculate_gas_information_new(const struct dive *dive, const struct
* EAD just uses N₂ ("Air" for nitrox dives) */
pressure_t modpO2 = { .mbar = (int)(prefs.modpO2 * 1000) };
entry->mod = gas_mod(gasmix, modpO2, dive, 1).mm;
entry->end = mbar_to_depth((int)(depth_to_mbarf(entry->depth, dive) * (1000 - fhe) / 1000.0), dive);
entry->ead = mbar_to_depth((int)(depth_to_mbarf(entry->depth, dive) * fn2 / (double)N2_IN_AIR), dive);
// Make shure there trivial cases get the right answer and there are no floating point mishaps
if (entry->pressures.he)
entry->end = mbar_to_depth((int)(depth_to_mbarf(entry->depth, dive) * (1000 - fhe) / 1000.0), dive);
else
entry->end = entry->depth;

if (gasmix_is_air(gasmix))
entry->ead = entry->depth;
else
entry->ead = mbar_to_depth((int)(depth_to_mbarf(entry->depth, dive) * fn2 / (double)N2_IN_AIR), dive);
entry->eadd = mbar_to_depth((int)(depth_to_mbarf(entry->depth, dive) *
(entry->pressures.o2 / amb_pressure * O2_DENSITY +
entry->pressures.n2 / amb_pressure * N2_DENSITY +
Expand Down

0 comments on commit bccce0f

Please sign in to comment.