Skip to content

Commit

Permalink
core: replace IS_FP_SAME macro by inline function
Browse files Browse the repository at this point in the history
No reason to keep this as a macro - a function is easier to
read, type safe and easier to debug. Moreover, give it the
more appropriate name "nearly_equal()". After all, it precisely
does NOT check floating points for equality.

Signed-off-by: Berthold Stoeger <[email protected]>
  • Loading branch information
bstoeger authored and atdotde committed Aug 30, 2022
1 parent 5db4a95 commit 6170150
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 25 deletions.
4 changes: 2 additions & 2 deletions core/parse-xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ static enum number_type parse_float(const char *buffer, double *res, const char
if (errno || *endp == buffer)
return NEITHER;
if (**endp == ',') {
if (IS_FP_SAME(val, rint(val))) {
if (nearly_equal(val, rint(val))) {
/* we really want to send an error if this is a Subsurface native file
* as this is likely indication of a bug - but right now we don't have
* that information available */
Expand Down Expand Up @@ -604,7 +604,7 @@ static void fahrenheit(char *buffer, temperature_t *temperature)

switch (integer_or_float(buffer, &val)) {
case FLOATVAL:
if (IS_FP_SAME(val.fp, 32.0))
if (nearly_equal(val.fp, 32.0))
break;
if (val.fp < 32.0)
temperature->mkelvin = C_to_mkelvin(val.fp);
Expand Down
6 changes: 3 additions & 3 deletions core/settings/qPrefDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void qPrefDisplay::disk_divelist_font(bool doSync)

void qPrefDisplay::set_font_size(double value)
{
if (!IS_FP_SAME(value, prefs.font_size)) {
if (!nearly_equal(value, prefs.font_size)) {
prefs.font_size = value;
disk_font_size(true);

Expand All @@ -121,7 +121,7 @@ void qPrefDisplay::disk_font_size(bool doSync)

void qPrefDisplay::set_mobile_scale(double value)
{
if (!IS_FP_SAME(value, prefs.mobile_scale)) {
if (!nearly_equal(value, prefs.mobile_scale)) {
prefs.mobile_scale = value;
disk_mobile_scale(true);

Expand Down Expand Up @@ -159,7 +159,7 @@ void qPrefDisplay::setCorrectFont()
// get the font from the settings or our defaults
// respect the system default font size if none is explicitly set
QFont defaultFont = qPrefPrivate::propValue(keyFromGroupAndName(group, "divelist_font"), prefs.divelist_font).value<QFont>();
if (IS_FP_SAME(system_divelist_default_font_size, -1.0)) {
if (nearly_equal(system_divelist_default_font_size, -1.0)) {
prefs.font_size = qApp->font().pointSizeF();
system_divelist_default_font_size = prefs.font_size; // this way we don't save it on exit
}
Expand Down
2 changes: 1 addition & 1 deletion core/settings/qPrefPrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void qPrefPrivate::propSetValue(const QString &key, const QVariant &value, const
#else
if (value.isValid() && value.type() == QVariant::Double)
#endif
isDefault = IS_FP_SAME(value.toDouble(), defaultValue.toDouble());
isDefault = nearly_equal(value.toDouble(), defaultValue.toDouble());
else
isDefault = (value == defaultValue);

Expand Down
9 changes: 6 additions & 3 deletions core/subsurface-string.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <math.h>

// shared generic definitions and macros
// mostly about strings, but a couple of math macros are here as well
Expand All @@ -22,13 +23,15 @@
(void) (&_max1 == &_max2); \
_max1 > _max2 ? _max1 : _max2; })

#define IS_FP_SAME(_a, _b) (fabs((_a) - (_b)) <= 0.000001 * MAX(fabs(_a), fabs(_b)))


#ifdef __cplusplus
extern "C" {
#endif

static inline bool nearly_equal(double a, double b)
{
return fabs(a - b) <= 1e-6 * fmax(fabs(a), fabs(b));
}

static inline bool nearly_0(double fp)
{
return fabs(fp) <= 1e-6;
Expand Down
2 changes: 1 addition & 1 deletion desktop-widgets/downloadfromdivecomputer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void DownloadFromDCWidget::updateProgressBar()
if (empty_string(last_text)) {
// if we get the first actual text after the download is finished
// (which happens for example on the OSTC), then don't bother
if (!empty_string(progress_bar_text) && IS_FP_SAME(progress_bar_fraction, 1.0))
if (!empty_string(progress_bar_text) && nearly_equal(progress_bar_fraction, 1.0))
progress_bar_text = "";
}
if (!empty_string(progress_bar_text)) {
Expand Down
2 changes: 1 addition & 1 deletion mobile-widgets/themeinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ double ThemeInterface::currentScale()

void ThemeInterface::set_currentScale(double newScale)
{
if (!IS_FP_SAME(newScale, qPrefDisplay::mobile_scale())) {
if (!nearly_equal(newScale, qPrefDisplay::mobile_scale())) {
qPrefDisplay::set_mobile_scale(newScale);
emit currentScaleChanged();
m_needSignals = true;
Expand Down
2 changes: 1 addition & 1 deletion profile-widget/divecartesianaxis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ double DiveCartesianAxis::posAtValue(double value, double max, double min) const

double screenFrom = position == Position::Bottom ? m.x1() : m.y1();
double screenTo = position == Position::Bottom ? m.x2() : m.y2();
if (IS_FP_SAME(min, max))
if (nearly_equal(min, max))
return (screenFrom + screenTo) / 2.0;
if ((position == Position::Bottom) == inverted)
std::swap(screenFrom, screenTo);
Expand Down
4 changes: 2 additions & 2 deletions profile-widget/qmlprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void QMLProfile::setDevicePixelRatio(qreal dpr)
// don't update the profile here, have the user update x and y and then manually trigger an update
void QMLProfile::setXOffset(qreal value)
{
if (IS_FP_SAME(value, m_xOffset))
if (nearly_equal(value, m_xOffset))
return;
m_xOffset = value;
emit xOffsetChanged();
Expand All @@ -105,7 +105,7 @@ void QMLProfile::setXOffset(qreal value)
// don't update the profile here, have the user update x and y and then manually trigger an update
void QMLProfile::setYOffset(qreal value)
{
if (IS_FP_SAME(value, m_yOffset))
if (nearly_equal(value, m_yOffset))
return;
m_yOffset = value;
emit yOffsetChanged();
Expand Down
22 changes: 11 additions & 11 deletions tests/testunitconversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@

void TestUnitConversion::testUnitConversions()
{
QCOMPARE(IS_FP_SAME(grams_to_lbs(1000), 2.204586), true);
QCOMPARE(nearly_equal(grams_to_lbs(1000), 2.204586), true);
QCOMPARE(lbs_to_grams(1), 454);
QCOMPARE(IS_FP_SAME(ml_to_cuft(1000), 0.0353147), true);
QCOMPARE(IS_FP_SAME(cuft_to_l(1), 28.316847), true);
QCOMPARE(IS_FP_SAME(mm_to_feet(1000), 3.280840), true);
QCOMPARE(nearly_equal(ml_to_cuft(1000), 0.0353147), true);
QCOMPARE(nearly_equal(cuft_to_l(1), 28.316847), true);
QCOMPARE(nearly_equal(mm_to_feet(1000), 3.280840), true);
QCOMPARE(feet_to_mm(1), 305L);
QCOMPARE(to_feet((depth_t){1000}), 3);
QCOMPARE(IS_FP_SAME(mkelvin_to_C(647000), 373.85), true);
QCOMPARE(IS_FP_SAME(mkelvin_to_F(647000), 704.93), true);
QCOMPARE(nearly_equal(mkelvin_to_C(647000), 373.85), true);
QCOMPARE(nearly_equal(mkelvin_to_F(647000), 704.93), true);
QCOMPARE(F_to_mkelvin(704.93), 647000UL);
QCOMPARE(C_to_mkelvin(373.85), 647000UL);
QCOMPARE(IS_FP_SAME(psi_to_bar(14.6959488), 1.01325), true);
QCOMPARE(nearly_equal(psi_to_bar(14.6959488), 1.01325), true);
QCOMPARE(psi_to_mbar(14.6959488), 1013L);
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(IS_FP_SAME(mbar_to_PSI(1013), 14.6923228594), true);
QCOMPARE(nearly_equal(to_PSI((pressure_t){1013}), 14.6923228594), true);
QCOMPARE(nearly_equal(bar_to_atm(1.013), 1.0), true);
QCOMPARE(nearly_equal(mbar_to_atm(1013), 1.0), true);
QCOMPARE(nearly_equal(mbar_to_PSI(1013), 14.6923228594), true);
}

QTEST_GUILESS_MAIN(TestUnitConversion)

0 comments on commit 6170150

Please sign in to comment.