math::iround

Signed-off-by: Alexander Borsuk <me@alex.bio>
This commit is contained in:
Alexander Borsuk
2025-08-14 22:54:56 +03:00
committed by Konstantin Pastbin
parent 7781528263
commit 836c39ff64
8 changed files with 42 additions and 20 deletions

View File

@@ -209,4 +209,20 @@ UNIT_TEST(is_finite)
TEST(is_finite(DBL_MIN / 2.0), ("As in cppreference example"));
}
UNIT_TEST(iround)
{
TEST_EQUAL(iround(0.0), 0, ());
TEST_EQUAL(iround(1.0), 1, ());
TEST_EQUAL(iround(1.5), 2, ());
TEST_EQUAL(iround(-1.5), -2, ());
TEST_EQUAL(iround(2.5), 3, ());
TEST_EQUAL(iround(-2.5), -3, ());
TEST_EQUAL(iround(2.5f), 3, ());
TEST_EQUAL(iround(-2.5f), -3, ());
TEST_EQUAL(iround(double(std::numeric_limits<int>::max()) - 0.5), std::numeric_limits<int>::max(), ());
TEST_EQUAL(iround(double(std::numeric_limits<int>::min()) + 0.5), std::numeric_limits<int>::min(), ());
}
} // namespace math_test

View File

@@ -1,9 +1,11 @@
#pragma once
#include "base/assert.hpp"
#include "base/checked_cast.hpp"
#include <algorithm> // std::max
#include <cmath>
#include <concepts>
#include <functional> // std::hash
#include <type_traits>
@@ -18,6 +20,12 @@ double Nan();
double Infinity();
bool is_finite(double d);
template <std::floating_point T>
int iround(T x)
{
return base::checked_cast<int>(std::lround(x));
}
template <typename T>
T Abs(T x)
{

View File

@@ -12,6 +12,7 @@
#include "indexer/scales.hpp"
#include "base/logging.hpp"
#include "base/math.hpp"
#include <algorithm>
#include <array>
@@ -306,7 +307,7 @@ bool TrafficRenderer::CanBeRenderedAsLine(RoadClass const & roadClass, int zoomL
if (it == lineDrawerEnd)
return false;
width = std::max(1, static_cast<int>(std::lround(TrafficRenderer::GetPixelWidthInternal(roadClass, zoomLevel))));
width = std::max(1, math::iround(TrafficRenderer::GetPixelWidthInternal(roadClass, zoomLevel)));
return width <= dp::SupportManager::Instance().GetMaxLineWidth();
}
} // namespace df

View File

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

View File

@@ -1,9 +1,9 @@
#include "ge0/url_generator.hpp"
#include "base/assert.hpp"
#include "base/math.hpp"
#include "coding/url.hpp"
#include <cmath>
#include <iomanip>
#include <sstream>
@@ -158,7 +158,7 @@ int LatToInt(double lat, int maxValue)
// 000111111222222...LLLLLMMMM
double const x = (lat + 90.0) / 180.0 * maxValue;
return x < 0 ? 0 : (x > maxValue ? maxValue : static_cast<int>(std::lround(x)));
return x < 0 ? 0 : (x > maxValue ? maxValue : math::iround(x));
}
// Make lon in [-180, 180)

View File

@@ -1,12 +1,10 @@
#include "geometry/geometry_tests/equality.hpp"
#include "base/math.hpp"
#include "geometry/screenbase.hpp"
#include "geometry/transformations.hpp"
#include "testing/testing.hpp"
#include <cmath>
namespace screen_test
{
using test::is_equal;
@@ -23,10 +21,10 @@ static void check_set_from_rect(ScreenBase & screen, int width, int height)
b2 = screen.GtoP(b2);
// check that we are in boundaries.
TEST(math::Between(0, width, static_cast<int>(std::lround(b1.x))), ());
TEST(math::Between(0, width, static_cast<int>(std::lround(b2.x))), ());
TEST(math::Between(0, height, static_cast<int>(std::lround(b1.y))), ());
TEST(math::Between(0, height, static_cast<int>(std::lround(b2.y))), ());
TEST(math::Between(0, width, math::iround(b1.x)), ());
TEST(math::Between(0, width, math::iround(b2.x)), ());
TEST(math::Between(0, height, math::iround(b1.y)), ());
TEST(math::Between(0, height, math::iround(b2.y)), ());
}
UNIT_TEST(ScreenBase_P2G2P)

View File

@@ -3,8 +3,7 @@
#include "geometry/transformations.hpp"
#include "base/assert.hpp"
#include <cmath>
#include "base/math.hpp"
double constexpr kPerspectiveAngleFOV = math::pi / 3.0;
double constexpr kMaxPerspectiveAngle1 = math::pi4;
@@ -230,12 +229,12 @@ void ScreenBase::SetAngle(double angle)
int ScreenBase::GetWidth() const
{
return static_cast<int>(std::lround(m_PixelRect.SizeX()));
return math::iround(m_PixelRect.SizeX());
}
int ScreenBase::GetHeight() const
{
return static_cast<int>(std::lround(m_PixelRect.SizeY()));
return math::iround(m_PixelRect.SizeY());
}
ScreenBase::MatrixT ScreenBase::CalcTransform(m2::PointD const & oldPt1, m2::PointD const & oldPt2,

View File

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