Removed SignedRound and replaced std::round with std::lround where needed

Also see https://clang.llvm.org/extra/clang-tidy/checks/bugprone/incorrect-roundings.html

Signed-off-by: Alexander Borsuk <me@alex.bio>
This commit is contained in:
Alexander Borsuk
2025-07-06 01:25:08 +02:00
committed by Konstantin Pastbin
parent ae349462c6
commit 76d7ef146c
22 changed files with 67 additions and 60 deletions

View File

@@ -104,12 +104,6 @@ bool Between(T const a, T const b, T const x)
return a <= x && x <= b; return a <= x && x <= b;
} }
// This function is deprecated. Use std::round instead.
inline int SignedRound(double x)
{
return x > 0.0 ? static_cast<int>(x + 0.5) : static_cast<int>(x - 0.5);
}
// Computes x^n. // Computes x^n.
template <typename T> template <typename T>
T PowUint(T x, uint64_t n) T PowUint(T x, uint64_t n)

View File

@@ -5,6 +5,7 @@
#include "base/logging.hpp" #include "base/logging.hpp"
#include "base/shared_buffer_manager.hpp" #include "base/shared_buffer_manager.hpp"
#include <cmath>
#include <cstring> #include <cstring>
#include <iomanip> #include <iomanip>
#include <numeric> #include <numeric>
@@ -122,7 +123,7 @@ void StipplePenRasterizator::RasterizeTriangle(uint8_t * pixels) const
while (trgH > 0) while (trgH > 0)
{ {
uint8_t const base = std::round(trgH * tan); uint8_t const base = std::lround(trgH * tan);
uint32_t const left = (m_patternLength - base) / 2; uint32_t const left = (m_patternLength - base) / 2;
memset(pixels + 1, 0, left); memset(pixels + 1, 0, left);
memset(pixels + left + 1, 255, base); memset(pixels + left + 1, 255, base);

View File

@@ -9,6 +9,7 @@
#include "geometry/point2d.hpp" #include "geometry/point2d.hpp"
#include "geometry/rect2d.hpp" #include "geometry/rect2d.hpp"
#include <cmath>
#include <map> #include <map>
#include <mutex> #include <mutex>
@@ -22,7 +23,7 @@ using PenPatternT = buffer_vector<uint16_t, 2>;
inline uint16_t PatternFloat2Pixel(double d) inline uint16_t PatternFloat2Pixel(double d)
{ {
auto const px = static_cast<uint16_t>(std::round(d)); uint16_t const px = std::lround(d);
//ASSERT(px > 0, (d)); //ASSERT(px > 0, (d));
return px > 0 ? px : 1; return px > 0 ? px : 1;
} }

View File

@@ -10,6 +10,7 @@
#include "base/macros.hpp" #include "base/macros.hpp"
#include <algorithm> #include <algorithm>
#include <cmath>
#include <iterator> #include <iterator>
#include <limits> #include <limits>
#include <set> #include <set>
@@ -113,7 +114,7 @@ RulerHelper::RulerHelper()
void RulerHelper::Update(ScreenBase const & screen) void RulerHelper::Update(ScreenBase const & screen)
{ {
m2::PointD pivot = screen.PixelRect().Center(); m2::PointD pivot = screen.PixelRect().Center();
int const minPxWidth = math::SignedRound(kMinPixelWidth * df::VisualParams::Instance().GetVisualScale()); int const minPxWidth = std::lround(kMinPixelWidth * df::VisualParams::Instance().GetVisualScale());
m2::PointD pt1 = screen.PtoG(pivot); m2::PointD pt1 = screen.PtoG(pivot);
m2::PointD pt0 = screen.PtoG(pivot - m2::PointD(minPxWidth, 0)); m2::PointD pt0 = screen.PtoG(pivot - m2::PointD(minPxWidth, 0));
@@ -133,7 +134,7 @@ void RulerHelper::Update(ScreenBase const & screen)
double const a = ang::AngleTo(pt1, pt0); double const a = ang::AngleTo(pt1, pt0);
pt0 = mercator::GetSmPoint(pt1, cos(a) * metersDiff, sin(a) * metersDiff); pt0 = mercator::GetSmPoint(pt1, cos(a) * metersDiff, sin(a) * metersDiff);
m_pixelLength = math::SignedRound(pivot.Length(screen.GtoP(pt0))); m_pixelLength = std::round(pivot.Length(screen.GtoP(pt0)));
} }
int drawScale = df::GetDrawTileScale(screen); int drawScale = df::GetDrawTileScale(screen);

View File

@@ -15,6 +15,7 @@
#include <array> #include <array>
#include <algorithm> #include <algorithm>
#include <cmath>
#include <utility> #include <utility>
namespace df namespace df
@@ -323,7 +324,7 @@ bool TrafficRenderer::CanBeRenderedAsLine(RoadClass const & roadClass, int zoomL
if (it == lineDrawerEnd) if (it == lineDrawerEnd)
return false; return false;
width = std::max(1, math::SignedRound(TrafficRenderer::GetPixelWidthInternal(roadClass, zoomLevel))); width = std::max(1l, std::lround(TrafficRenderer::GetPixelWidthInternal(roadClass, zoomLevel)));
return width <= dp::SupportManager::Instance().GetMaxLineWidth(); return width <= dp::SupportManager::Instance().GetMaxLineWidth();
} }
} // namespace df } // namespace df

