lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] valyuta/005 90d7483 10/17: Improve currency class


From: Greg Chicares
Subject: [lmi-commits] [lmi] valyuta/005 90d7483 10/17: Improve currency class
Date: Sat, 16 Jan 2021 21:06:18 -0500 (EST)

branch: valyuta/005
commit 90d7483eab2935b60db0b0b1914264afaf9176da
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Improve currency class
    
    Most notably, CURRENCY_UNIT_IS_CENTS should be defined because it's
    used elsewhere. Now speed is the same as valyuta/004.
    
    This is to be used in preference to valyuta/004, whose currency class
    has apparent mistakes like this:
      inline currency operator*(currency lhs, int rhs) {return lhs *= rhs;}
    so that
      currency c0 {1, raw_cents};
      int i {10};
      std::cout << c0 * i0 << std::endl;
    would appear to modify 'c0'.
---
 Speed_gcc_i686-w64-mingw32    | 12 ++++----
 Speed_gcc_x86_64-pc-linux-gnu | 12 ++++----
 Speed_gcc_x86_64-w64-mingw32  | 12 ++++----
 currency.hpp                  | 69 ++++++++++++++++---------------------------
 4 files changed, 44 insertions(+), 61 deletions(-)

diff --git a/Speed_gcc_i686-w64-mingw32 b/Speed_gcc_i686-w64-mingw32
index 7214fcb..7c97163 100644
--- a/Speed_gcc_i686-w64-mingw32
+++ b/Speed_gcc_i686-w64-mingw32
@@ -1,7 +1,7 @@
 Test speed:
-  naic, no solve      : 5.441e-02 s mean;      54230 us least of  19 runs
-  naic, specamt solve : 1.082e-01 s mean;     100873 us least of  10 runs
-  naic, ee prem solve : 9.227e-02 s mean;      92007 us least of  11 runs
-  finra, no solve     : 1.718e-02 s mean;      16512 us least of  59 runs
-  finra, specamt solve: 5.920e-02 s mean;      59006 us least of  17 runs
-  finra, ee prem solve: 5.451e-02 s mean;      54315 us least of  19 runs
+  naic, no solve      : 5.378e-02 s mean;      52957 us least of  19 runs
+  naic, specamt solve : 1.002e-01 s mean;      99248 us least of  10 runs
+  naic, ee prem solve : 9.134e-02 s mean;      90378 us least of  11 runs
+  finra, no solve     : 1.664e-02 s mean;      16331 us least of  61 runs
+  finra, specamt solve: 5.897e-02 s mean;      58153 us least of  17 runs
+  finra, ee prem solve: 5.436e-02 s mean;      53565 us least of  19 runs
diff --git a/Speed_gcc_x86_64-pc-linux-gnu b/Speed_gcc_x86_64-pc-linux-gnu
index 1347b30..e3671aa 100644
--- a/Speed_gcc_x86_64-pc-linux-gnu
+++ b/Speed_gcc_x86_64-pc-linux-gnu
@@ -1,7 +1,7 @@
 Test speed:
-  naic, no solve      : 2.114e-02 s mean;      20601 us least of  48 runs
-  naic, specamt solve : 3.800e-02 s mean;      37455 us least of  27 runs
-  naic, ee prem solve : 3.461e-02 s mean;      34090 us least of  29 runs
-  finra, no solve     : 6.025e-03 s mean;       5735 us least of 100 runs
-  finra, specamt solve: 2.144e-02 s mean;      20834 us least of  47 runs
-  finra, ee prem solve: 1.977e-02 s mean;      19395 us least of  51 runs
+  naic, no solve      : 2.062e-02 s mean;      20294 us least of  49 runs
+  naic, specamt solve : 3.719e-02 s mean;      36633 us least of  27 runs
+  naic, ee prem solve : 3.386e-02 s mean;      33575 us least of  30 runs
+  finra, no solve     : 6.020e-03 s mean;       5799 us least of 100 runs
+  finra, specamt solve: 2.112e-02 s mean;      20654 us least of  48 runs
+  finra, ee prem solve: 1.947e-02 s mean;      19002 us least of  52 runs
diff --git a/Speed_gcc_x86_64-w64-mingw32 b/Speed_gcc_x86_64-w64-mingw32
index 541c303..e06ccc3 100644
--- a/Speed_gcc_x86_64-w64-mingw32
+++ b/Speed_gcc_x86_64-w64-mingw32
@@ -1,7 +1,7 @@
 Test speed:
