From 969011f8885d284fd72a613f32279f4f5ef4efe6 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Sun, 20 Nov 2022 07:47:47 -0700 Subject: [PATCH] tidy bugprone-incorrect-roundings manual fix, passing rounding off to Qt. Qt may do something smarter, or it may do just what si_round used to do. --- defs.h | 7 ------- garmin_txt.cc | 12 ++++++------ gtrnctr.cc | 6 +++--- humminbird.cc | 15 ++++++++------- kml.cc | 4 ++-- util.cc | 10 ---------- xcsv.cc | 4 ++-- 7 files changed, 21 insertions(+), 37 deletions(-) diff --git a/defs.h b/defs.h index 4dfd4830a..d59ba99bf 100644 --- a/defs.h +++ b/defs.h @@ -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. */ diff --git a/garmin_txt.cc b/garmin_txt.cc index e0eda354e..de62974eb 100644 --- a/garmin_txt.cc +++ b/garmin_txt.cc @@ -40,7 +40,7 @@ #include // for QTextStream #include // for QVector #include // for CaseInsensitive -#include // for qPrintable +#include // for qRound, qPrintable #include "csv_util.h" // for csv_linesplit #include "formspec.h" // for FormatSpecificDataList @@ -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); } } @@ -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 { @@ -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)); } } } @@ -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); diff --git a/gtrnctr.cc b/gtrnctr.cc index e7c404e73..81f1dcee6 100644 --- a/gtrnctr.cc +++ b/gtrnctr.cc @@ -28,7 +28,7 @@ #include // for QByteArray #include // for QDateTime -#include // for qPrintable +#include // for qRound, qPrintable #include // for QXmlStreamAttributes #include // for va_end, va_list, va_start @@ -242,7 +242,7 @@ GtrnctrFormat::gtc_fake_hdr(const computed_trkdata& tdata) } if (tdata.avg_hrt) { gtc_write_xml(1, "\n"); - gtc_write_xml(0, "%d\n", (int)(*tdata.avg_hrt + 0.5)); + gtc_write_xml(0, "%d\n", qRound(*tdata.avg_hrt)); gtc_write_xml(-1,"\n"); } if (tdata.max_hrt) { @@ -252,7 +252,7 @@ GtrnctrFormat::gtc_fake_hdr(const computed_trkdata& tdata) } gtc_write_xml(0, "Active\n"); if (tdata.avg_cad) { - gtc_write_xml(0, "%d\n", (int)(*tdata.avg_cad + 0.5)); + gtc_write_xml(0, "%d\n", qRound(*tdata.avg_cad)); } if (!gtc_course_flag) { /* activity (history) format */ diff --git a/humminbird.cc b/humminbird.cc index baadd27e8..3f5979105 100644 --- a/humminbird.cc +++ b/humminbird.cc @@ -23,12 +23,13 @@ #include // for QMap #include // for CaseInsensitive +#include // for qRound #include // for atan, tan, M_PI, log, sinh #include // for snprintf, SEEK_SET #include // 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 @@ -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) @@ -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(); @@ -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); diff --git a/kml.cc b/kml.cc index 0dd315c3f..548b63f85 100644 --- a/kml.cc +++ b/kml.cc @@ -40,7 +40,7 @@ #include // for QVector #include // for QXmlStreamAttributes #include // for ISODate -#include // for foreach, qint64, qPrintable +#include // for foreach, qint64, qRound, qPrintable #include "defs.h" #include "kml.h" @@ -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 { diff --git a/util.cc b/util.cc index b1337b327..b343a0dda 100644 --- a/util.cc +++ b/util.cc @@ -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 diff --git a/xcsv.cc b/xcsv.cc index 44e9cfe78..c4f21ca94 100644 --- a/xcsv.cc +++ b/xcsv.cc @@ -44,7 +44,7 @@ #include // for QString, operator+, operator== #include // for QStringList #include // for QTextStream -#include // for qAsConst, qPrintable +#include // 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 @@ -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: {