> On 05 Jul 2015, at 21:31, Dirk Hohndel <[email protected]> wrote:
> 
> On Sun, Jul 05, 2015 at 12:11:15PM -0700, Linus Torvalds wrote:
>> On Sun, Jul 5, 2015 at 11:56 AM, Dirk Hohndel <[email protected]> wrote:
>>> static inline depth_t gas_mod(struct gasmix *mix, pressure_t po2_limit, int 
>>> roundto) {
>>>        depth_t depth;
>>>        depth.mm = ((po2_limit.mbar * 1000 / get_o2(mix) * 10 - 10000) / 
>>> roundto) * roundto;
>>>        return depth;
>>> }
>>> 
>>> So all we need to do is adjust this function to add (roundto - 1) to the
>>> enumerator before deviding by roundto, correct?
>> 
>> Ugh. I think it would be better to write it more readably first.
>> That's a particularly unreadable line of noise.
> 
> Well, Robert wrote it, now he can fix it.

I couldn’t remember but git blame says so. But I only added the rounding, the 
poor man’s pressure to depth conversion was there before.

So, here is a patch that gets the maths right (boy, that was more complicated 
than I thought).

I did not touch the roundto arguments that this is called with (3m or 10ft) as 
I would still think this is what people expect. But feel free to change those 
(but only in a way that gives 6m/20ft for O2 and 21m/70ft for EAN50 please).

The second patch fixes a compiler warning. I did not really check the logic to 
figure out what was actually meant there but the compiler is right: What’s was 
written there makes no sense (to me).

Best
Robert
From e90e3a42d769ce2c23c40741c447dbfb32edb693 Mon Sep 17 00:00:00 2001
From: "Robert C. Helling" <[email protected]>
Date: Mon, 6 Jul 2015 00:07:39 +0200
Subject: [PATCH 1/2] To compute expected MOD of gas round rather than truncate

For the proper calculation, we need to take salinity and
surface pressure into account (rather than depth = bar * 10 - 10)

Signed-off-by: Robert C. Helling <[email protected]>
---
 dive.h                         | 26 +++++++++++++++++++-------
 equipment.c                    |  2 +-
 planner.c                      |  2 +-
 profile.c                      |  2 +-
 qt-models/cylindermodel.cpp    |  2 +-
 qt-models/diveplannermodel.cpp |  2 +-
 6 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/dive.h b/dive.h
index 9019511..fcc01a8 100644
--- a/dive.h
+++ b/dive.h
@@ -167,13 +167,6 @@ static inline int interpolate(int a, int b, int part, int 
whole)
        return rint(x / whole);
 }
 
-/* MOD rounded to multiples of roundto mm */
-static inline depth_t gas_mod(struct gasmix *mix, pressure_t po2_limit, int 
roundto) {
-       depth_t depth;
-       depth.mm = ((po2_limit.mbar * 1000 / get_o2(mix) * 10 - 10000) / 
roundto) * roundto;
-       return depth;
-}
-
 void get_gas_string(const struct gasmix *gasmix, char *text, int len);
 const char *gasname(const struct gasmix *gasmix);
 
@@ -445,6 +438,25 @@ static inline int rel_mbar_to_depth(int mbar, struct dive 
*dive)
        return cm * 10;
 }
 
+static inline int mbar_to_depth(int mbar, struct dive *dive)
+{
+       pressure_t surface_pressure;
+       if (dive->surface_pressure.mbar)
+               surface_pressure = dive->surface_pressure;
+       else
+               surface_pressure.mbar = SURFACE_PRESSURE;
+       return rel_mbar_to_depth(mbar - surface_pressure.mbar, dive);
+}
+
+/* MOD rounded to multiples of roundto mm */
+static inline depth_t gas_mod(struct gasmix *mix, pressure_t po2_limit, struct 
dive *dive, int roundto) {
+       depth_t rounded_depth;
+
+       double depth = (double) mbar_to_depth(po2_limit.mbar * 1000 / 
get_o2(mix), dive);
+       rounded_depth.mm = rint(depth / roundto) * roundto;
+       return rounded_depth;
+}
+
 #define SURFACE_THRESHOLD 750 /* somewhat arbitrary: only below 75cm is it 
really diving */
 
 /* this is a global spot for a temporary dive structure that we use to
diff --git a/equipment.c b/equipment.c
index aebac51..47c4397 100644
--- a/equipment.c
+++ b/equipment.c
@@ -226,7 +226,7 @@ void reset_cylinders(struct dive *dive, bool track_gas)
                if (cylinder_none(cyl))
                        continue;
                if (cyl->depth.mm == 0) /* if the gas doesn't give a mod, 
calculate based on prefs */
-                       cyl->depth = gas_mod(&cyl->gasmix, decopo2, 
M_OR_FT(3,10));
+                       cyl->depth = gas_mod(&cyl->gasmix, decopo2, dive, 
M_OR_FT(3,10));
                if (track_gas)
                        cyl->start.mbar = cyl->end.mbar = 
