Skip to content
Permalink
Browse files

[BUGFIX] fix qgsRound for negative numbers. Fixes #20861

(cherry picked from commit cfdc8c2)
  • Loading branch information
lbartoletti authored and nyalldawson committed Jan 29, 2019
1 parent 3462511 commit 3f0171b8bfa3e7de5a852ea9c57935541e15edcf
Showing with 6 additions and 3 deletions.
  1. +2 −1 src/core/qgis.h
  2. +4 −2 tests/src/core/testqgis.cpp
@@ -316,8 +316,9 @@ inline bool qgsDoubleNearSig( double a, double b, int significantDigits = 10 )
*/
inline double qgsRound( double number, double places )
{
double m = ( number < 0.0 ) ? -1.0 : 1.0;
double scaleFactor = std::pow( 10.0, places );
return std::trunc( number * scaleFactor + 0.5 ) / scaleFactor;
return ( std::round( number * m * scaleFactor ) / scaleFactor ) * m;
}


@@ -339,6 +339,8 @@ void TestQgis::testQgsAsConst()

void TestQgis::testQgsRound()
{
QGSCOMPARENEAR( qgsRound( 1234.567, 2 ), 1234.57, 0.01 );
QGSCOMPARENEAR( qgsRound( -1234.567, 2 ), -1234.57, 0.01 );
QGSCOMPARENEAR( qgsRound( 98765432198, 8 ), 98765432198, 1.0 );
QGSCOMPARENEAR( qgsRound( 98765432198, 9 ), 98765432198, 1.0 );
QGSCOMPARENEAR( qgsRound( 98765432198, 10 ), 98765432198, 1.0 );
@@ -354,9 +356,9 @@ void TestQgis::testQgsRound()
QGSCOMPARENEAR( qgsRound( 9.8765432198765, 5 ), 9.87654, 0.000001 );
QGSCOMPARENEAR( qgsRound( 9.8765432198765, 6 ), 9.876543, 0.0000001 );
QGSCOMPARENEAR( qgsRound( 9.8765432198765, 7 ), 9.8765432, 0.00000001 );
QGSCOMPARENEAR( qgsRound( -9.8765432198765, 7 ), -9.876543, 0.000001 );
QGSCOMPARENEAR( qgsRound( -9.8765432198765, 7 ), -9.8765432, 0.0000001 );
QGSCOMPARENEAR( qgsRound( 9876543.2198765, 5 ), 9876543.219880, 0.000001 );
QGSCOMPARENEAR( qgsRound( -9876543.2198765, 5 ), -9876543.219870, 0.000001 );
QGSCOMPARENEAR( qgsRound( -9876543.2198765, 5 ), -9876543.219880, 0.000001 );
QGSCOMPARENEAR( qgsRound( 9.87654321987654321, 13 ), 9.87654321987654, 0.0000000000001 );
QGSCOMPARENEAR( qgsRound( 9.87654321987654321, 14 ), 9.876543219876543, 0.00000000000001 );
QGSCOMPARENEAR( qgsRound( 9998.87654321987654321, 14 ), 9998.876543219876543, 0.00000000000001 );

0 comments on commit 3f0171b

Please sign in to comment.
You can’t perform that action at this time.