[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] valyuta/002 794727e 22/65: Round when converting fro
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] valyuta/002 794727e 22/65: Round when converting from double to currency |
Date: |
Wed, 16 Sep 2020 16:55:15 -0400 (EDT) |
branch: valyuta/002
commit 794727e6cd5d520331fb58d6b948deece7814ffe
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Round when converting from double to currency
Ultimately, each conversion from double should be rounded explicitly
in some specific manner. This is just a temporary expedient to avoid
spurious regression errors.
---
currency.hpp | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)
diff --git a/currency.hpp b/currency.hpp
index d8db058..c2260fa 100644
--- a/currency.hpp
+++ b/currency.hpp
@@ -27,6 +27,7 @@
#include "bourn_cast.hpp"
////#include "round_to.hpp"
+#include <cmath> // round
#include <cstdint> // int64_t
#include <iostream> // ostream
#include <vector>
@@ -109,6 +110,7 @@ class currency
// currency& operator*=(double z) {m_ = bourn_cast<data_type>(100.0 *
to_double() * z); return *this;}
// Far too permissive:
// currency& operator*=(double z) {m_ = static_cast<data_type>(100.0 *
to_double() * z); return *this;}
+ // wrong: doesn't affect '*this'
double operator*=(double z) {return to_double() * z;}
// Dangerous--can somehow take the place of operator*(double)
// currency const& operator*=(int z) {m_ *= z; return *this;}
@@ -123,17 +125,22 @@ class currency
// ...and a bit insidious:
// data_type from_double(double d) const {return
static_cast<data_type>(100.000000000001 * d);}
// ...less bad:
-// data_type from_double(double d) const {return round(cents_per_dollar * d);}
-// double to_double() const {return bourn_cast<double>(m_) /
cents_per_dollar;}
- data_type from_double(double d) const {return
static_cast<data_type>(cents_per_dollar * d);}
- double to_double() const {return static_cast<double>(m_) /
cents_per_dollar;}
+ data_type from_double(double d) const {return round(cents_per_dollar * d);}
+ double to_double() const {return bourn_cast<double>(m_) /
cents_per_dollar;}
+// data_type from_double(double d) const {return
static_cast<data_type>(cents_per_dollar * d);}
+// double to_double() const {return static_cast<double>(m_) /
cents_per_dollar;}
#if 0 // will a fwd decl be wanted somewhere?
data_type round(double d) const
{
static round_to<double> const r(0, r_to_nearest);
return static_cast<data_type>(r(d));
}
-#endif // 0
+#else // 1
+ data_type round(double d) const
+ {
+ return static_cast<data_type>(std::round(d));
+ }
+#endif // 1
data_type m_ = {0};
};
- [lmi-commits] [lmi] valyuta/002 ba43414 61/65: Record 64-bit timings, (continued)
- [lmi-commits] [lmi] valyuta/002 ba43414 61/65: Record 64-bit timings, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 fb82290 63/65: Use maximal 64-bit rather than 32-bit integer value, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 8fd00bb 34/65: rework operator*(), Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 bf1943b 03/65: reformat, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 7481c2b 09/65: discard--throwaway assertions, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 998fdf5 35/65: operator/(), Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 c105c78 29/65: Currency: implement binary operator-, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 9cf4517 46/65: c(), Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 9da6616 55/65: Round explicitly in a few more cases, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 0e8c429 12/65: rounding, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 794727e 22/65: Round when converting from double to currency,
Greg Chicares <=
- [lmi-commits] [lmi] valyuta/002 d137177 02/65: Currency, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 ba6a8d1 31/65: implement currency comparisons as free functions, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 28a5e54 39/65: make data_type public, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 e9f5447 30/65: round currency to currency? huh?, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 90773f3 47/65: currency() rather than currency(0.0), Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 5f00c5a 43/65: Add a commented-out idea, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 2dabfcf 62/65: Try to make something a little less arcane, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 9433c8e 65/65: Discuss timing, and philosophical and political questions, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 9bee994 19/65: use currency type, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 0bf83a4 18/65: augment, Greg Chicares, 2020/09/16