changeset 1413:c6fa111b4553 scale-ticks

Tests now passing
author Chris Cannam
date Thu, 04 May 2017 08:53:00 +0100
parents b7a9edee85e0
children c57994e1edd7
files base/ScaleTickIntervals.h base/test/TestScaleTickIntervals.h
diffstat 2 files changed, 32 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/base/ScaleTickIntervals.h	Thu May 04 08:32:41 2017 +0100
+++ b/base/ScaleTickIntervals.h	Thu May 04 08:53:00 2017 +0100
@@ -20,7 +20,7 @@
 #include <vector>
 #include <cmath>
 
-#define DEBUG_SCALE_TICK_INTERVALS 1
+//#define DEBUG_SCALE_TICK_INTERVALS 1
 
 #ifdef DEBUG_SCALE_TICK_INTERVALS
 #include <iostream>
@@ -122,6 +122,17 @@
 	double min = ceil(r.min / roundTo) * roundTo;
 	if (min > r.max) min = r.max;
 
+        if (!fixed && min != 0.0) {
+            double digNewMin = log10(fabs(min));
+            if (digNewMin < digInc) {
+                prec = int(ceil(digMax - digNewMin));
+#ifdef DEBUG_SCALE_TICK_INTERVALS
+                std::cerr << "min is smaller than increment, adjusting prec to "
+                          << prec << std::endl;
+#endif
+            }
+        }
+        
 	Ticks t { min, inc, roundTo, fixed, prec, {} };
 	explode(r, t);
 	return t;
--- a/base/test/TestScaleTickIntervals.h	Thu May 04 08:32:41 2017 +0100
+++ b/base/test/TestScaleTickIntervals.h	Thu May 04 08:53:00 2017 +0100
@@ -229,6 +229,19 @@
 	compareTicks(ticks.ticks, expected);
     }
     
+    void linear_0p001_1_5()
+    {
+	auto ticks = ScaleTickIntervals::linear({ 0.001, 1, 5 });
+	vector<ScaleTickIntervals::Tick> expected {
+	    { 0.1, "0.1" },
+	    { 0.3, "0.3" },
+	    { 0.5, "0.5" },
+	    { 0.7, "0.7" },
+	    { 0.9, "0.9" },
+	};
+	compareTicks(ticks.ticks, expected);
+    }
+        
     void linear_10000_10010_5()
     {
 	auto ticks = ScaleTickIntervals::linear({ 10000, 10010, 5 });
@@ -313,13 +326,13 @@
     {
 	auto ticks = ScaleTickIntervals::linear({ M_PI, 6.022140857e23, 7 });
 	vector<ScaleTickIntervals::Tick> expected {
-            { 1e+21, "1.0e+21" },
-            { 8.7e+22, "8.7e+22" },
-            { 1.73e+23, "1.73e+23" },
-            { 2.59e+23, "2.59e+23" },
-            { 3.45e+23, "3.45e+23" },
-            { 4.31e+23, "4.31e+23" },
-            { 5.17e+23, "5.17e+23" },
+            { 1e+21, "1.000e+21" },
+            { 8.7e+22, "8.700e+22" },
+            { 1.73e+23, "1.730e+23" },
+            { 2.59e+23, "2.590e+23" },
+            { 3.45e+23, "3.450e+23" },
+            { 4.31e+23, "4.310e+23" },
+            { 5.17e+23, "5.170e+23" },
 	};
 	compareTicks(ticks.ticks, expected);
     }