[Git][NTPsec/ntpsec][wip-ntpq-peers-display] Fixed scaling. Again. Floats work as they should.

Ian Bruene gitlab at mg.gitlab.com
Thu Mar 30 04:37:51 UTC 2017


Ian Bruene pushed to branch wip-ntpq-peers-display at NTPsec / ntpsec


Commits:
472f1b37 by Ian Bruene at 2017-03-29T23:36:52-05:00
Fixed scaling. Again. Floats work as they should.

- - - - -


1 changed file:

- pylib/util.py


Changes:

=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -131,19 +131,19 @@ def rescaleunit(f, ooms):
 
 def scaleforunit(f):
     "Scales a number by units to keep it in the range 0.000-999.9"
-    if f < timefuzz:
+    if -timefuzz < f < timefuzz:  # if sufficiently close to zero do nothing
         return (f, 0)
     unitsmoved = 0
-    oom = int(math.log10(abs(f)))  # Orders Of Magnitude
+    af = abs(f)
+    if af < 1.0:
+        oom = math.floor(math.log10(af))
+    else:
+        oom = math.log10(af)  # Orders Of Magnitude
     oom -= oom % 3  # We only want to move in groups of 3 ooms
-    multiplier = 10 ** oom
+    multiplier = 10 ** -oom  # Reciprocol because floating * more accurate
     unitsmoved = oom // 3
-    if f < 1.0:  # Shift upwards
-        f *= multiplier
-        unitsmoved = -unitsmoved
-    else:
-        f /= multiplier
-    return (f, unitsmoved)
+    f *= multiplier
+    return (f, int(unitsmoved))
 
 
 def formatdigitsplit(f, fieldsize):
@@ -174,7 +174,7 @@ def unitformatter(f, unitgroup, startingunit, baseunit=None,
         strip = True
     if baseunit is None:
         baseunit = 0  # Assume that the lowest unit is equal to LSB
-    if f == 0.0:  # Zero, don't show decimals, and show that it is zero
+    if -timefuzz < f < timefuzz:  # Zero, don't show decimals
         unit = unitgroup[baseunit]  # all the way to the lsb
         rendered = "0" + unit
         if not strip:



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/472f1b3761f863e7d4ef2c0e080fba97610de62b
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170330/8d5f2662/attachment.html>


More information about the vc mailing list