From 0e9eee0a7f2203864ee8812a305adf34d70de38a Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Fri, 24 Sep 2021 15:07:11 +0200 Subject: [PATCH] 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 --- core/dive.c | 2 +- core/gaspressures.c | 2 +- core/units.c | 2 +- core/units.h | 6 +++--- desktop-widgets/diveplanner.cpp | 2 +- tests/testunitconversion.cpp | 4 ++-- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/core/dive.c b/core/dive.c index d84e45b5a..f027d6c3c 100644 --- a/core/dive.c +++ b/core/dive.c @@ -698,7 +698,7 @@ static void match_standard_cylinder(cylinder_type_t *type) bar = type->workingpressure.mbar / 1000.0; cuft = ml_to_cuft(type->size.mliter); cuft *= bar_to_atm(bar); - psi = to_PSI(type->workingpressure); + psi = lrint(to_PSI(type->workingpressure)); switch (psi) { case 2300 ... 2500: /* 2400 psi: LP tank */ diff --git a/core/gaspressures.c b/core/gaspressures.c index 2f60f8442..5acbb038b 100644 --- a/core/gaspressures.c +++ b/core/gaspressures.c @@ -100,7 +100,7 @@ static void dump_pr_track(int cyl, pr_track_t *track_pr) printf("cyl%d:\n", cyl); list = track_pr; 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->end), FRACTION(list->t_start, 60), diff --git a/core/units.c b/core/units.c index 59c09e4dc..bc91a106e 100644 --- a/core/units.c +++ b/core/units.c @@ -16,7 +16,7 @@ int get_pressure_units(int mb, const char **units) unit = translate("gettextFromC", "bar"); break; case PSI: - pressure = mbar_to_PSI(mb); + pressure = (int)lrint(mbar_to_PSI(mb)); unit = translate("gettextFromC", "psi"); break; } diff --git a/core/units.h b/core/units.h index 0ce97e21c..86403731b 100644 --- a/core/units.h +++ b/core/units.h @@ -245,9 +245,9 @@ static inline long psi_to_mbar(double psi) 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) @@ -260,7 +260,7 @@ static inline double mbar_to_atm(int mbar) 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 }; return to_PSI(p); diff --git a/desktop-widgets/diveplanner.cpp b/desktop-widgets/diveplanner.cpp index 5f0930b5b..e5d55f0d2 100644 --- a/desktop-widgets/diveplanner.cpp +++ b/desktop-widgets/diveplanner.cpp @@ -494,7 +494,7 @@ void PlannerSettingsWidget::settingsChanged() ui.reserve_gas->setSuffix(tr("psi")); ui.reserve_gas->setSingleStep(10); 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); diff --git a/tests/testunitconversion.cpp b/tests/testunitconversion.cpp index acba2a5f2..04f5fa1fb 100644 --- a/tests/testunitconversion.cpp +++ b/tests/testunitconversion.cpp @@ -18,10 +18,10 @@ void TestUnitConversion::testUnitConversions() QCOMPARE(C_to_mkelvin(373.85), (unsigned long)647000); QCOMPARE(IS_FP_SAME(psi_to_bar(14.6959488), 1.01325), true); 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(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)