changeset 1421:8b7d6c1e1ab7

Adjust rounding; fixes tests on 32-bit Linux test box. Also update one test. Next: recheck everywhere else.
author Chris Cannam
date Thu, 20 Jul 2017 14:52:49 +0100
parents a533662c17f4
children 04ce84f21af3
files base/ScaleTickIntervals.h base/test/TestScaleTickIntervals.h
diffstat 2 files changed, 21 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/base/ScaleTickIntervals.h	Mon Jul 10 14:23:19 2017 +0100
+++ b/base/ScaleTickIntervals.h	Thu Jul 20 14:52:49 2017 +0100
@@ -147,10 +147,22 @@
         double min = r.min;
         
         if (roundTo != 0.0) {
-            inc = round(inc / roundTo) * roundTo;
+            // Round inc to the nearest multiple of roundTo, and min
+            // to the next multiple of roundTo up. The small offset of
+            // eps is included to avoid inc of 2.49999999999 rounding
+            // to 2 or a min of -0.9999999999 rounding to 0, both of
+            // which would prevent some of our test cases from getting
+            // the most natural results.
+            double eps = 1e-8;
+            inc = round(inc / roundTo + eps) * roundTo;
             if (inc < roundTo) inc = roundTo;
-            min = ceil(min / roundTo) * roundTo;
+            min = ceil(min / roundTo - eps) * roundTo;
             if (min > r.max) min = r.max;
+            if (min == -0.0) min = 0.0;
+#ifdef DEBUG_SCALE_TICK_INTERVALS
+            SVDEBUG << "ScaleTickIntervals: rounded inc to " << inc
+                    << " and min to " << min << endl;
+#endif
         }
 
         if (display == Scientific && min != 0.0) {
--- a/base/test/TestScaleTickIntervals.h	Mon Jul 10 14:23:19 2017 +0100
+++ b/base/test/TestScaleTickIntervals.h	Thu Jul 20 14:52:49 2017 +0100
@@ -337,13 +337,13 @@
 	auto ticks = ScaleTickIntervals::linear({ M_PI, 6.022140857e23, 7 });
 	ScaleTickIntervals::Ticks expected {
             // not perfect, but ok-ish
-            { 1e+22, "1.00e+22" },
-            { 1e+23, "1.00e+23" },
-            { 1.9e+23, "1.90e+23" },
-            { 2.8e+23, "2.80e+23" },
-            { 3.7e+23, "3.70e+23" },
-            { 4.6e+23, "4.60e+23" },
-            { 5.5e+23, "5.50e+23" },
+            { 0, "0.0e+00" },
+            { 9e+22, "9.0e+22" },
+            { 1.8e+23, "1.8e+23" },
+            { 2.7e+23, "2.7e+23" },
+            { 3.6e+23, "3.6e+23" },
+            { 4.5e+23, "4.5e+23" },
+            { 5.4e+23, "5.4e+23" },
 	};
 	compareTicks(ticks, expected);
     }