Skip to content

Commit

Permalink
tidy bugprone-incorrect-roundings
Browse files Browse the repository at this point in the history
manual fix, passing rounding off to Qt.  Qt may do something
smarter, or it may do just what si_round used to do.
  • Loading branch information
tsteven4 committed Nov 20, 2022
1 parent 0c57dd6 commit 969011f
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 37 deletions.
7 changes: 0 additions & 7 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1100,13 +1100,6 @@ gpsbabel::DateTime xml_parse_time(const QString& dateTimeString);

QString rot13(const QString& s);

/*
* PalmOS records like fixed-point numbers, which should be rounded
* to deal with possible floating-point representation errors.
*/

signed int si_round(double d);

/*
* Prototypes for Endianness helpers.
*/
Expand Down
12 changes: 6 additions & 6 deletions garmin_txt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include <QTextStream> // for QTextStream
#include <QVector> // for QVector
#include <Qt> // for CaseInsensitive
#include <QtGlobal> // for qPrintable
#include <QtGlobal> // for qRound, qPrintable

#include "csv_util.h" // for csv_linesplit
#include "formspec.h" // for FormatSpecificDataList
Expand Down Expand Up @@ -423,7 +423,7 @@ static void
print_course(const Waypoint* A, const Waypoint* B) /* seems to be okay */
{
if ((A != nullptr) && (B != nullptr) && (A != B)) {
int course = si_round(waypt_course(A, B));
int course = qRound(waypt_course(A, B));
*fout << QString::asprintf("%d° true", course);
}
}
Expand All @@ -443,7 +443,7 @@ print_distance(const double distance, const int no_scale, const int with_tab, co
if (dist < 100.0) {
*fout << QString::asprintf("%.1f mi", dist);
} else {
*fout << QString::asprintf("%d mi", si_round(dist));
*fout << QString::asprintf("%d mi", qRound(dist));
}
}
} else {
Expand All @@ -454,7 +454,7 @@ print_distance(const double distance, const int no_scale, const int with_tab, co
if (dist < 100.0) {
*fout << QString::asprintf("%.1f km", dist);
} else {
*fout << QString::asprintf("%d km", si_round(dist));
*fout << QString::asprintf("%d km", qRound(dist));
}
}
}
Expand All @@ -475,11 +475,11 @@ print_speed(const double* distance, const time_t* time)
} else {
unit = "kph";
}
int idist = si_round(dist);
int idist = qRound(dist);

if ((*time != 0) && (idist > 0)) {
double speed = MPS_TO_KPH(dist / (double)*time);
int ispeed = si_round(speed);
int ispeed = qRound(speed);

if (speed < 0.01) {
*fout << QString::asprintf("0 %s", unit);
Expand Down
6 changes: 3 additions & 3 deletions gtrnctr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

#include <QByteArray> // for QByteArray
#include <QDateTime> // for QDateTime
#include <QtGlobal> // for qPrintable
#include <QtGlobal> // for qRound, qPrintable
#include <QXmlStreamAttributes> // for QXmlStreamAttributes

#include <cstdarg> // for va_end, va_list, va_start
Expand Down Expand Up @@ -242,7 +242,7 @@ GtrnctrFormat::gtc_fake_hdr(const computed_trkdata& tdata)
}
if (tdata.avg_hrt) {
gtc_write_xml(1, "<AverageHeartRateBpm xsi:type=\"HeartRateInBeatsPerMinute_t\">\n");
gtc_write_xml(0, "<Value>%d</Value>\n", (int)(*tdata.avg_hrt + 0.5));
gtc_write_xml(0, "<Value>%d</Value>\n", qRound(*tdata.avg_hrt));
gtc_write_xml(-1,"</AverageHeartRateBpm>\n");
}
if (tdata.max_hrt) {
Expand All @@ -252,7 +252,7 @@ GtrnctrFormat::gtc_fake_hdr(const computed_trkdata& tdata)
}
gtc_write_xml(0, "<Intensity>Active</Intensity>\n");
if (tdata.avg_cad) {
gtc_write_xml(0, "<Cadence>%d</Cadence>\n", (int)(*tdata.avg_cad + 0.5));
gtc_write_xml(0, "<Cadence>%d</Cadence>\n", qRound(*tdata.avg_cad));
}

if (!gtc_course_flag) { /* activity (history) format */
Expand Down
15 changes: 8 additions & 7 deletions humminbird.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@

#include <QMap> // for QMap
#include <Qt> // for CaseInsensitive
#include <QtGlobal> // for qRound

#include <cmath> // for atan, tan, M_PI, log, sinh
#include <cstdio> // for snprintf, SEEK_SET
#include <cstring> // for strncpy, memcpy, memset

#include "defs.h" // for Waypoint, be_read32, be_read16, be_write32, fatal, xfree, be_write16, route_head, si_round, xcalloc, track_add_wpt, xstrndup, mkshort, mkshort_del_handle, mkshort_new_handle, setshort_badchars, setshort_defname, setshort_length, setshort_mustuniq, setshort_...
#include "defs.h" // for Waypoint, be_read32, be_read16, be_write32, fatal, xfree, be_write16, route_head, xcalloc, track_add_wpt, xstrndup, mkshort, mkshort_del_handle, mkshort_new_handle, setshort_badchars, setshort_defname, setshort_length, setshort_mustuniq, setshort_...
#include "src/core/datetime.h" // for DateTime


Expand Down Expand Up @@ -656,17 +657,17 @@ HumminbirdFormat::humminbird_write_waypoint(const Waypoint* wpt)
}
}