View File

@@ -186,7 +186,7 @@ int GetTileScaleBase(m2::RectD const & r)
{ {
double const sz = std::max(r.SizeX(), r.SizeY()); double const sz = std::max(r.SizeX(), r.SizeY());
ASSERT_GREATER(sz, 0., ("Rect should not be a point:", r)); ASSERT_GREATER(sz, 0., ("Rect should not be a point:", r));
return std::max(1, math::SignedRound(std::log2(mercator::Bounds::kRangeX / sz))); return std::max(1l, std::lround(std::log2(mercator::Bounds::kRangeX / sz)));
} }
double GetTileScaleBase(double drawScale) double GetTileScaleBase(double drawScale)
@@ -224,12 +224,12 @@ m2::RectD GetRectForDrawScale(int drawScale, m2::PointD const & center)
m2::RectD GetRectForDrawScale(double drawScale, m2::PointD const & center, uint32_t tileSize, double visualScale) m2::RectD GetRectForDrawScale(double drawScale, m2::PointD const & center, uint32_t tileSize, double visualScale)
{ {
return GetRectForDrawScale(math::SignedRound(drawScale), center, tileSize, visualScale); return GetRectForDrawScale(static_cast<int>(std::lround(drawScale)), center, tileSize, visualScale);
} }
m2::RectD GetRectForDrawScale(double drawScale, m2::PointD const & center) m2::RectD GetRectForDrawScale(double drawScale, m2::PointD const & center)
{ {
return GetRectForDrawScale(math::SignedRound(drawScale), center); return GetRectForDrawScale(static_cast<int>(std::lround(drawScale)), center);
} }
uint32_t CalculateTileSize(uint32_t screenWidth, uint32_t screenHeight) uint32_t CalculateTileSize(uint32_t screenWidth, uint32_t screenHeight)

View File

@@ -133,7 +133,7 @@ int LatToInt(double lat, int maxValue)
// 000111111222222...LLLLLMMMM // 000111111222222...LLLLLMMMM
double const x = (lat + 90.0) / 180.0 * maxValue; double const x = (lat + 90.0) / 180.0 * maxValue;
return x < 0 ? 0 : (x > maxValue ? maxValue : static_cast<int>(x + 0.5)); return x < 0 ? 0 : (x > maxValue ? maxValue : std::lround(x));
} }
// Make lon in [-180, 180) // Make lon in [-180, 180)

