changeset 1463:2b2b58ae8b59

Tidy up rounding + tests
author Chris Cannam
date Thu, 10 May 2018 10:12:13 +0100
parents aab2d7177d3d
children 91bb68146dfc
files base/ScaleTickIntervals.h base/test/TestScaleTickIntervals.h
diffstat 2 files changed, 15 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/base/ScaleTickIntervals.h	Fri May 04 14:05:41 2018 +0100
+++ b/base/ScaleTickIntervals.h	Thu May 10 10:12:13 2018 +0100
@@ -266,7 +266,11 @@
 
         if (display == Auto) {
 
-            int digits = (value != 0.0 ? 1 + int(floor(log10(abs(value)))) : 0);
+            double eps = 1e-10;
+            
+            int digits = (value != 0.0 ?
+                          1 + int(floor(eps + log10(abs(value)))) :
+                          0);
 
             // This is not the same logic as %g uses for determining
             // whether to delegate to use scientific or fixed notation
--- a/base/test/TestScaleTickIntervals.h	Fri May 04 14:05:41 2018 +0100
+++ b/base/test/TestScaleTickIntervals.h	Thu May 10 10:12:13 2018 +0100
@@ -32,14 +32,14 @@
     void printDiff(vector<ScaleTickIntervals::Tick> ticks,
                    vector<ScaleTickIntervals::Tick> expected) {
 
-    SVCERR << "Have " << ticks.size() << " ticks, expected "
-             << expected.size() << endl;
+        SVCERR << "Have " << ticks.size() << " ticks, expected "
+               << expected.size() << endl;
         for (int i = 0; i < int(ticks.size()); ++i) {
             SVCERR << i << ": have " << ticks[i].value << " \""
-                 << ticks[i].label << "\", expected ";
+                   << ticks[i].label << "\", expected ";
             if (i < int(expected.size())) {
                 SVCERR << expected[i].value << " \"" << expected[i].label
-                     << "\"" << endl;
+                       << "\"" << endl;
             } else {
                 SVCERR << "(n/a)" << endl;
             }
@@ -50,27 +50,24 @@
                       ScaleTickIntervals::Ticks expected,
                       bool fuzzier = false)
     {
-        double eps = 1e-7;
         for (int i = 0; i < int(expected.size()); ++i) {
             if (i < int(ticks.size())) {
                 bool pass = true;
                 if (ticks[i].label != expected[i].label) {
                     pass = false;
-                } else if (!fuzzier) {
-                    if (fabs(ticks[i].value - expected[i].value) > eps) {
-                        pass = false;
-                    }
                 } else {
-                    if (fabs(ticks[i].value - expected[i].value) >
-                        fabs(ticks[i].value) * 1e-5) {
+                    double eps = fuzzier ? 1e-5 : 1e-10;
+                    double diff = fabs(ticks[i].value - expected[i].value);
+                    double limit = max(eps, fabs(ticks[i].value) * eps);
+                    if (diff > limit) {
                         pass = false;
                     }
                 }
                 if (!pass) {
                     printDiff(ticks, expected);
-                    QCOMPARE(ticks[i].label, expected[i].label);
-                    QCOMPARE(ticks[i].value, expected[i].value);
                 }
+                QCOMPARE(ticks[i].label, expected[i].label);
+                QCOMPARE(ticks[i].value, expected[i].value);
             }
         }
         if (ticks.size() != expected.size()) {