From a49b41a74b37db35562efb298c91ff0dcc8bd954 Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (o)" <j.wuttke@fz-juelich.de>
Date: Mon, 31 Jul 2017 17:22:12 +0200
Subject: [PATCH] Extra precautions to prevent rounding error or excessive
 precision in tack values.

---
 pub/plot/axis.cpp | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/pub/plot/axis.cpp b/pub/plot/axis.cpp
index 4ebe1d6f..01a50438 100644
--- a/pub/plot/axis.cpp
+++ b/pub/plot/axis.cpp
@@ -362,10 +362,21 @@ void CAxis::calc_lintacks(vector<double>& Tacks, int& ntpt, double& dtack) const
         dtack = .25 * pow(10., ir);
         ntpt = 5;
     }
+    // Sets tack vector, taking extra precaution to avoid rounding errors.
     Tacks.clear();
-    for (double d = dtack * (floor(inf/dtack)-1); d <= dtack * (ceil(sup/dtack)+1); d += dtack)
+    double dlow = dtack * (floor(inf/dtack)-1);
+    double dhig = dtack * (ceil(sup/dtack)+1);
+    double drange = dhig-dlow;
+    int nTacks = drange/dtack + 1.5;
+    if (nTacks<2)
+        throw "BUG: calc_lintacks -> nTacks<2";
+    for (int i=0; i<nTacks; ++i) {
+        double d = (i*dhig + (nTacks-1-i)*dlow)/(nTacks-1);
+        if (std::abs(d)<1e-15*drange)
+            d = 0;
         if (inf-1e-4*range <= d && d <= sup+1e-4*range)
             Tacks.push_back(d);
+    }
 }
 
 
-- 
GitLab