View File

@@ -4,6 +4,7 @@
#include "base/stl_helpers.hpp" #include "base/stl_helpers.hpp"
#include <algorithm> #include <algorithm>
#include <cmath>
namespace namespace
{ {
@@ -42,8 +43,8 @@ CellsMerger::CellsMerger(std::vector<m2::RectD> && cells)
auto const sideSize = cells.front().SizeX(); auto const sideSize = cells.front().SizeX();
for (auto const & cell : cells) for (auto const & cell : cells)
{ {
auto const xIdx = static_cast<int32_t>(std::round((cell.minX() - minX) / sideSize)); int32_t const xIdx = std::lround((cell.minX() - minX) / sideSize);
auto const yIdx = static_cast<int32_t>(std::round((cell.minY() - minY) / sideSize)); int32_t const yIdx = std::lround((cell.minY() - minY) / sideSize);
m_matrix.emplace(m2::PointI{xIdx, yIdx}, cell); m_matrix.emplace(m2::PointI{xIdx, yIdx}, cell);
m_maxX = std::max(m_maxX, xIdx); m_maxX = std::max(m_maxX, xIdx);
m_maxY = std::max(m_maxY, yIdx); m_maxY = std::max(m_maxY, yIdx);

View File

@@ -24,6 +24,7 @@
#include "base/string_utils.hpp" #include "base/string_utils.hpp"
#include <algorithm> #include <algorithm>
#include <cmath>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <utility> #include <utility>

View File

@@ -8,6 +8,7 @@
#include "base/file_name_utils.hpp" #include "base/file_name_utils.hpp"
#include "base/logging.hpp" #include "base/logging.hpp"
#include <cmath>
#include <fstream> #include <fstream>
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
@@ -125,7 +126,7 @@ geometry::Altitude SrtmTile::GetHeight(ms::LatLon const & coord) const
auto const ll = GetCoordInSeconds(coord); auto const ll = GetCoordInSeconds(coord);
return GetHeightRC(static_cast<size_t>(std::round(ll.m_lat)), static_cast<size_t>(std::round(ll.m_lon))); return GetHeightRC(std::lround(ll.m_lat), std::lround(ll.m_lon));
} }
geometry::Altitude SrtmTile::GetHeightRC(size_t row, size_t col) const geometry::Altitude SrtmTile::GetHeightRC(size_t row, size_t col) const
@@ -142,7 +143,7 @@ double SrtmTile::GetTriangleHeight(ms::LatLon const & coord) const
auto const ll = GetCoordInSeconds(coord); auto const ll = GetCoordInSeconds(coord);
m2::Point<int> const p1(static_cast<int>(std::round(ll.m_lon)), static_cast<int>(std::round(ll.m_lat))); m2::Point<int> const p1(std::lround(ll.m_lon), std::lround(ll.m_lat));
auto p2 = p1; auto p2 = p1;
if (p2.x > ll.m_lon) if (p2.x > ll.m_lon)

View File

@@ -5,13 +5,14 @@
#include "testing/testing.hpp" #include "testing/testing.hpp"
#include <cmath>
using namespace test; namespace screen_test
{
namespace using test::is_equal;
static void check_set_from_rect(ScreenBase & screen, int width, int height)
{ {
void check_set_from_rect(ScreenBase & screen, int width, int height)
{
screen.OnSize(0, 0, width, height); screen.OnSize(0, 0, width, height);
m2::PointD b1(0.0, 0.0); m2::PointD b1(0.0, 0.0);
@@ -22,11 +23,10 @@ namespace
b2 = screen.GtoP(b2); b2 = screen.GtoP(b2);
// check that we are in boundaries. // check that we are in boundaries.
TEST(math::Between(0, width, math::SignedRound(b1.x)), ()); TEST(math::Between(0, width, static_cast<int>(std::lround(b1.x))), ());
TEST(math::Between(0, width, math::SignedRound(b2.x)), ()); TEST(math::Between(0, width, static_cast<int>(std::lround(b2.x))), ());
TEST(math::Between(0, height, math::SignedRound(b1.y)), ()); TEST(math::Between(0, height, static_cast<int>(std::lround(b1.y))), ());
TEST(math::Between(0, height, math::SignedRound(b2.y)), ()); TEST(math::Between(0, height, static_cast<int>(std::lround(b2.y))), ());
}
} }
UNIT_TEST(ScreenBase_P2G2P) UNIT_TEST(ScreenBase_P2G2P)
@@ -221,3 +221,4 @@ UNIT_TEST(ScreenBase_CombineTransforms)
TEST(is_equal(p1, pp1), (p1, pp1)); TEST(is_equal(p1, pp1), (p1, pp1));
TEST(is_equal(p2, pp2), (p2, pp2)); TEST(is_equal(p2, pp2), (p2, pp2));
} }
} // namespace screen_test

