From c57924d7eb337babb5097ba66dc07349c4923c6d Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Wed, 14 Jul 2021 13:29:46 +0200 Subject: [PATCH] qskFuzzyFloor/Ceil improved --- src/common/QskFunctions.cpp | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/common/QskFunctions.cpp b/src/common/QskFunctions.cpp index 8187adf7..77638fe0 100644 --- a/src/common/QskFunctions.cpp +++ b/src/common/QskFunctions.cpp @@ -196,25 +196,18 @@ QRect qskPlatformScreenGeometry( const QScreen* screen ) return screen->handle()->geometry(); } -/* - Due to the nature of floating point arithmetic - we might floor a value, that is already "aligned" - F.e static_cast< int >( 0.29 / 0.01 ) -> 28 - */ qreal qskFuzzyFloor( qreal value, qreal stepSize ) { - qreal v = std::floor( value / stepSize ) * stepSize; - if ( qFuzzyCompare( value - v, stepSize ) ) - v = value; + const double eps = 1.0e-6 * stepSize; - return v; + value = ( value + eps ) / stepSize; + return std::floor( value ) * stepSize; } qreal qskFuzzyCeil( qreal value, qreal stepSize ) { - qreal v = std::ceil( value / stepSize ) * stepSize; - if ( qFuzzyCompare( v - value, stepSize ) ) - v = value; + const double eps = 1.0e-6 * stepSize; - return v; + value = ( value - eps ) / stepSize; + return std::ceil( value ) * stepSize; }