[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] valyuta/005 58b3efe: Identify issues in class curren
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] valyuta/005 58b3efe: Identify issues in class currency |
Date: |
Wed, 20 Jan 2021 15:55:36 -0500 (EST) |
branch: valyuta/005
commit 58b3efe4d2f36f1e7f19683823968415f5c3e646
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Identify issues in class currency
---
currency.hpp | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)
diff --git a/currency.hpp b/currency.hpp
index 03fbcf7..9c55583 100644
--- a/currency.hpp
+++ b/currency.hpp
@@ -24,19 +24,26 @@
#include "config.hpp"
-#include "so_attributes.hpp"
+#include "so_attributes.hpp" // CURRENCY !! iff a separate TU is ever wanted
#include <ostream>
#include <vector>
+// Macros USE_CURRENCY_CLASS and CURRENCY_UNIT_IS_CENTS are used
+// elsewhere. Eventually they'll both be eliminated, along with
+// all code along paths where they aren't both defined.
+
#define USE_CURRENCY_CLASS
#if !defined USE_CURRENCY_CLASS
+# undef CURRENCY_UNIT_IS_CENTS // Requires currency class.
using currency = double;
inline currency from_cents(double z) {return z / 100.0;}
+// CURRENCY !! Rename these both to 'dblize'.
+
inline double d(currency z) {return z;}
inline std::vector<double> doubleize(std::vector<currency> const& z)
@@ -46,16 +53,17 @@ inline std::vector<double> doubleize(std::vector<currency>
const& z)
#else // defined USE_CURRENCY_CLASS
-// Retain this--it's used elsewhere.
# define CURRENCY_UNIT_IS_CENTS
class raw_cents {}; // Tag class.
+// CURRENCY !! "LMI_SO" iff some part eventually becomes a TU.
class LMI_SO currency
{
friend class currency_test;
template<typename> friend class round_to;
friend class round_to_test;
+
# if defined CURRENCY_UNIT_IS_CENTS
static constexpr int cents_digits = 2;
static constexpr double cents_per_dollar = 100.0;
@@ -67,9 +75,9 @@ class LMI_SO currency
public:
using data_type = double;
-// currency() : m_ {0} {} // prevents "constexpr currency zero {};"
+// currency() : m_ {0} {} // CURRENCY !! prevents "constexpr currency zero
{};"
currency() = default;
- explicit currency(data_type z, raw_cents) : m_ {z} {}
+ explicit currency(data_type z, raw_cents) : m_ {z} {assert_integral(z);}
currency& operator+=(currency const& z) {m_ += z.m_; return *this;}
currency& operator-=(currency const& z) {m_ -= z.m_; return *this;}
@@ -77,10 +85,13 @@ class LMI_SO currency
currency& operator*=(int z) {m_ *= z ; return *this;}
data_type cents() const {return m_;}
- // !! possible underflow?
+ // CURRENCY !! add a unit test for possible underflow
+ // CURRENCY !! is multiplication by reciprocal faster or more accurate?
double d() const {return m_ / cents_per_dollar;}
private:
+ // CURRENCY !! Actually implement this.
+ void assert_integral(data_type) {}
data_type m_ = {};
};
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [lmi-commits] [lmi] valyuta/005 58b3efe: Identify issues in class currency,
Greg Chicares <=