lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master f9d13e6 8/8: Refactor


From: Greg Chicares
Subject: [lmi-commits] [lmi] master f9d13e6 8/8: Refactor
Date: Tue, 25 May 2021 20:11:00 -0400 (EDT)

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

    Refactor
---
 zero.hpp      | 46 +++++++++++++++++++---------------------------
 zero_test.cpp |  6 ++++++
 2 files changed, 25 insertions(+), 27 deletions(-)

diff --git a/zero.hpp b/zero.hpp
index fe0831a..c430d35 100644
--- a/zero.hpp
+++ b/zero.hpp
@@ -47,6 +47,22 @@ enum root_bias
 
 typedef std::pair<double,root_validity> root_type;
 
+namespace detail
+{
+inline void expatiate(int& n_iter, double x, double fx, std::ostream& os)
+{
+    if(os.good())
+        {
+        os
+            << "iteration " << n_iter++
+            << " iterand "  << x
+            << " value "    << fx
+            << std::endl
+            ;
+        }
+}
+} // namespace detail
+
 /// Return a zero z of a function f within input bounds [a,b].
 ///
 /// Precondition: either
@@ -232,30 +248,14 @@ root_type decimal_root
     double b = round_(bound1);
 
     double fa = static_cast<double>(f(a));
-    if(iteration_stream.good())
-        {
-        iteration_stream
-            << "iteration " << number_of_iterations++
-            << " iterand "  << a
-            << " value "    << fa
-            << std::endl
-            ;
-        }
+    detail::expatiate(number_of_iterations, a, fa, iteration_stream);
     if(0.0 == fa) // Note 0.
         {
         return std::make_pair(a, root_is_valid);
         }
 
     double fb = static_cast<double>(f(b));
-    if(iteration_stream.good())
-        {
-        iteration_stream
-            << "iteration " << number_of_iterations++
-            << " iterand "  << b
-            << " value "    << fb
-            << std::endl
-            ;
-        }
+    detail::expatiate(number_of_iterations, b, fb, iteration_stream);
     if(0.0 == fb) // Note 0 [bis].
         {
         return std::make_pair(b, root_is_valid);
@@ -378,15 +378,7 @@ root_type decimal_root
         else
             {
             fb = static_cast<double>(f(b));
-            if(iteration_stream.good())
-                {
-                iteration_stream
-                    << "iteration " << number_of_iterations++
-                    << " iterand "  << b
-                    << " value "    << fb
-                    << std::endl
-                    ;
-                }
+            detail::expatiate(number_of_iterations, b, fb, iteration_stream);
             }
         }
 }
diff --git a/zero_test.cpp b/zero_test.cpp
index 6642ad4..0d2b32b 100644
--- a/zero_test.cpp
+++ b/zero_test.cpp
@@ -122,6 +122,12 @@ int test_main(int, char*[])
     root_type r = decimal_root(0.5, 5.0, bias_none, 9, e_function);
     LMI_TEST(root_is_valid == r.second);
 
+    // Same, with expatiation.
+
+    std::ostringstream oss;
+    r = decimal_root(0.5, 5.0, bias_none, 9, e_function, oss);
+    std::cout << oss.str() << std::endl;
+
     // Test use with function object.
 
     e_functor e;



reply via email to

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