Skip to content

Commit f9015f3

Browse files
committed
Make qgsRound more tolerant to large values
Fixes #19844
1 parent cdd72e5 commit f9015f3

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/core/qgis.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ inline bool qgsDoubleNearSig( double a, double b, int significantDigits = 10 )
291291
*/
292292
inline double qgsRound( double number, double places )
293293
{
294-
int scaleFactor = static_cast<int>( std::pow( 10, places ) );
295-
return static_cast<double>( static_cast<qlonglong>( number * scaleFactor + 0.5 ) ) / scaleFactor;
294+
double scaleFactor = std::pow( 10.0, places );
295+
return std::trunc( number * scaleFactor + 0.5 ) / scaleFactor;
296296
}
297297

298298

tests/src/core/testqgis.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class TestQgis : public QObject
4444
void qVariantCompare_data();
4545
void qVariantCompare();
4646
void testQgsAsConst();
47+
void testQgsRound();
4748

4849
private:
4950
QString mReport;
@@ -332,6 +333,30 @@ void TestQgis::testQgsAsConst()
332333
QCOMPARE( ct.mVal, 2 );
333334
}
334335

336+
void TestQgis::testQgsRound()
337+
{
338+
QGSCOMPARENEAR( qgsRound( 98765432198, 8 ), 98765432198, 1.0 );
339+
QGSCOMPARENEAR( qgsRound( 98765432198, 9 ), 98765432198, 1.0 );
340+
QGSCOMPARENEAR( qgsRound( 98765432198, 10 ), 98765432198, 1.0 );
341+
QGSCOMPARENEAR( qgsRound( 98765432198, 11 ), 98765432198, 1.0 );
342+
QGSCOMPARENEAR( qgsRound( 98765432198, 12 ), 98765432198, 1.0 );
343+
QGSCOMPARENEAR( qgsRound( 98765432198, 13 ), 98765432198, 1.0 );
344+
QGSCOMPARENEAR( qgsRound( 98765432198, 14 ), 98765432198, 1.0 );
345+
QGSCOMPARENEAR( qgsRound( 98765432198765, 14 ), 98765432198765, 1.0 );
346+
QGSCOMPARENEAR( qgsRound( 98765432198765432, 20 ), 98765432198765432, 1.0 );
347+
QGSCOMPARENEAR( qgsRound( 9.8765432198765, 2 ), 9.88, 0.001 );
348+
QGSCOMPARENEAR( qgsRound( 9.8765432198765, 3 ), 9.877, 0.0001 );
349+
QGSCOMPARENEAR( qgsRound( 9.8765432198765, 4 ), 9.8765, 0.00001 );
350+
QGSCOMPARENEAR( qgsRound( 9.8765432198765, 5 ), 9.87654, 0.000001 );
351+
QGSCOMPARENEAR( qgsRound( 9.8765432198765, 6 ), 9.876543, 0.0000001 );
352+
QGSCOMPARENEAR( qgsRound( 9.8765432198765, 7 ), 9.8765432, 0.00000001 );
353+
QGSCOMPARENEAR( qgsRound( -9.8765432198765, 7 ), -9.876543, 0.000001 );
354+
QGSCOMPARENEAR( qgsRound( 9876543.2198765, 5 ), 9876543.219880, 0.000001 );
355+
QGSCOMPARENEAR( qgsRound( -9876543.2198765, 5 ), -9876543.219870, 0.000001 );
356+
357+
358+
}
359+
335360

336361
QGSTEST_MAIN( TestQgis )
337362
#include "testqgis.moc"

0 commit comments

Comments
 (0)