[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))
{
- [lmi-commits] [lmi] master updated (566fa11 -> 232d118), Greg Chicares, 2018/03/17
- [lmi-commits] [lmi] master 50b6290 2/8: Add a failing unit test, Greg Chicares, 2018/03/17
- [lmi-commits] [lmi] master ef76b1f 5/8: Fix the failing test just committed, Greg Chicares, 2018/03/17
- [lmi-commits] [lmi] master 99c2c5e 7/8: Augment unit test and documentation, Greg Chicares, 2018/03/17
- [lmi-commits] [lmi] master b282c3f 1/8: Add simple unit tests, Greg Chicares, 2018/03/17
- [lmi-commits] [lmi] master 2f9e574 4/8: Add a failing unit test, Greg Chicares, 2018/03/17
- [lmi-commits] [lmi] master f053d67 6/8: Reduce trickiness,
Greg Chicares <=
- [lmi-commits] [lmi] master a96528d 3/8: Fix the failing test just committed, Greg Chicares, 2018/03/17
- [lmi-commits] [lmi] master 232d118 8/8: Augment unit test, demonstrating the present algorithm's limitations, Greg Chicares, 2018/03/17