core: return floating point from to_PSI() functions

Dive data are stored internally using integral types using
appropriately fine units (mm, mbar, mkelvin, etc.). These
are converted with functions defined in units.h for display
(m, bar, C, etc.). Usually floating points are returned by
these functions, to retain the necessary precision. There
is one exception: the to_PSI() and mbar_to_PSI() functions.

For consistency, make these functions likewise return floats.
This will be needed for the rework of the profile-axes.
The plan is to use the conversion functions to make the
axes aware of the displayed values. This in turn will be
necessary to place the ticks at sensible distances. However,
the conversions need to be precise, which is not the
case for the current to_PSI() functions.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-09-24 15:07:11 +02:00 committed by Dirk Hohndel
parent 322e2baa8d
commit 0e9eee0a7f
6 changed files with 9 additions and 9 deletions

View file

@ -698,7 +698,7 @@ static void match_standard_cylinder(cylinder_type_t *type)
bar = type->workingpressure.mbar / 1000.0; bar = type->workingpressure.mbar / 1000.0;
cuft = ml_to_cuft(type->size.mliter); cuft = ml_to_cuft(type->size.mliter);
cuft *= bar_to_atm(bar); cuft *= bar_to_atm(bar);
psi = to_PSI(type->workingpressure); psi = lrint(to_PSI(type->workingpressure));
switch (psi) { switch (psi) {
case 2300 ... 2500: /* 2400 psi: LP tank */ case 2300 ... 2500: /* 2400 psi: LP tank */

View file

@ -100,7 +100,7 @@ static void dump_pr_track(int cyl, pr_track_t *track_pr)
printf("cyl%d:\n", cyl); printf("cyl%d:\n", cyl);
list = track_pr; list = track_pr;
while (list) { while (list) {
printf(" start %d end %d t_start %d:%02d t_end %d:%02d pt %d\n", printf(" start %f end %f t_start %d:%02d t_end %d:%02d pt %d\n",
mbar_to_PSI(list->start), mbar_to_PSI(list->start),
mbar_to_PSI(list->end), mbar_to_PSI(list->end),
FRACTION(list->t_start, 60), FRACTION(list->t_start, 60),

View file

@ -16,7 +16,7 @@ int get_pressure_units(int mb, const char **units)
unit = translate("gettextFromC", "bar"); unit = translate("gettextFromC", "bar");
break; break;
case PSI: case PSI:
pressure = mbar_to_PSI(mb); pressure = (int)lrint(mbar_to_PSI(mb));
unit = translate("gettextFromC", "psi"); unit = translate("gettextFromC", "psi");
break; break;
} }

View file

@ -245,9 +245,9 @@ static inline long psi_to_mbar(double psi)
return lrint(psi_to_bar(psi) * 1000); return lrint(psi_to_bar(psi) * 1000);
} }
static inline int to_PSI(pressure_t pressure) static inline double to_PSI(pressure_t pressure)
{ {
return (int)lrint(pressure.mbar * 0.0145037738); return pressure.mbar * 0.0145037738;
} }
static inline double bar_to_atm(double bar) static inline double bar_to_atm(double bar)
@ -260,7 +260,7 @@ static inline double mbar_to_atm(int mbar)
return (double)mbar / SURFACE_PRESSURE; return (double)mbar / SURFACE_PRESSURE;
} }
static inline int mbar_to_PSI(int mbar) static inline double mbar_to_PSI(int mbar)
{ {
pressure_t p = { mbar }; pressure_t p = { mbar };
return to_PSI(p); return to_PSI(p);

View file

@ -494,7 +494,7 @@ void PlannerSettingsWidget::settingsChanged()
ui.reserve_gas->setSuffix(tr("psi")); ui.reserve_gas->setSuffix(tr("psi"));
ui.reserve_gas->setSingleStep(10); ui.reserve_gas->setSingleStep(10);
ui.reserve_gas->setMaximum(5000); ui.reserve_gas->setMaximum(5000);
ui.reserve_gas->setValue(mbar_to_PSI(prefs.reserve_gas)); ui.reserve_gas->setValue(lrint(mbar_to_PSI(prefs.reserve_gas)));
} }
ui.bottomSAC->blockSignals(false); ui.bottomSAC->blockSignals(false);

View file

@ -18,10 +18,10 @@ void TestUnitConversion::testUnitConversions()
QCOMPARE(C_to_mkelvin(373.85), (unsigned long)647000); QCOMPARE(C_to_mkelvin(373.85), (unsigned long)647000);
QCOMPARE(IS_FP_SAME(psi_to_bar(14.6959488), 1.01325), true); QCOMPARE(IS_FP_SAME(psi_to_bar(14.6959488), 1.01325), true);
QCOMPARE(psi_to_mbar(14.6959488), (long)1013); QCOMPARE(psi_to_mbar(14.6959488), (long)1013);
QCOMPARE(to_PSI((pressure_t){1013}), (int)15); QCOMPARE(IS_FP_SAME(to_PSI((pressure_t){1013}), 14.6923228594), true);
QCOMPARE(IS_FP_SAME(bar_to_atm(1.013), 1.0), true); QCOMPARE(IS_FP_SAME(bar_to_atm(1.013), 1.0), true);
QCOMPARE(IS_FP_SAME(mbar_to_atm(1013), 1.0), true); QCOMPARE(IS_FP_SAME(mbar_to_atm(1013), 1.0), true);
QCOMPARE(mbar_to_PSI(1013), (int)15); QCOMPARE(IS_FP_SAME(mbar_to_PSI(1013), 14.6923228594), true);
} }
QTEST_GUILESS_MAIN(TestUnitConversion) QTEST_GUILESS_MAIN(TestUnitConversion)