View File

@@ -218,9 +218,9 @@ void ScreenBase::SetAngle(double angle)
UpdateDependentParameters(); UpdateDependentParameters();
} }
int ScreenBase::GetWidth() const { return math::SignedRound(m_PixelRect.SizeX()); } int ScreenBase::GetWidth() const { return std::lround(m_PixelRect.SizeX()); }
int ScreenBase::GetHeight() const { return math::SignedRound(m_PixelRect.SizeY()); } int ScreenBase::GetHeight() const { return std::lround(m_PixelRect.SizeY()); }
ScreenBase::MatrixT ScreenBase::CalcTransform(m2::PointD const & oldPt1, ScreenBase::MatrixT ScreenBase::CalcTransform(m2::PointD const & oldPt1,
m2::PointD const & oldPt2, m2::PointD const & oldPt2,

View File

@@ -1,18 +1,19 @@
#include "indexer/feature_impl.hpp" #include "indexer/feature_impl.hpp"
#include "base/math.hpp" #include <algorithm>
#include <cmath>
namespace feature namespace feature
{ {
uint8_t PopulationToRank(uint64_t p) uint8_t PopulationToRank(uint64_t p)
{ {
return static_cast<uint8_t>(std::min(0xFF, math::SignedRound(log(double(p)) / log(1.1)))); return std::min(0xFFl, std::lround(std::log(double(p)) / std::log(1.1)));
} }
uint64_t RankToPopulation(uint8_t r) uint64_t RankToPopulation(uint8_t r)
{ {
return static_cast<uint64_t>(pow(1.1, r)); return static_cast<uint64_t>(std::pow(1.1, r));
} }
} // namespace feature } // namespace feature

View File

@@ -930,7 +930,7 @@ double GetRadiusByPopulationForRouting(uint64_t p, LocalityType localityType)
uint64_t GetPopulationByRadius(double r) uint64_t GetPopulationByRadius(double r)
{ {
return math::SignedRound(pow(r / 550.0, 3.6)); return std::lround(pow(r / 550.0, 3.6));
} }
} // namespace ftypes } // namespace ftypes

View File

@@ -2,9 +2,9 @@
#include "indexer/feature_algo.hpp" #include "indexer/feature_algo.hpp"
#include "geometry/mercator.hpp" #include "geometry/mercator.hpp"
#include "base/math.hpp"
#include <algorithm> #include <algorithm>
#include <cmath>
namespace scales namespace scales
{ {
@@ -33,12 +33,12 @@ namespace scales
int GetScaleLevel(double ratio) int GetScaleLevel(double ratio)
{ {
return math::SignedRound(GetScaleLevelD(ratio)); return std::lround(GetScaleLevelD(ratio));
} }
int GetScaleLevel(m2::RectD const & r) int GetScaleLevel(m2::RectD const & r)
{ {
return math::SignedRound(GetScaleLevelD(r)); return std::lround(GetScaleLevelD(r));
} }
namespace namespace

View File

@@ -1644,12 +1644,12 @@ UNIT_CLASS_TEST(Runner, Bookmarks_TestSaveRoute)
{ {
BookmarkManager bmManager(BM_CALLBACKS); BookmarkManager bmManager(BM_CALLBACKS);
bmManager.EnableTestMode(true); bmManager.EnableTestMode(true);
auto const points = {geometry::PointWithAltitude({0.0, 0.0}, 0.0), geometry::PointWithAltitude({0.001, 0.001}, 0.001)}; auto const points = {geometry::PointWithAltitude({0.0, 0.0}, 0.0), geometry::PointWithAltitude({0.001, 0.001}, 0)};
auto const trackId = bmManager.SaveRoute(points, "London", "Paris"); auto const trackId = bmManager.SaveRoute(points, "London", "Paris");
auto const * track = bmManager.GetTrack(trackId); auto const * track = bmManager.GetTrack(trackId);
TEST_EQUAL(track->GetName(), "London - Paris", ()); TEST_EQUAL(track->GetName(), "London - Paris", ());
auto const line = track->GetData().m_geometry.m_lines[0]; auto const line = track->GetData().m_geometry.m_lines[0];
std::vector const expectedLine = {{geometry::PointWithAltitude(m2::PointD(0.0, 0.0), 0.0), geometry::PointWithAltitude(m2::PointD(0.001, 0.001), 0.001)}}; std::vector const expectedLine = {{geometry::PointWithAltitude(m2::PointD(0.0, 0.0), 0.0), geometry::PointWithAltitude(m2::PointD(0.001, 0.001), 0)}};
TEST_EQUAL(line, expectedLine, ()); TEST_EQUAL(line, expectedLine, ());
} }

View File

@@ -6,6 +6,8 @@
#include "base/assert.hpp" #include "base/assert.hpp"
#include <cmath>
namespace platform namespace platform
{ {
using namespace measurement_utils; using namespace measurement_utils;

View File

@@ -113,7 +113,7 @@ std::string FormatLatLonAsDMSImpl(double value, char positive, char negative, in
// Seconds // Seconds
d = d * 60.0; d = d * 60.0;
if (dac == 0) if (dac == 0)
d = math::SignedRound(d); d = std::round(d);
d = std::modf(d, &i); d = std::modf(d, &i);
sstream << std::setw(2) << i; sstream << std::setw(2) << i;
@@ -124,7 +124,7 @@ std::string FormatLatLonAsDMSImpl(double value, char positive, char negative, in
sstream << ""; sstream << "";
// This condition is too heavy for production purposes (but more correct). // This condition is too heavy for production purposes (but more correct).
//if (math::SignedRound(value * 3600.0 * pow(10, dac)) != 0) //if (std::round(value * 3600.0 * pow(10, dac)) != 0)
if (!AlmostEqualULPs(value, 0.0)) if (!AlmostEqualULPs(value, 0.0))
{ {
char postfix = positive; char postfix = positive;
@@ -200,7 +200,7 @@ double MpsToUnits(double metersPerSecond, Units units)
int FormatSpeed(double metersPerSecond, Units units) int FormatSpeed(double metersPerSecond, Units units)
{ {
return static_cast<int>(std::round(MpsToUnits(metersPerSecond, units))); return std::lround(MpsToUnits(metersPerSecond, units));
} }
std::string FormatSpeedNumeric(double metersPerSecond, Units units) std::string FormatSpeedNumeric(double metersPerSecond, Units units)
@@ -214,8 +214,8 @@ std::string FormatOsmLink(double lat, double lon, int zoom)
// Same as (lon + 180) / 360 * 1UL << 32, but without warnings. // Same as (lon + 180) / 360 * 1UL << 32, but without warnings.
double constexpr factor = (1 << 30) / 90.0; double constexpr factor = (1 << 30) / 90.0;
uint32_t const x = round((lon + 180.0) * factor); uint32_t const x = std::lround((lon + 180.0) * factor);
uint32_t const y = round((lat + 90.0) * factor * 2.0); uint32_t const y = std::lround((lat + 90.0) * factor * 2.0);
uint64_t const code = bits::BitwiseMerge(y, x); uint64_t const code = bits::BitwiseMerge(y, x);
std::string osmUrl = "https://osm.org/go/"; std::string osmUrl = "https://osm.org/go/";

View File

@@ -152,8 +152,8 @@ std::string UTMtoStr(UTMPoint const & point)
// last 5 digits of UTM and MGRS coordinates could differ (inaccuracy is no more then 1 meter). // last 5 digits of UTM and MGRS coordinates could differ (inaccuracy is no more then 1 meter).
// Some UTM converters truncate easting and northing instead of rounding. Consider this option. // Some UTM converters truncate easting and northing instead of rounding. Consider this option.
return std::to_string(point.zoneNumber) + point.zoneLetter + ' ' + return std::to_string(point.zoneNumber) + point.zoneLetter + ' ' +
std::to_string(static_cast<int>(std::round(point.easting))) + ' ' + std::to_string(std::lround(point.easting)) + ' ' +
std::to_string(static_cast<int>(std::round(point.northing))); std::to_string(std::lround(point.northing));
} }
// Build 2 chars string with MGRS 100k designator. // Build 2 chars string with MGRS 100k designator.

View File

@@ -9,6 +9,7 @@
#include <QtWidgets/QToolBar> #include <QtWidgets/QToolBar>
#include <cmath>
#include <memory> #include <memory>
namespace qt namespace qt
@@ -67,7 +68,7 @@ void ScaleSlider::SetPosWithBlockedSignals(double pos)
{ {
bool const blocked = signalsBlocked(); bool const blocked = signalsBlocked();
blockSignals(true); blockSignals(true);
setSliderPosition(math::SignedRound(pos * m_factor)); setSliderPosition(std::lround(pos * m_factor));
blockSignals(blocked); blockSignals(blocked);
} }

View File

@@ -141,7 +141,7 @@ void CrossBorderGraphSerializer::Serialize(CrossBorderGraph const & graph, Sink
{ {
WriteVarUint(sink, segId); WriteVarUint(sink, segId);
WriteVarUint(sink, static_cast<uint64_t>(std::round(seg.m_weight * kDouble2Int))); WriteVarUint(sink, static_cast<uint64_t>(std::lround(seg.m_weight * kDouble2Int)));
writeSegEnding(seg.m_start); writeSegEnding(seg.m_start);
writeSegEnding(seg.m_end); writeSegEnding(seg.m_end);

View File

@@ -12,6 +12,7 @@
#include "base/thread_pool_computational.hpp" #include "base/thread_pool_computational.hpp"
#include <algorithm> #include <algorithm>
#include <cmath>
#include <fstream> #include <fstream>
#include <set> #include <set>
#include <vector> #include <vector>
@@ -188,8 +189,8 @@ public:
double lt = pos.m_lat - m_bottomLat; double lt = pos.m_lat - m_bottomLat;
lt = 1 - lt; // From North to South, the same direction as inside the SRTM tiles. lt = 1 - lt; // From North to South, the same direction as inside the SRTM tiles.
auto const row = static_cast<size_t>(std::round(kArcSecondsInDegree * lt)); auto const row = std::lround(kArcSecondsInDegree * lt);
auto const col = static_cast<size_t>(std::round(kArcSecondsInDegree * ln)); auto const col = std::lround(kArcSecondsInDegree * ln);
auto const ix = row * (kArcSecondsInDegree + 1) + col; auto const ix = row * (kArcSecondsInDegree + 1) + col;
CHECK(ix < m_values.size(), (pos)); CHECK(ix < m_values.size(), (pos));