[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;
- [lmi-commits] [lmi] branch odd/width-of-inf created (now b7b4812), Greg Chicares, 2018/03/21
- [lmi-commits] [lmi] odd/width-of-inf b7b4812 6/6: Sketch and compare a different algorithm,
Greg Chicares <=
- [lmi-commits] [lmi] odd/width-of-inf df8f6b0 3/6: Augment unit tests, Greg Chicares, 2018/03/21
- [lmi-commits] [lmi] odd/width-of-inf d3cb41e 2/6: Explicitly qualify std::strtof(), Greg Chicares, 2018/03/21
- [lmi-commits] [lmi] odd/width-of-inf 3eff232 5/6: Add failing test: scale by 10^2147483640 to print "inf", Greg Chicares, 2018/03/21
- [lmi-commits] [lmi] odd/width-of-inf b487178 4/6: Augment unit tests, Greg Chicares, 2018/03/21
- [lmi-commits] [lmi] odd/width-of-inf 2589cea 1/6: Add digressionary-branch spells to the grimoire, Greg Chicares, 2018/03/21