Skip to content

Commit

Permalink
Make qgsRound more tolerant to large values
Browse files Browse the repository at this point in the history
Fixes #19844
  • Loading branch information
nyalldawson committed Sep 18, 2018
1 parent cdd72e5 commit f9015f3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/core/qgis.h
Expand Up @@ -291,8 +291,8 @@ inline bool qgsDoubleNearSig( double a, double b, int significantDigits = 10 )
*/
inline double qgsRound( double number, double places )
{
int scaleFactor = static_cast<int>( std::pow( 10, places ) );
return static_cast<double>( static_cast<qlonglong>( number * scaleFactor + 0.5 ) ) / scaleFactor;
double scaleFactor = std::pow( 10.0, places );
return std::trunc( number * scaleFactor + 0.5 ) / scaleFactor;
}


Expand Down
25 changes: 25 additions & 0 deletions tests/src/core/testqgis.cpp
Expand Up @@ -44,6 +44,7 @@ class TestQgis : public QObject
void qVariantCompare_data();
void qVariantCompare();
void testQgsAsConst();
void testQgsRound();

private:
QString mReport;
Expand Down Expand Up @@ -332,6 +333,30 @@ void TestQgis::testQgsAsConst()
QCOMPARE( ct.mVal, 2 );
}

void TestQgis::testQgsRound()
{
QGSCOMPARENEAR( qgsRound( 98765432198, 8 ), 98765432198, 1.0 );
QGSCOMPARENEAR( qgsRound( 98765432198, 9 ), 98765432198, 1.0 );
QGSCOMPARENEAR( qgsRound( 98765432198, 10 ), 98765432198, 1.0 );
QGSCOMPARENEAR( qgsRound( 98765432198, 11 ), 98765432198, 1.0 );
QGSCOMPARENEAR( qgsRound( 98765432198, 12 ), 98765432198, 1.0 );
QGSCOMPARENEAR( qgsRound( 98765432198, 13 ), 98765432198, 1.0 );
QGSCOMPARENEAR( qgsRound( 98765432198, 14 ), 98765432198, 1.0 );
QGSCOMPARENEAR( qgsRound( 98765432198765, 14 ), 98765432198765, 1.0 );
QGSCOMPARENEAR( qgsRound( 98765432198765432, 20 ), 98765432198765432, 1.0 );
QGSCOMPARENEAR( qgsRound( 9.8765432198765, 2 ), 9.88, 0.001 );
QGSCOMPARENEAR( qgsRound( 9.8765432198765, 3 ), 9.877, 0.0001 );
QGSCOMPARENEAR( qgsRound( 9.8765432198765, 4 ), 9.8765, 0.00001 );
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( 9876543.2198765, 5 ), 9876543.219880, 0.000001 );
QGSCOMPARENEAR( qgsRound( -9876543.2198765, 5 ), -9876543.219870, 0.000001 );


}


QGSTEST_MAIN( TestQgis )
#include "testqgis.moc"

0 comments on commit f9015f3

Please sign in to comment.