core: replace IS_FP_SAME macro by inline function

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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-08-30 17:55:43 +02:00 committed by Robert C. Helling
parent 5db4a95a26
commit 61701509b0
9 changed files with 28 additions and 25 deletions

View file

@ -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 */
@ -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);

View file

@ -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);
@ -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);
@ -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
}

View file

@ -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);

View file

@ -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
@ -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;