lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] odd/width-of-inf b7b4812 6/6: Sketch and compare a d


From: Greg Chicares
Subject: [lmi-commits] [lmi] odd/width-of-inf b7b4812 6/6: Sketch and compare a different algorithm
Date: Wed, 21 Mar 2018 19:20:23 -0400 (EDT)

branch: odd/width-of-inf
commit b7b4812d23d7b5590ad091fd532220261eea7e07
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Sketch and compare a different algorithm
    
    "-0" is really two characters wide.
    "inf" is really three characters wide.
---
 miscellany.cpp | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/miscellany.cpp b/miscellany.cpp
index 665c234..d8bf738 100644
--- a/miscellany.cpp
+++ b/miscellany.cpp
@@ -29,6 +29,7 @@
 
 #include <algorithm>                    // equal(), max()
 #include <cmath>                        // ceil(), floor(), log10()
+#include <cstdio>                       // snprintf()
 #include <ctime>
 #include <fstream>
 #include <istream>
@@ -124,6 +125,15 @@ int scale_power(int max_power, double min_value, double 
max_value)
     LMI_ASSERT(3 <= max_power);
     LMI_ASSERT(min_value <= max_value);
 
+    // Round to int, away from zero.
+    auto round_outward = [](double d)
+        {return (d < 0) ? std::floor(d) : std::ceil(d);};
+
+    int width = std::max
+        (std::snprintf(nullptr, 0, "%.f", round_outward(min_value))
+        ,std::snprintf(nullptr, 0, "%.f", round_outward(max_value))
+        );
+
     // 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.
@@ -132,6 +142,14 @@ int scale_power(int max_power, double min_value, double 
max_value)
 
     double widest = std::max(adjust(min_value), adjust(max_value));
 
+if(width != ((0 == widest) ? 1 : 1 + static_cast<int>(std::log10(widest))))
+ warning()
+  << min_value << " min_value\n"
+  << max_value << " max_value\n"
+  << width << " width\n"
+  << ((0 == widest) ? 1 : 1 + static_cast<int>(std::log10(widest))) << " (0 == 
widest) ? 1 : 1 + static_cast<int>(std::log10(widest))\n"
+  << std::flush
+  ;
     if(0 == widest || widest < nonstd::power(10.0, max_power))
         {
         return 0;



reply via email to

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