lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master f053d67 6/8: Reduce trickiness


From: Greg Chicares
Subject: [lmi-commits] [lmi] master f053d67 6/8: Reduce trickiness
Date: Sat, 17 Mar 2018 19:12:57 -0400 (EDT)

branch: master
commit f053d676939a4551058348061f6bf5615da1aab7
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Reduce trickiness
    
    The widest element (considering '-' as wide as any digit) may very well
    be the greater of these:
      std::floor(min_value) * -10.0
      std::ceil (max_value)
    for all combinations of maximum and minimum values, but it's not easy to
    tell at a glance. It is not materially more expensive, but much clearer,
    to adjust each value in the same manner (dependent on algebraic sign):
    if it's 999.99, make it 1000; and if it's -999, make it 9999, because
    both have four characters (which is still objectionably artificial).
---
 miscellany.cpp | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/miscellany.cpp b/miscellany.cpp
index 48d18d6..6d6576c 100644
--- a/miscellany.cpp
+++ b/miscellany.cpp
@@ -108,15 +108,13 @@ int scale_power(int max_power, double min_value, double 
max_value)
     LMI_ASSERT(3 <= max_power);
     LMI_ASSERT(min_value <= max_value);
 
-    auto round_away_from_zero = [&](double d)
-        {return (d < 0) ? std::floor(d) : std::ceil(d);};
-
-    min_value = round_away_from_zero(min_value);
-    max_value = round_away_from_zero(max_value);
-
-    // A negative value needs an extra '-' character: i.e., as many
+    // Round to int, away from zero, and multiply by ten if negative:
+    // a negative value needs an extra '-' character: i.e., as many
     // total characters as ten times its absolute value requires.
-    double widest = std::max(min_value * -10, max_value);
+    auto adjust = [](double d)
+        {return (d < 0) ? -10.0 * std::floor(d) : std::ceil(d);};
+
+    double widest = std::max(adjust(min_value), adjust(max_value));
 
     if(0 == widest || widest < nonstd::power(10.0, max_power))
         {



reply via email to

[Prev in Thread] Current Thread [Next in Thread]