[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] valyuta/002 0856081 20/65: improve currency
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] valyuta/002 0856081 20/65: improve currency |
Date: |
Wed, 16 Sep 2020 16:55:14 -0400 (EDT) |
branch: valyuta/002
commit 08560813468f44152691c85292e4b5f98f8065d2
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
improve currency
---
currency.hpp | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/currency.hpp b/currency.hpp
index f6137d3..d8db058 100644
--- a/currency.hpp
+++ b/currency.hpp
@@ -47,9 +47,9 @@ class currency
#if defined __GNUC__
# pragma GCC diagnostic ignored "-Wuseless-cast"
#endif // defined __GNUC__
- using data_type = double;
+// using data_type = double;
// using data_type = long double;
-// using data_type = std::int64_t;
+ using data_type = std::int64_t;
friend std::ostream& operator<<(std::ostream&, currency const&);
friend class currency_test;
@@ -67,9 +67,11 @@ class currency
explicit currency(std::int64_t d, bool) {m_ = bourn_cast<data_type>(d);}
// instead: init-list
currency& operator=(currency const&) = default;
+ // IMPORTANT eventually suppress this?
currency& operator=(double d) {m_ = from_double(d); return *this;}
operator double() const {return to_double();}
+ double d() const {return to_double();}
// Is this better, with 'const&'?
// bool operator==(currency const& z) const {return z.m_ == m_;}
@@ -135,6 +137,7 @@ class currency
data_type m_ = {0};
};
+//inline currency operator+(currency lhs, currency rhs) {return lhs += rhs;}
inline currency operator+(currency lhs, double rhs) {return lhs +=
currency(rhs);}
inline currency operator-(currency lhs, double rhs) {return lhs -=
currency(rhs);}
//inline currency operator*(currency lhs, double rhs) {return lhs *=
currency(rhs);}
@@ -151,6 +154,7 @@ inline std::ostream& operator<<(std::ostream& os, currency
const& c)
#endif // defined USE_CURRENCY_CLASS
+#if 0
// Sloppy.
inline currency requantize(double z) {return currency(z);}
@@ -162,10 +166,12 @@ inline std::vector<currency>
currencyize(std::vector<double> const& z)
r.push_back(currency(i));
return r;
}
+#endif // 0
inline double doubleize(currency const& z)
{
- return currency(z); // This implementation seems surprising.
+// return currency(z); // This implementation would seem surprising.
+ return z.d();
}
inline std::vector<double> doubleize(std::vector<currency> const& z)
@@ -174,7 +180,8 @@ inline std::vector<double> doubleize(std::vector<currency>
const& z)
r.reserve(z.size());
for(auto const& i : z)
// r.push_back(i.operator double()); // no need to convert explicitly
- r.push_back(i);
+// but that relies on a public operator double(), which is better avoided
+ r.push_back(i.d());
return r;
}
- [lmi-commits] [lmi] valyuta/002 2dabfcf 62/65: Try to make something a little less arcane, (continued)
- [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
- [lmi-commits] [lmi] valyuta/002 615a24c 48/65: round.c(), Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 9a4b91d 51/65: Speed measurements, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 49f84a1 60/65: Don't convert type with operator=(), Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 9a9665c 58/65: Record some speed improvements, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 ce34d89 04/65: improve, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 8331a5c 07/65: avoid catastrophic cancellation, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 0856081 20/65: improve currency,
Greg Chicares <=
- [lmi-commits] [lmi] valyuta/002 3d03e4a 41/65: fix typo, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 a00f68f 52/65: document, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 7de1baa 56/65: Note some more instances of the missing unary operator-(), Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 4d9fed5 15/65: document, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 5489e25 40/65: use data_type, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 a32e29b 53/65: Avoid implicit conversions, whose (hidden) cost is very high, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 39d842f 57/65: Simplify: catastrophic cancellation less worrisome with integer math, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 d124022 64/65: trace solves in self test, Greg Chicares, 2020/09/16
- [lmi-commits] [lmi] valyuta/002 4f46dd5 59/65: Assert a precondition, Greg Chicares, 2020/09/16