lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 776f09c 15/22: Return a struct rather than a


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 776f09c 15/22: Return a struct rather than a std::pair
Date: Sun, 6 Jun 2021 21:38:02 -0400 (EDT)

branch: master
commit 776f09c320e81842999629a91ad8866d4d54b0d3
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Return a struct rather than a std::pair
    
    A struct is extensible, and the names of its members can convey meaning.
---
 calendar_date.cpp |  4 ++--
 financial.hpp     |  4 ++--
 gpt_specamt.cpp   |  4 ++--
 ihs_avsolve.cpp   |  6 +++---
 solve.cpp         |  6 +++---
 zero.hpp          | 19 ++++++++++--------
 zero_test.cpp     | 60 +++++++++++++++++++++++++++----------------------------
 7 files changed, 53 insertions(+), 50 deletions(-)

diff --git a/calendar_date.cpp b/calendar_date.cpp
index 85f51d9..54ebd2b 100644
--- a/calendar_date.cpp
+++ b/calendar_date.cpp
@@ -751,8 +751,8 @@ class birthdate_limit
             ,0
             ,*this
             );
-        LMI_ASSERT(root_is_valid == z.second);
-        int j = bourn_cast<int>(z.first);
+        LMI_ASSERT(root_is_valid == z.validity);
+        int j = bourn_cast<int>(z.root);
         j = std::min(j, as_of_date_.julian_day_number());
         j = std::max(j, a_priori_minimum_);
         j = std::min(j, a_priori_maximum_);
diff --git a/financial.hpp b/financial.hpp
index de7b2ec..daed2df 100644
--- a/financial.hpp
+++ b/financial.hpp
@@ -122,10 +122,10 @@ class irr_helper
             ,decimals_
             ,*this
             );