-  naic, no solve      : 2.763e-02 s mean;      27513 us least of  37 runs
-  naic, specamt solve : 4.727e-02 s mean;      47033 us least of  22 runs
-  naic, ee prem solve : 4.337e-02 s mean;      43207 us least of  24 runs
-  finra, no solve     : 1.049e-02 s mean;      10334 us least of  96 runs
-  finra, specamt solve: 2.816e-02 s mean;      27960 us least of  36 runs
-  finra, ee prem solve: 2.619e-02 s mean;      26079 us least of  39 runs
+  naic, no solve      : 2.780e-02 s mean;      27325 us least of  36 runs
+  naic, specamt solve : 4.736e-02 s mean;      46781 us least of  22 runs
+  naic, ee prem solve : 4.344e-02 s mean;      42963 us least of  24 runs
+  finra, no solve     : 1.056e-02 s mean;      10319 us least of  95 runs
+  finra, specamt solve: 2.827e-02 s mean;      27932 us least of  36 runs
+  finra, ee prem solve: 2.633e-02 s mean;      25996 us least of  38 runs
diff --git a/currency.hpp b/currency.hpp
index 996b761..db76089 100644
--- a/currency.hpp
+++ b/currency.hpp
@@ -29,16 +29,22 @@
 #include <ostream>
 #include <vector>
 
-class raw_cents {};
+// Retain this--it's used elsewhere.
+#define CURRENCY_UNIT_IS_CENTS
+
+class raw_cents {}; // Tag class.
 
 class LMI_SO currency
 {
     friend class currency_test;
-    friend class round_to_test;
     template<typename T> friend class round_to;
+    friend class round_to_test;
 #if 1
     static constexpr int    cents_digits     = 2;
     static constexpr double cents_per_dollar = 100.0;
+#else
+    static constexpr int    cents_digits     = 0;
+    static constexpr double cents_per_dollar = 1.0;
 #endif // 1
 
   public:
@@ -46,16 +52,12 @@ class LMI_SO currency
 
 //  currency() : m_ {0} {} // prevents "constexpr currency zero {};"
     currency() = default;
-//  currency(currency const&) = default;
-//  ~currency() = default;
     explicit currency(data_type z, raw_cents) : m_ {z} {}
 
-//  currency& operator=(currency const&) = default;
-
     currency& operator+=(currency const& z) {m_ += z.m_; return *this;}
     currency& operator-=(currency const& z) {m_ -= z.m_; return *this;}
 
-    currency& operator*=(int z)             {m_ *= z; return *this;}
+    currency& operator*=(int z)             {m_ *= z   ; return *this;}
 
     data_type cents() const {return m_;}
     // !! possible underflow?
@@ -66,9 +68,7 @@ class LMI_SO currency
 };
 
 inline currency operator-(currency const& z)
-{
-    return currency(-z.cents(), raw_cents {});
-}
+    {return currency(-z.cents(), raw_cents {});}
 
 inline bool operator==(currency const& lhs, currency const& rhs)
     {return lhs.cents() == rhs.cents();}
@@ -84,50 +84,25 @@ inline bool operator>=(currency const& lhs, currency const& 
rhs)
     {return !operator< (lhs, rhs);}
 
 inline currency operator+(currency const& lhs, currency const& rhs)
-{
-    return currency {lhs} += rhs;
-}
-
+    {return currency {lhs} += rhs;}
 inline currency operator-(currency const& lhs, currency const& rhs)
-{
-    return currency {lhs} += -currency {rhs};
-}
+    {return currency {lhs} -= rhs;}
 
 inline currency operator*(currency const& lhs, int rhs)
-{
-    return currency {lhs} *= rhs;
-}
-
+    {return currency {lhs} *= rhs;}
 inline currency operator*(int lhs, currency const& rhs)
-{
-    return currency {rhs} *= lhs;
-}
+    {return currency {rhs} *= lhs;}
 
 inline double operator*(currency const& lhs, double rhs)
-{
-    return lhs.d() * rhs;
-}
-
+    {return lhs.d() * rhs;}
 inline double operator*(double lhs, currency const& rhs)
-{
-    return lhs * rhs.d();
-}
-
+    {return lhs * rhs.d();}
 inline double operator/(currency const& lhs, double rhs)
-{
-    return lhs.d() / rhs;
-}
-
-#if 0 // really?
-inline double operator/(double lhs, currency const& rhs)
-{
-    return lhs / rhs.d();
-}
-#endif // 0
+    {return lhs.d() / rhs;}
 
 inline std::ostream& operator<<(std::ostream& os, currency const& z)
 {
-    os << z.d(); return os;
+    return os << z.d();
 }
 
 inline std::vector<double> doubleize(std::vector<currency> const& z)
@@ -141,5 +116,13 @@ inline std::vector<double> doubleize(std::vector<currency> 
const& z)
     return r;
 }
 
+/// Zero cents--akin to a user-defined literal.
+///
+/// UDLs seem less convenient because the obvious "0_c" is likely to
+/// collide with some other UDL, and "currency::0_c" is too verbose.
+/// "0_cents" may avoid both those problems, but "C0" is terser.
+/// "C0" is chosen instead of "c0" only for the pixilated reason that
+/// the capital letter looks kind of like a "0".
+
 inline constexpr currency C0 = {};
 #endif // currency_hpp



reply via email to

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