hum.depth = si_round(WAYPT_GET(wpt, depth, 0)*100.0);
hum.depth = qRound(WAYPT_GET(wpt, depth, 0)*100.0);
be_write16(&hum.depth, hum.depth);

be_write32(&hum.time, wpt->GetCreationTime().toTime_t());

double east = wpt->longitude / 180.0 * EAST_SCALE;
be_write32(&hum.east, si_round((east)));
be_write32(&hum.east, qRound((east)));

double lat = geodetic_to_geocentric_hwr(wpt->latitude);
double north = inverse_gudermannian_i1924(lat);
be_write32(&hum.north, si_round(north));
be_write32(&hum.north, qRound(north));

QString name = (global_opts.synthesize_shortnames)
? mkshort_from_wpt(wptname_sh, wpt)
Expand Down Expand Up @@ -741,9 +742,9 @@ HumminbirdHTFormat::humminbird_track_cb(const Waypoint* wpt)

int i = trk_head->num_points;

int32_t east = si_round(wpt->longitude / 180.0 * EAST_SCALE);
int32_t east = qRound(wpt->longitude / 180.0 * EAST_SCALE);
double lat = geodetic_to_geocentric_hwr(wpt->latitude);
int32_t north = si_round(inverse_gudermannian_i1924(lat));
int32_t north = qRound(inverse_gudermannian_i1924(lat));

if (wpt->creation_time.isValid()) {
last_time = wpt->GetCreationTime().toTime_t();
Expand All @@ -768,7 +769,7 @@ HumminbirdHTFormat::humminbird_track_cb(const Waypoint* wpt)
int j = i-1;
trk_points[j].deltaeast = east - last_east;
trk_points[j].deltanorth = north - last_north;
trk_points[j].depth = si_round(WAYPT_GET(wpt, depth, 0)*100.0);
trk_points[j].depth = qRound(WAYPT_GET(wpt, depth, 0)*100.0);

/* BE-ify */
be_write16(&trk_points[j].deltaeast, trk_points[j].deltaeast);
Expand Down
4 changes: 2 additions & 2 deletions kml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include <QVector> // for QVector
#include <QXmlStreamAttributes> // for QXmlStreamAttributes
#include <Qt> // for ISODate
#include <QtGlobal> // for foreach, qint64, qPrintable
#include <QtGlobal> // for foreach, qint64, qRound, qPrintable

#include "defs.h"
#include "kml.h"
Expand Down Expand Up @@ -845,7 +845,7 @@ void KmlFormat::kml_output_point(const Waypoint* waypointp, kml_point_type pt_ty
value = QStringLiteral("%1-none").arg(style);
} else {
value = QStringLiteral("%1-%2").arg(style)
.arg((int)(waypointp->course / 22.5 + .5) % 16);
.arg(qRound(waypointp->course / 22.5) % 16);
}
writer->writeTextElement(QStringLiteral("styleUrl"), value);
} else {
Expand Down
10 changes: 0 additions & 10 deletions util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -607,16 +607,6 @@ le_write32(void* ptr, const unsigned value)
p[3] = value >> 24;
}

signed int
si_round(double d)
{
if (d < 0) {
return (signed int)(d-0.5);
} else {
return (signed int)(d+0.5);
}
}

/*
mkgmtime -- convert tm struct in UTC to time_t
Expand Down
4 changes: 2 additions & 2 deletions xcsv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#include <QString> // for QString, operator+, operator==
#include <QStringList> // for QStringList
#include <QTextStream> // for QTextStream
#include <QtGlobal> // for qAsConst, qPrintable
#include <QtGlobal> // for qAsConst, qRound, qPrintable

#include "defs.h"
#include "csv_util.h" // for csv_stringtrim, dec_to_human, csv_stringclean, human_to_dec, ddmmdir_to_degrees, dec_to_intdeg, decdir_to_dec, intdeg_to_dec, csv_linesplit
Expand Down Expand Up @@ -1211,7 +1211,7 @@ XcsvFormat::xcsv_waypt_pr(const Waypoint* wpt)
if (! GPS_Math_WGS84_To_UKOSMap_M(wpt->latitude, wpt->longitude, &east, &north, map))
fatal(MYNAME ": Position (%.5f/%.5f) outside of BNG.\n",
wpt->latitude, wpt->longitude);
buff = QString::asprintf(fmp.printfc.constData(), map, (int)(east + 0.5), (int)(north + 0.5));
buff = QString::asprintf(fmp.printfc.constData(), map, qRound(east), qRound(north));
}
break;
case XcsvStyle::XT_UTM: {
Expand Down

0 comments on commit 969011f

Please sign in to comment.