[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master c41faec 12/13: Resolve a marked defect [284]
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master c41faec 12/13: Resolve a marked defect [284] |
Date: |
Fri, 9 Apr 2021 18:42:38 -0400 (EDT) |
branch: master
commit c41faec51e590438a74ef45ffcb9b6db1fcb3eb4
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Resolve a marked defect [284]
---
round_to.hpp | 22 +++++++++++++++-------
1 file changed, 15 insertions(+), 7 deletions(-)
diff --git a/round_to.hpp b/round_to.hpp
index 06bd092..54f552e 100644
--- a/round_to.hpp
+++ b/round_to.hpp
@@ -207,13 +207,6 @@ class round_to
rounding_fn_t rounding_function_ {detail::erroneous_rounding_function};
};
-// Division by an exact integer value should have slightly better
-// accuracy in some cases. But profiling shows that multiplication by
-// the reciprocal stored in scale_back_ makes a realistic application
-// that performs a lot of rounding run about four percent faster with
-// all compilers tested. TODO ?? The best design decision would be
-// clearer if we quantified the effect on accuracy.
-
template<typename RealType>
round_to<RealType>::round_to(int a_decimals, rounding_style a_style)
:decimals_ {a_decimals}
@@ -268,6 +261,21 @@ bool round_to<RealType>::operator==(round_to const& z)
const
return decimals() == z.decimals() && style() == z.style();
}
+/// Division by an exact integer value would afford a stronger
+/// guarantee of accuracy, particularly when the value to be rounded
+/// is already rounded--e.g., if 3.00 is to be rounded to hundredths,
+/// then
+/// (100.0 * 3.00) / 100
+/// is preferable to
+/// (100.0 * 3.00) * 0.01
+/// especially if the rounding style is anything but to-nearest.
+///
+/// However, reciprocal multiplication is faster than division--see:
+/// https://lists.nongnu.org/archive/html/lmi/2021-04/msg00010.html
+/// et seqq., which demonstrates an average three-percent speedup,
+/// with no observed difference in a comprehensive system test as
+/// long as the multiplications are performed in extended precision.
+
template<typename RealType>
inline RealType round_to<RealType>::operator()(RealType r) const
{
- [lmi-commits] [lmi] master edb098f 02/13: Strengthen unit tests, (continued)
- [lmi-commits] [lmi] master edb098f 02/13: Strengthen unit tests, Greg Chicares, 2021/04/09
- [lmi-commits] [lmi] master 2a3d961 03/13: Measure cost of a needless transcendental calculation, Greg Chicares, 2021/04/09
- [lmi-commits] [lmi] master 6bc828a 05/13: Realign, Greg Chicares, 2021/04/09
- [lmi-commits] [lmi] master 290dc89 10/13: Remove a source of inaccuracy [285], Greg Chicares, 2021/04/09
- [lmi-commits] [lmi] master a32cee0 13/13: Hoist a division, Greg Chicares, 2021/04/09
- [lmi-commits] [lmi] master 1e49ee3 04/13: Revise 'round_to' documentation [286], Greg Chicares, 2021/04/09
- [lmi-commits] [lmi] master 1bead9e 08/13: Purge unwanted, commented-out code, Greg Chicares, 2021/04/09
- [lmi-commits] [lmi] master ce9ed5c 07/13: Add an inchoate test of power-of-ten scaling, Greg Chicares, 2021/04/09
- [lmi-commits] [lmi] master 858d037 09/13: Include appropriate headers, and say why they're included, Greg Chicares, 2021/04/09
- [lmi-commits] [lmi] master ca63009 11/13: Expunge an obsolete comment, Greg Chicares, 2021/04/09
- [lmi-commits] [lmi] master c41faec 12/13: Resolve a marked defect [284],
Greg Chicares <=