cyl->type.workingpressure.mbar;
                cyl->gas_used.mliter = 0;
diff --git a/planner.c b/planner.c
index fcca0d7..b04c835 100644
--- a/planner.c
+++ b/planner.c
@@ -176,7 +176,7 @@ void fill_default_cylinder(cylinder_t *cyl)
                        cyl->type.size.mliter = cuft_to_l(ti->cuft) * 1000 / 
bar_to_atm(psi_to_bar(ti->psi));
        }
        // MOD of air
-       cyl->depth = gas_mod(&cyl->gasmix, pO2, 1);
+       cyl->depth = gas_mod(&cyl->gasmix, pO2, &displayed_dive, 1);
 }
 
 /* make sure that the gas we are switching to is represented in our
diff --git a/profile.c b/profile.c
index 8deff29..74382b0 100644
--- a/profile.c
+++ b/profile.c
@@ -963,7 +963,7 @@ static void calculate_gas_information_new(struct dive 
*dive, struct plot_info *p
                 * END takes O₂ + N₂ (air) into account ("Narcotic" for 
trimix dives)
                 * EAD just uses N₂ ("Air" for nitrox dives) */
                pressure_t modpO2 = { .mbar = (int)(prefs.modpO2 * 1000) };
-               entry->mod = 
(double)gas_mod(&dive->cylinder[cylinderindex].gasmix, modpO2, 1).mm;
+               entry->mod = 
(double)gas_mod(&dive->cylinder[cylinderindex].gasmix, modpO2, dive, 1).mm;
                entry->end = (entry->depth + 10000) * (1000 - fhe) / 1000.0 - 
10000;
                entry->ead = (entry->depth + 10000) * fn2 / (double)N2_IN_AIR - 
10000;
                entry->eadd = (entry->depth + 10000) *
diff --git a/qt-models/cylindermodel.cpp b/qt-models/cylindermodel.cpp
index a3ab345..8786f78 100644
--- a/qt-models/cylindermodel.cpp
+++ b/qt-models/cylindermodel.cpp
@@ -218,7 +218,7 @@ bool CylindersModel::setData(const QModelIndex &index, 
const QVariant &value, in
                                                prefs.o2consumption / 
prefs.decosac / prefs.pscr_ratio;
                        else
                                modpO2.mbar = prefs.decopo2;
-                       cyl->depth = gas_mod(&cyl->gasmix, modpO2, M_OR_FT(3, 
10));
+                       cyl->depth = gas_mod(&cyl->gasmix, modpO2, 
&displayed_dive, M_OR_FT(3, 10));
                        changed = true;
                }
                break;
diff --git a/qt-models/diveplannermodel.cpp b/qt-models/diveplannermodel.cpp
index 439e7a4..e4cff38 100644
--- a/qt-models/diveplannermodel.cpp
+++ b/qt-models/diveplannermodel.cpp
@@ -536,7 +536,7 @@ bool DivePlannerPointsModel::addGas(struct gasmix mix)
                                                prefs.o2consumption / 
prefs.decosac / prefs.pscr_ratio;
                        else
                                modpO2.mbar = prefs.decopo2;
-                       cyl->depth = gas_mod(&mix, modpO2, M_OR_FT(3,10));
+                       cyl->depth = gas_mod(&mix, modpO2, &displayed_dive, 
M_OR_FT(3,10));
 
 
 
-- 
1.9.5 (Apple Git-50.3)

From 2d868b16d18fd936cc1a4b022b23dd588f7dbcf6 Mon Sep 17 00:00:00 2001
From: "Robert C. Helling" <[email protected]>
Date: Mon, 6 Jul 2015 00:09:19 +0200
Subject: [PATCH 2/2] Correct logic

At least my compiler warns about ! binding stronger than == and
thus comparing a bool to an int. I guess this is what was meant.

Signed-off-by: Robert C. Helling <[email protected]>
---
 qt-ui/maintab.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp
index c0f5434..f698dbb 100644
--- a/qt-ui/maintab.cpp
+++ b/qt-ui/maintab.cpp
@@ -966,7 +966,7 @@ void MainTab::acceptChanges()
                                fixup_dive(d);
                }
        }
-       if (!editMode == TRIP && current_dive->divetrip) {
+       if (editMode != TRIP && current_dive->divetrip) {
                current_dive->divetrip->when = current_dive->when;
                find_new_trip_start_time(current_dive->divetrip);
        }
-- 
1.9.5 (Apple Git-50.3)

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

_______________________________________________
subsurface mailing list
[email protected]
http://lists.subsurface-divelog.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to