[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 4b2bdc9 4/4: Fix the failing tests just commi
[lmi-commits] [lmi] master 4b2bdc9 4/4: Fix the failing tests just committed
Sun, 18 Mar 2018 20:02:19 -0400 (EDT)
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Fix the failing tests just committed
Redesigned the scaling-factor algorithm to be correct when the number
of characters allowed to the left of the decimal point is constrained
to a value that is not a multiple of three. The manifest constant '6'
had never been documented, and probably was just a magical value that
made the original code seem correct when exactly nine characters are
allowed. Comprehensibility has been improved along with correctness.
miscellany.cpp | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/miscellany.cpp b/miscellany.cpp
index a15f960..3c969c7 100644
@@ -135,13 +135,16 @@ int scale_power(int max_power, double min_value, double
- int k = static_cast<int>(std::log10(widest));
- k = 3 * (k / 3) - 6;
+ // Only characters [0-9-] to the left of any decimal point matter.
+ int const chars_required = 1 + static_cast<int>(std::log10(widest));
+ int const chars_available = max_power;
+ int const excess = chars_required - chars_available;
+ int const r = 3 * (1 + (excess - 1) / 3);
- LMI_ASSERT(0 <= k);
- LMI_ASSERT(k <= 18);
+ LMI_ASSERT(0 <= r);
+ LMI_ASSERT(r <= 18);
- return k;
+ return r;
/// Return the number of newline characters in a string.