lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 28955d1 01/13: Refactor for flexibility


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 28955d1 01/13: Refactor for flexibility
Date: Sun, 4 Jul 2021 19:04:43 -0400 (EDT)

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

    Refactor for flexibility
    
    Factored out the rounding function, making it default to std::identity,
    and the tolerance, making it an argument, so that decimal_root() can be
    used with any tolerance and without any rounding.
    
    Documentation changes follow in a separate commit.
---
 zero.hpp | 36 ++++++++++++++++++++++++++++++++----
 1 file changed, 32 insertions(+), 4 deletions(-)

diff --git a/zero.hpp b/zero.hpp
index 988412d..e027a4f 100644
--- a/zero.hpp
+++ b/zero.hpp
@@ -29,6 +29,7 @@
 #include "round_to.hpp"
 
 #include <cmath>                        // fabs(), pow()
+#include <functional>                   // function(), identity()
 #include <iomanip>                      // setw()
 #include <limits>
 #include <ostream>
@@ -60,6 +61,11 @@ struct root_type
     int           n_iter   {0};
 };
 
+namespace detail
+{
+using RoundT = std::function<double(double)>;
+} // namespace detail
+
 /// Return a zero z of a function f within input bounds [a,b].
 ///
 /// Preconditions: bounds are distinct after rounding; and either
@@ -234,16 +240,15 @@ template<typename FunctionalType>
 root_type decimal_root
     (double          bound0
     ,double          bound1
+    ,double          tolerance
     ,root_bias       bias
-    ,int             decimals
     ,FunctionalType& f
+    ,detail::RoundT  round_dec = std::identity()
     ,std::ostream&   os_trace = null_stream()
     )
 {
     constexpr double        epsilon   {std::numeric_limits<double>::epsilon()};
 
-    round_to<double> const  round_dec {decimals, r_to_nearest};
-
     int                     n_iter    {0};
     interpolation_technique technique {interpolate_initialization};
 
@@ -275,7 +280,7 @@ root_type decimal_root
             ;
         };
 
-    double t = 0.5 * std::pow(10.0, -decimals);
+    double t = tolerance;
 
     a = round_dec(bound0);
     b = round_dec(bound1);
@@ -426,6 +431,29 @@ root_type decimal_root
         }
 }
 
+template<typename FunctionalType>
+root_type decimal_root
+    (double          bound0
+    ,double          bound1
+    ,root_bias       bias
+    ,int             decimals
+    ,FunctionalType& f
+    ,std::ostream&   os_trace = null_stream()
+    )
+{
+    round_to<double> const round_dec {decimals, r_to_nearest};
+
+    return decimal_root
+        (bound0
+        ,bound1
+        ,0.5 * std::pow(10.0, -decimals)
+        ,bias
+        ,f
+        ,detail::RoundT(round_dec)
+        ,os_trace
+        );
+}
+
 /// A C++ equivalent of Brent's algol60 original, for reference only.
 
 template<typename FunctionalType>



reply via email to

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