[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] valyuta/002 11484a9 7/8: Differentiate integral from
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] valyuta/002 11484a9 7/8: Differentiate integral from floating conversions |
Date: |
Mon, 5 Oct 2020 19:57:19 -0400 (EDT) |
branch: valyuta/002
commit 11484a9b5c6c29ad4d2d3ba17529edfbc76e2d3a
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Differentiate integral from floating conversions
---
currency.hpp | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/currency.hpp b/currency.hpp
index 0ee3475..283159f 100644
--- a/currency.hpp
+++ b/currency.hpp
@@ -128,8 +128,25 @@ class currency
// ...less bad:
// data_type from_double(double d) const {return
bourn_cast<data_type>(round(cents_per_dollar * d));}
#if !defined MAKE_IT_EVEN_FASTER
+# if defined CURRENCY_HAS_INTEGER_DATATYPE
+ // Convert double <-> integer: prefer explicit rounding to
+ // implicit truncation.
data_type from_double(double d) const {return round(cents_per_dollar * d);}
+ // Here, bourn_cast actually does something:
double to_double() const {return bourn_cast<double>(m_) /
cents_per_dollar;}
+# else // !defined CURRENCY_HAS_INTEGER_DATATYPE
+ // Converting double <-> double involves no implicit change.
+ // Certainly 1.23 * 100.0 is unlikely to equal 123 exactly, but
+ // that's why this conversion really should be avoided: instead
+ // of calling some arbitrary rounding function here, something
+ // suitable for the context should be used, e.g.:
+ // double d = loan_value()
+ // currency c = round_loan_.c(d);
+ data_type from_double(double d) const {return cents_per_dollar * d ;}
+ // Here, bourn_cast costs something, but does nothing:
+// double to_double() const {return bourn_cast<double>(m_) /
cents_per_dollar;}
+ double to_double() const {return static_cast<double>(m_) /
cents_per_dollar;}
+# endif // !defined CURRENCY_HAS_INTEGER_DATATYPE
#else // defined MAKE_IT_EVEN_FASTER
data_type from_double(double d) const {return d;}
double to_double() const {return m_;}
- [lmi-commits] [lmi] valyuta/002 updated (53eefda -> a3f1b6f), Greg Chicares, 2020/10/05
- [lmi-commits] [lmi] valyuta/002 3d21745 3/8: Fix unit tests, Greg Chicares, 2020/10/05
- [lmi-commits] [lmi] valyuta/002 11484a9 7/8: Differentiate integral from floating conversions,
Greg Chicares <=
- [lmi-commits] [lmi] valyuta/002 3d1ad5e 2/8: Avoid another run-time check, Greg Chicares, 2020/10/05
- [lmi-commits] [lmi] valyuta/002 1700262 1/8: Make fewer costly assertions, Greg Chicares, 2020/10/05
- [lmi-commits] [lmi] valyuta/002 a8bfa96 5/8: Improve encapsulation, Greg Chicares, 2020/10/05
- [lmi-commits] [lmi] valyuta/002 a3f1b6f 8/8: Probably prefer floating to integral datatype, Greg Chicares, 2020/10/05
- [lmi-commits] [lmi] valyuta/002 7be2c59 4/8: Comment out member functions that are apparently unused, Greg Chicares, 2020/10/05
- [lmi-commits] [lmi] valyuta/002 bfc32eb 6/8: Use a more appropriate type for 'cents_per_dollar', Greg Chicares, 2020/10/05