lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] valyuta/002 63419d4 14/21: Improve documentation


From: Greg Chicares
Subject: [lmi-commits] [lmi] valyuta/002 63419d4 14/21: Improve documentation
Date: Fri, 18 Sep 2020 21:53:49 -0400 (EDT)

branch: valyuta/002
commit 63419d484f25c409154f2903b4ba9d90d9324968
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Improve documentation
---
 currency.hpp | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/currency.hpp b/currency.hpp
index 55ba053..ca6cd74 100644
--- a/currency.hpp
+++ b/currency.hpp
@@ -100,7 +100,15 @@ class currency
 
     // Mixed-mode arithmetic is generally to be avoided, but it is
     // safe to multiply currency by an integer such as twelve:
-    // $1 monthly really does equal $12 annually.
+    // $1 monthly really does equal $12 annually. No operator/(int)
+    // is provided because $1 annually doesn't equal any integral
+    // number of cents per month.
+    //
+    // Ignore the possibility of overflow, at least for now:
+    //  - multiplying by an enormous integer would be unreasonable;
+    //  - C++ can't detect integer overflow very well anyway; and
+    // ultimately this class might use a floating data_type, which is
+    // highly unlikely to overflow on multiplication by an int.
     currency const& operator*=(int z) {m_ *= z; return *this;}
 
     data_type m() const {return m_;} // restrict to friends?
@@ -151,13 +159,18 @@ inline bool operator>=(currency const& lhs, currency 
const& rhs){return !operato
 inline currency operator+(currency lhs, currency rhs) {return lhs += rhs;}
 inline currency operator-(currency lhs, currency rhs) {return lhs -= rhs;}
 
-// Don't actually want any member operator*() for double:
+inline currency operator*(currency lhs, int    rhs) {return lhs *= rhs;}
+inline currency operator*(int    lhs, currency rhs) {return rhs *= lhs;}
+
+// There is deliberately no operator*=(double) or operator/=(double).
+// These operators are safe because they return double. Yet because
+// they are implicit, they hide conversions. At least the conversions
+// they hide are the comparatively inexpensive and value-preserving
+// to_double() instead of the expensive from_double() that does not
+// preserve value. Probably either analogous additive operators should
+// be provided, or these should not be provided.
 inline double operator*(currency lhs, double rhs) {return lhs.d() *  rhs;}
 inline double operator*(double lhs, currency rhs) {return lhs *  rhs.d();}
-inline currency    operator*(currency lhs, int    rhs) {return lhs *= rhs;}
-inline currency    operator*(int    lhs, currency rhs) {return rhs *= lhs;}
-//inline currency operator*(currency lhs, int rhs) {return lhs *= rhs;}
-//inline currency operator*(int lhs, currency rhs) {return rhs *= lhs;}
 inline double operator/(currency lhs, double rhs) {return lhs.d() /  rhs;}
 inline double operator/(double lhs, currency rhs) {return lhs /  rhs.d();}
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]