-        switch(z.second)
+        switch(z.validity)
             {
             case root_is_valid:
-                {return z.first;}
+                {return z.root;}
             // Return -100% if NPVs of a priori bounds have same sign.
             case root_not_bracketed:
                 {return -1.0L;}
diff --git a/gpt_specamt.cpp b/gpt_specamt.cpp
index d2cf144..1facf08 100644
--- a/gpt_specamt.cpp
+++ b/gpt_specamt.cpp
@@ -170,10 +170,10 @@ currency gpt_specamt::CalculateSpecAmt
     // Because it is implausible that the upper bound is too low,
     // failure in practice implies that the solution would be lower
     // than the product minimum--in which case, return that minimum.
-    switch(solution.second)
+    switch(solution.validity)
         {
         case root_is_valid:
-            {return a_Values.round_specamt().c(solution.first);}
+            {return a_Values.round_specamt().c(solution.root);}
         case root_not_bracketed:
             {return a_Values.min_issue_spec_amt();}
         case improper_bounds:
diff --git a/ihs_avsolve.cpp b/ihs_avsolve.cpp
index a18cdd5..c6c5e77 100644
--- a/ihs_avsolve.cpp
+++ b/ihs_avsolve.cpp
@@ -457,7 +457,7 @@ currency AccountValue::Solve
         ,solve_helper
         ,os_trace
         );
-    currency const solution_cents = round_minutiae().c(solution.first);
+    currency const solution_cents = round_minutiae().c(solution.root);
 
     Solving = false;
 
@@ -468,14 +468,14 @@ currency AccountValue::Solve
     // now, and actual values are freshly generated downstream.
     (this->*solve_set_fn)(solution_cents);
 
-    switch(solution.second)
+    switch(solution.validity)
         {
         case root_is_valid:
             {} // Do nothing.
             break;
         case root_not_bracketed:
             {
-            LMI_ASSERT(0.0 == solution.first);
+            LMI_ASSERT(0.0 == solution.root);
             // Don't want this firing continually in census runs.
             if(!SolvingForGuarPremium)
                 {
diff --git a/solve.cpp b/solve.cpp
index 10625af..0a0f60d 100644
--- a/solve.cpp
+++ b/solve.cpp
@@ -330,7 +330,7 @@ currency AccountValue::Solve()
         ,SolveFn
         ,status()
         );
-    currency const solution_cents = round_to_cents.c(solution.first);
+    currency const solution_cents = round_to_cents.c(solution.root);
 
     Solving = false;
 
@@ -347,14 +347,14 @@ currency AccountValue::Solve()
     // now, and actual values are freshly generated downstream.
     SolveFn(dblize(solution_cents));
 
-    switch(solution.second)
+    switch(solution.validity)
         {
         case root_is_valid:
             {} // Do nothing.
             break;
         case root_not_bracketed:
             {
-            LMI_ASSERT(0.0 == solution.first);
+            LMI_ASSERT(0.0 == solution.root);
             warning() << "solution not found. Using zero instead." << 
LMI_FLUSH;
             }
             break;
diff --git a/zero.hpp b/zero.hpp
index 81a1828..be1e7c6 100644
--- a/zero.hpp
+++ b/zero.hpp
@@ -31,7 +31,6 @@
 #include <cmath>                        // fabs(), pow()
 #include <limits>
 #include <ostream>
-#include <utility>                      // pair
 
 enum root_bias
     {bias_none   // Return root z with f(z) closest to 0.0 .
@@ -53,7 +52,11 @@ enum root_validity
     ,improper_bounds
     };
 
-typedef std::pair<double,root_validity> root_type;
+struct root_type
+{
+    double        root     {0.0};
+    root_validity validity {improper_bounds};
+};
 
 namespace detail
 {
@@ -274,27 +277,27 @@ root_type decimal_root
 
     if(a == b)
         {
-        return std::make_pair(a, improper_bounds);
+        return {a, improper_bounds};
         }
 
     double fa = static_cast<double>(f(a));
     detail::expatiate(os_trace, n_iter++, technique, a, fa);
     if(0.0 == fa) // Note 0.
         {
-        return std::make_pair(a, root_is_valid);
+        return {a, root_is_valid};
         }
 
     double fb = static_cast<double>(f(b));
     detail::expatiate(os_trace, n_iter++, technique, b, fb);
     if(0.0 == fb) // Note 0 [bis].
         {
-        return std::make_pair(b, root_is_valid);
+        return {b, root_is_valid};
         }
 
     // f(a) and f(b) must have different signs.
     if((0.0 < fa) == (0.0 < fb))
         {
-        return std::make_pair(0.0, root_not_bracketed);
+        return {0.0, root_not_bracketed};
         }
 
     double fc = fb; // Note 1.
@@ -325,11 +328,11 @@ root_type decimal_root
                 ||  bias_higher == bias && 0.0 <= fb
                 )
                 {
-                return std::make_pair(b, root_is_valid);
+                return {b, root_is_valid};
                 }
             else if(std::fabs(m) <= 2.0 * epsilon * std::fabs(c) + t)
                 {
-                return std::make_pair(c, root_is_valid);
+                return {c, root_is_valid};
                 }
             else
                 {
diff --git a/zero_test.cpp b/zero_test.cpp
index c1b0e29..19ea45e 100644
--- a/zero_test.cpp
+++ b/zero_test.cpp
@@ -42,31 +42,31 @@ void test_zero(double bound0, double bound1, int dec, F f, 
double exact_root)
     root_type rl = decimal_root(bound0, bound1, bias_lower,  dec, f);
     root_type rh = decimal_root(bound0, bound1, bias_higher, dec, f);
 
-    LMI_TEST(root_is_valid == rn.second);
-    LMI_TEST(root_is_valid == rl.second);
-    LMI_TEST(root_is_valid == rh.second);
+    LMI_TEST(root_is_valid == rn.validity);
+    LMI_TEST(root_is_valid == rl.validity);
+    LMI_TEST(root_is_valid == rh.validity);
 
-    LMI_TEST(rl.first <= rn.first && rn.first <= rh.first);
+    LMI_TEST(rl.root <= rn.root && rn.root <= rh.root);
 
     double tol =
             std::pow(10.0, -dec)
         +   6.0 * epsilon * std::max
-                (std::fabs(rl.first), std::fabs(rh.first)
+                (std::fabs(rl.root), std::fabs(rh.root)
                 )
         ;
-    LMI_TEST(std::fabs(rh.first - rl.first) <= tol);
+    LMI_TEST(std::fabs(rh.root - rl.root) <= tol);
 
     double toll =
             std::pow(10.0, -dec)
-        +   6.0 * epsilon * std::fabs(rl.first)
+        +   6.0 * epsilon * std::fabs(rl.root)
         ;
-    LMI_TEST(std::fabs(rl.first - exact_root) <= toll);
+    LMI_TEST(std::fabs(rl.root - exact_root) <= toll);
 
     double tolh =
             std::pow(10.0, -dec)
-        +   6.0 * epsilon * std::fabs(rh.first)
+        +   6.0 * epsilon * std::fabs(rh.root)
         ;
-    LMI_TEST(std::fabs(rh.first - exact_root) <= tolh);
+    LMI_TEST(std::fabs(rh.root - exact_root) <= tolh);
 }
 
 double e_function(double z)
@@ -170,7 +170,7 @@ int test_main(int, char*[])
     // Test use with function.
 
     root_type r = decimal_root(0.5, 5.0, bias_none, 9, e_function);
-    LMI_TEST(root_is_valid == r.second);
+    LMI_TEST(root_is_valid == r.validity);
 
     // Same, with expatiation.
 
@@ -182,17 +182,17 @@ int test_main(int, char*[])
 
     e_functor e;
     r = decimal_root(0.5, 5.0, bias_none, 9, e);
-    LMI_TEST(root_is_valid == r.second);
+    LMI_TEST(root_is_valid == r.validity);
 
     // Test failure with improper interval.
 
     r = decimal_root(1.0, 1.0, bias_none, 9, e);
-    LMI_TEST(improper_bounds == r.second);
+    LMI_TEST(improper_bounds == r.validity);
 
     // Test failure with interval containing no root.
 
     r = decimal_root(0.1, 1.0, bias_none, 9, e);
-    LMI_TEST(root_not_bracketed == r.second);
+    LMI_TEST(root_not_bracketed == r.validity);
 
     // Test different biases.
 
@@ -201,22 +201,22 @@ int test_main(int, char*[])
     // bounds: neither can equal the unrepresentable true value.
 
     r = decimal_root(0.5, 5.0, bias_lower, 9, e);
-    LMI_TEST(root_is_valid == r.second);
-    double e_or_less = r.first;
+    LMI_TEST(root_is_valid == r.validity);
+    double e_or_less = r.root;
     LMI_TEST(e_or_less < std::exp(1.0));
 //  LMI_TEST(e.e_state < std::exp(1.0)); // Not necessarily true.
 
     r = decimal_root(0.5, 5.0, bias_higher, 9, e);
-    LMI_TEST(root_is_valid == r.second);
-    double e_or_more = r.first;
+    LMI_TEST(root_is_valid == r.validity);
+    double e_or_more = r.root;
     LMI_TEST(std::exp(1.0) < e_or_more);
 //  LMI_TEST(std::exp(1.0) < e.e_state); // Not necessarily true.
 
     LMI_TEST(e_or_less < e_or_more);
 
     r = decimal_root(0.5, 5.0, bias_none, 9, e);
-    LMI_TEST(root_is_valid == r.second);
-    double e_more_or_less = r.first;
+    LMI_TEST(root_is_valid == r.validity);
+    double e_more_or_less = r.root;
 
     LMI_TEST(e_more_or_less == e_or_less || e_more_or_less == e_or_more);
 
@@ -248,8 +248,8 @@ int test_main(int, char*[])
 
     e_nineteenth e_19;
     r = decimal_root(-1.0, 4.0, bias_none, 20, e_19);
-    LMI_TEST(root_is_valid == r.second);
-    LMI_TEST(std::fabs(r.first) <= epsilon);
+    LMI_TEST(root_is_valid == r.validity);
+    LMI_TEST(std::fabs(r.root) <= epsilon);
 
     double d = brent_zero(-1.0, 4.0, 1.0e-20, e_19);
     LMI_TEST(std::fabs(d) <= epsilon);
@@ -258,17 +258,17 @@ int test_main(int, char*[])
     LMI_TEST(-100.0 <= d && d <= -100.0 * (1.0 - 6.0 * epsilon));
 
     r = decimal_root(-100.0, 100.0, bias_none, 20, eq_2_1);
-    LMI_TEST(root_is_valid == r.second);
-    LMI_TEST(-100.0 <= r.first && r.first <= -100.0 * (1.0 - 6.0 * epsilon));
+    LMI_TEST(root_is_valid == r.validity);
+    LMI_TEST(-100.0 <= r.root && r.root <= -100.0 * (1.0 - 6.0 * epsilon));
 
     r = decimal_root(-1.0, 1.0, bias_none, 13, signum_offset);
-    LMI_TEST(root_is_valid == r.second);
-    LMI_TEST(materially_equal(-1.0 / 3.0, r.first));
+    LMI_TEST(root_is_valid == r.validity);
+    LMI_TEST(materially_equal(-1.0 / 3.0, r.root));
 
     e_former_rounding_problem e_frp;
     r = decimal_root(0.12609, 0.12611, bias_lower, 5, e_frp);
 #if !defined LMI_COMO_WITH_MINGW
-    LMI_TEST(materially_equal(0.12610, r.first));
+    LMI_TEST(materially_equal(0.12610, r.root));
 #else // defined LMI_COMO_WITH_MINGW
     // One would naively expect 0.12610 to be the answer, but it's
     // necessary to inquire which of the two closest representations
@@ -280,12 +280,12 @@ int test_main(int, char*[])
     // resulting in a final iterand whose function value is slightly
     // different from zero, and in the "wrong" direction.
     LMI_TEST
-        (   materially_equal(0.12609, r.first)
-        ||  materially_equal(0.12610, r.first)
+        (   materially_equal(0.12609, r.root)
+        ||  materially_equal(0.12610, r.root)
         );
 #endif // defined LMI_COMO_WITH_MINGW
 
-    LMI_TEST(root_is_valid == r.second);
+    LMI_TEST(root_is_valid == r.validity);
 
     return 0;
 }



reply via email to

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