[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master ef76b1f 5/8: Fix the failing test just commit
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master ef76b1f 5/8: Fix the failing test just committed |
Date: |
Sat, 17 Mar 2018 19:12:57 -0400 (EDT) |
branch: master
commit ef76b1f155f883a1a59d66dd74433349f05e63cb
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Fix the failing test just committed
Incidentally improved documentation.
---
miscellany.cpp | 21 ++++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)
diff --git a/miscellany.cpp b/miscellany.cpp
index 9f0cce9..48d18d6 100644
--- a/miscellany.cpp
+++ b/miscellany.cpp
@@ -28,7 +28,7 @@
#include "stl_extensions.hpp" // nonstd::power()
#include <algorithm> // equal(), max()
-#include <cmath> // floor(), log10()
+#include <cmath> // ceil(), floor(), log10()
#include <ctime>
#include <fstream>
#include <istream>
@@ -88,6 +88,17 @@ bool files_are_identical(std::string const& file0,
std::string const& file1)
/// max_power itself need not be an integral multiple of three:
/// a column might reasonably provide room for "99,999,999" only.
///
+/// It is reasonable to assume that rounding is away from infinity
+/// (potentially making formatted values wider), and no coarser than
+/// to whole units. Thus, 999.99 might be formatted as 1000. However,
+/// 600 would not become 1000 because round-to-nearest-hundred is not
+/// a reasonable rule for currency amounts--although, of course, after
+/// scaling by 10^3 it may become 1 (in thousands). These are the most
+/// conservative plausible rounding assumptions; actual rounding
+/// parameters are of course not knowable here because of separation
+/// of concerns, and knowing them would not enable any significant
+/// refinement.
+///
/// Asserted preconditions:
/// 3 <= max_power
/// min_value <= max_value
@@ -97,6 +108,12 @@ 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
// total characters as ten times its absolute value requires.
double widest = std::max(min_value * -10, max_value);
@@ -106,8 +123,6 @@ int scale_power(int max_power, double min_value, double
max_value)
return 0;
}
-// PDF !! This seems not to be rigorously correct: $999,999,999.99 is
-// less than one billion, but rounds to $1,000,000,000.
double d = std::log10(widest);
d = std::floor(d / 3.0);
int k = 3 * static_cast<int>(d);
- [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 <=
- [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, 2018/03/17
- [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