From a748e7f239f29f5aa63a7ff221d1bd5081e698a2 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Mon, 20 Nov 2017 21:39:50 +0100 Subject: [PATCH] Unify float calulations: use double Internal floating point (FP) calculations should be performed using double unless there is a very good reason. This avoids headaches with conversions. Indeed, the vast majority of FP calculations were already done using double. This patch adapts most remaining calculations. Not converted where things that were based on binary representations and variables which weren't used anyway. An analysis of all instances follows: core/plannernotes.c, l.404: This was a comparison between two floats. On the left side, first an integer was cast to float then multiplied with and integer and divided by a constant double. The right hand side was an integer cast to a float. Simply divide by 1000.0 first to convert to double and continue with calculations. On the right hand side, remove the cast, because the integer will be implicitely cast to double for comparison. This conversion actually emits less instructions, because no conversion to double and back is performed. core/planner.c, l.613: Same analysis as previous case. subsurface-desktop-main.cpp, l.155: A local variable representing the version OpenGL version. Turn this into integer logic. Not only does this avoid dreaded FP rounding issues, it also works correctly for minor version > 10 (not that such a thing is to be expected anytime soon). abstractpreferenceswidget.[h/cpp]: A widget where the position is described as a float. Turn into double. desktop-widgets/divelogexportdialog.cpp, l.313: total_weight is described as float. Use double arithmetics instead. This instance fixes a truncation warning emitted by gcc. --- core/planner.c | 2 +- core/plannernotes.c | 4 ++-- desktop-widgets/divelogexportdialog.cpp | 2 +- desktop-widgets/preferences/abstractpreferenceswidget.cpp | 4 ++-- desktop-widgets/preferences/abstractpreferenceswidget.h | 6 +++--- subsurface-desktop-main.cpp | 8 ++++---- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/core/planner.c b/core/planner.c index 8fb03015b..8ace46e35 100644 --- a/core/planner.c +++ b/core/planner.c @@ -610,7 +610,7 @@ bool enough_gas(int current_cylinder) if (!cyl->start.mbar) return true; if (cyl->type.size.mliter) - return (float)(cyl->end.mbar - prefs.reserve_gas) * cyl->type.size.mliter / 1000.0 > (float) cyl->deco_gas_used.mliter; + return (cyl->end.mbar - prefs.reserve_gas) / 1000.0 * cyl->type.size.mliter > cyl->deco_gas_used.mliter; else return true; } diff --git a/core/plannernotes.c b/core/plannernotes.c index 2e38aaff9..1773705b4 100644 --- a/core/plannernotes.c +++ b/core/plannernotes.c @@ -401,8 +401,8 @@ void add_plan_to_notes(struct diveplan *diveplan, struct dive *dive, bool show_d translate("gettextFromC", "Warning:"), translate("gettextFromC", "this is more gas than available in the specified cylinder!")); else - if ((float) cyl->end.mbar * cyl->type.size.mliter / 1000.0 / gas_compressibility_factor(&cyl->gasmix, cyl->end.mbar / 1000.0) - < (float) cyl->deco_gas_used.mliter) + if (cyl->end.mbar / 1000.0 * cyl->type.size.mliter / gas_compressibility_factor(&cyl->gasmix, cyl->end.mbar / 1000.0) + < cyl->deco_gas_used.mliter) snprintf(warning, sizeof(warning), "
 — %s %s", translate("gettextFromC", "Warning:"), translate("gettextFromC", "not enough reserve for gas sharing on ascent!")); diff --git a/desktop-widgets/divelogexportdialog.cpp b/desktop-widgets/divelogexportdialog.cpp index cb1745483..2af8d1b80 100644 --- a/desktop-widgets/divelogexportdialog.cpp +++ b/desktop-widgets/divelogexportdialog.cpp @@ -310,7 +310,7 @@ void DiveLogExportDialog::export_TeX(const char *filename, const bool selected_o int i; int qty_cyl; int qty_weight; - float total_weight; + double total_weight; if (need_pagebreak) put_format(&buf, "\\vfill\\eject\n"); diff --git a/desktop-widgets/preferences/abstractpreferenceswidget.cpp b/desktop-widgets/preferences/abstractpreferenceswidget.cpp index 1b2aeaae5..88228ee98 100644 --- a/desktop-widgets/preferences/abstractpreferenceswidget.cpp +++ b/desktop-widgets/preferences/abstractpreferenceswidget.cpp @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include "abstractpreferenceswidget.h" -AbstractPreferencesWidget::AbstractPreferencesWidget(const QString& name, const QIcon& icon, float positionHeight) +AbstractPreferencesWidget::AbstractPreferencesWidget(const QString& name, const QIcon& icon, double positionHeight) : QWidget(), _icon(icon), _name(name), _positionHeight(positionHeight) { } @@ -16,7 +16,7 @@ QString AbstractPreferencesWidget::name() const return _name; } -float AbstractPreferencesWidget::positionHeight() const +double AbstractPreferencesWidget::positionHeight() const { return _positionHeight; } diff --git a/desktop-widgets/preferences/abstractpreferenceswidget.h b/desktop-widgets/preferences/abstractpreferenceswidget.h index 7e303d182..7466fda16 100644 --- a/desktop-widgets/preferences/abstractpreferenceswidget.h +++ b/desktop-widgets/preferences/abstractpreferenceswidget.h @@ -8,10 +8,10 @@ class AbstractPreferencesWidget : public QWidget { Q_OBJECT public: - AbstractPreferencesWidget(const QString& name, const QIcon& icon, float positionHeight); + AbstractPreferencesWidget(const QString& name, const QIcon& icon, double positionHeight); QIcon icon() const; QString name() const; - float positionHeight() const; + double positionHeight() const; /* gets the values from the preferences and should set the correct values in * the interface */ @@ -26,6 +26,6 @@ signals: private: QIcon _icon; QString _name; - float _positionHeight; + double _positionHeight; }; #endif diff --git a/subsurface-desktop-main.cpp b/subsurface-desktop-main.cpp index 5b8424487..c439179c8 100644 --- a/subsurface-desktop-main.cpp +++ b/subsurface-desktop-main.cpp @@ -152,7 +152,6 @@ void validateGL() QOffscreenSurface surface; QOpenGLFunctions *func; const char *verChar; - float verFloat; surface.setFormat(ctx.format()); surface.create(); @@ -182,9 +181,10 @@ void validateGL() "before running Subsurface!\n").toUtf8().data(); return; } - if (sscanf(verChar, "%f", &verFloat) == 1) { - verMajor = (GLint)verFloat; - verMinor = (GLint)roundf((verFloat - verMajor) * 10.f); + int min, maj; + if (sscanf(verChar, "%d.%d", &maj, &min) == 2) { + verMajor = (GLint)maj; + verMinor = (GLint)min; } } // attempt to detect version using the newer API