lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] valyuta/004 9e6c7e1 4/9: Abjure converting currency


From: Greg Chicares
Subject: [lmi-commits] [lmi] valyuta/004 9e6c7e1 4/9: Abjure converting currency ctors
Date: Tue, 29 Dec 2020 14:46:02 -0500 (EST)

branch: valyuta/004
commit 9e6c7e18a748cd3a987aa978ed96758c7a720ffc
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Abjure converting currency ctors
---
 currency.hpp      | 17 +++++++++++++++--
 currency_test.cpp |  4 +++-
 loads_test.cpp    |  8 ++++----
 3 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/currency.hpp b/currency.hpp
index a745946..d4ca84e 100644
--- a/currency.hpp
+++ b/currency.hpp
@@ -84,12 +84,25 @@ class currency
     ~currency() = default;
 
     // The extra, ignored argument ensures that this special-purpose
-    // ctor ambiguates no other.
+    // ctor ambiguates no other. (But that doesn't matter now that
+    // there is no other non-default ctor.)
     explicit currency(data_type c, raw_cents) : m_ {c} {}
-    explicit currency(double    d)            : m_ {from_double(d)} {}
+// Converting ctors are preferably avoided:
 #if 0 // Apparently unused.
     explicit currency(int       i)            : m_ {from_int   (i)} {}
 #endif // 0
+// The underlying value 'm_' is intended to have an exact integral
+// value. A naive conversion from (e.g.) 3.14 would violate that
+// eminently desirable postcondition; a more complicated one would
+// either require cumbersome extra arguments to specify rounding, or
+// use some built-in rounding rule that would be inappropriate in
+// some circumstance. It seems better to avoid a converting ctor.
+//
+// Conversion from (e.g.) 3.5 would yield exactly 350 cents,
+// satisfying the integral postcondition, but would require a runtime
+// test that in practice would succeed so seldom that the idea is not
+// worth pursuing.
+//  explicit currency(double    d)            : m_ {from_double(d)} {}
 
     currency& operator=(currency const&) = default;
 
diff --git a/currency_test.cpp b/currency_test.cpp
index e0c36f3..e54d271 100644
--- a/currency_test.cpp
+++ b/currency_test.cpp
@@ -57,7 +57,9 @@ void currency_test::test_something()
 // Figure out what to do about this:
 //  currency a1(3.14);
 
-    currency a1(3.25);
+// It seems best to provide no converting ctor:
+//  currency a1(3.25);
+    currency a1(325, raw_cents{});
     BOOST_TEST(3.25 == a1.d());
     BOOST_TEST( 325 == a1.m_);
     a1 += a1;
diff --git a/loads_test.cpp b/loads_test.cpp
index 39bbd12..b6cab2c 100644
--- a/loads_test.cpp
+++ b/loads_test.cpp
@@ -86,8 +86,8 @@ void LoadsTest::Reinitialize()
     loads_.refundable_sales_load_proportion_    = std::vector<double>  
(length, 0.50000);
     loads_.dac_tax_load_                        = std::vector<double>  
(length, 0.00500);
 
-    loads_.monthly_policy_fee_   [mce_gen_guar] = 
std::vector<currency>(length, currency(8.00000));
-    loads_.annual_policy_fee_    [mce_gen_guar] = 
std::vector<currency>(length, currency(2.00000));
+    loads_.monthly_policy_fee_   [mce_gen_guar] = 
std::vector<currency>(length, currency(800, raw_cents{}));
+    loads_.annual_policy_fee_    [mce_gen_guar] = 
std::vector<currency>(length, currency(200, raw_cents{}));
     loads_.specified_amount_load_[mce_gen_guar] = std::vector<double>  
(length, 0.00003);
     loads_.separate_account_load_[mce_gen_guar] = std::vector<double>  
(length, 0.00130);
     loads_.target_premium_load_  [mce_gen_guar] = std::vector<double>  
(length, 0.04000);
@@ -95,8 +95,8 @@ void LoadsTest::Reinitialize()
     loads_.target_sales_load_    [mce_gen_guar] = std::vector<double>  
(length, 0.30000);
     loads_.excess_sales_load_    [mce_gen_guar] = std::vector<double>  
(length, 0.15000);
 
-    loads_.monthly_policy_fee_   [mce_gen_curr] = 
std::vector<currency>(length, currency(5.25000));
-    loads_.annual_policy_fee_    [mce_gen_curr] = 
std::vector<currency>(length, currency(1.00000));
+    loads_.monthly_policy_fee_   [mce_gen_curr] = 
std::vector<currency>(length, currency(525, raw_cents{}));
+    loads_.annual_policy_fee_    [mce_gen_curr] = 
std::vector<currency>(length, currency(100, raw_cents{}));
     loads_.specified_amount_load_[mce_gen_curr] = std::vector<double>  
(length, 0.00002);
     loads_.separate_account_load_[mce_gen_curr] = std::vector<double>  
(length, 0.00110);
     loads_.target_premium_load_  [mce_gen_curr] = std::vector<double>  
(length, 0.02000);



reply via email to

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