lmi-commits
[Top][All Lists]

## [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
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
//  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_;}

```