lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 3975592 07/11: Expatiate more informatively


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 3975592 07/11: Expatiate more informatively
Date: Thu, 1 Jul 2021 20:19:04 -0400 (EDT)

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

    Expatiate more informatively
    
    Instead of {b,fb}, show {a,fa,b,fb,c,fc} for each iteration.
    
    Instead of specifying the highest possible precision, specify column
    widths, striving for a reasonable default width of 83. A width of 80
    would fit on a single Hollerith card, but an extra digit in each column
    seems even more valuable.
    
    Use only one header for the first two columns. The second column cannot
    satisfactorily be explained in a few characters.
    
    The first column was formerly an index-zero iteration ordinal, e.g.
    {0,1,2,3} for 4 iterations. Now it is instead an evaluation cardinal,
    e.g. {2,3,4} for 4 iterations. The first cardinal is 2 because computing
    f(a) and f(b) takes two evaluations. The last number is, usefully, the
    total number of evaluations; displaying {1,2,3} instead would suggest,
    misleadingly, that there were only three evaluations.
---
 zero.hpp | 46 +++++++++++++++++++++++++++++-----------------
 1 file changed, 29 insertions(+), 17 deletions(-)

diff --git a/zero.hpp b/zero.hpp
index 50a2b9d..988412d 100644
--- a/zero.hpp
+++ b/zero.hpp
@@ -28,8 +28,8 @@
 #include "null_stream.hpp"
 #include "round_to.hpp"
 
-#include <cfloat>                       // DECIMAL_DIG
 #include <cmath>                        // fabs(), pow()
+#include <iomanip>                      // setw()
 #include <limits>
 #include <ostream>
 
@@ -247,42 +247,54 @@ root_type decimal_root
     int                     n_iter    {0};
     interpolation_technique technique {interpolate_initialization};
 
-    os_trace.precision(DECIMAL_DIG);
+    os_trace
+        << "#eval"
+        << "            a           fa"
+        << "            b           fb"
+        << "            c           fc"
+        << '\n'
+        ;
 
-    os_trace << "iteration, technique, x, fx\n";
+    // Declarations must precede lambda.
+    double a  {};
+    double fa {};
+    double b  {};
+    double fb {};
+    double c  {};
+    double fc {};
 
-    auto expatiate = [&](double x, double fx)
+    auto expatiate = [&]()
         {
         os_trace
-            << n_iter
-            << ' ' << "IBLQb"[technique]
-            << ' ' << x
-            << ' ' << fx
+            <<        std::setw(3)  << n_iter
+            << ' '                  << "IBLQb"[technique]
+            << ' ' << std::setw(12) << a << ' ' << std::setw(12) << fa
+            << ' ' << std::setw(12) << b << ' ' << std::setw(12) << fb
+            << ' ' << std::setw(12) << c << ' ' << std::setw(12) << fc
             << std::endl
             ;
         };
 
     double t = 0.5 * std::pow(10.0, -decimals);
 
-    double a = round_dec(bound0);
-    double b = round_dec(bound1);
+    a = round_dec(bound0);
+    b = round_dec(bound1);
 
     if(a == b)
         {
         return {a, improper_bounds, n_iter};
         }
 
-    double fa = static_cast<double>(f(a));
-    expatiate(a, fa);
+    fa = static_cast<double>(f(a));
     ++n_iter;
     if(0.0 == fa) // Note 0.
         {
         return {a, root_is_valid, n_iter};
         }
 
-    double fb = static_cast<double>(f(b));
-    expatiate(b, fb);
+    fb = static_cast<double>(f(b));
     ++n_iter;
+    expatiate();
     if(0.0 == fb) // Note 0 [bis].
         {
         return {b, root_is_valid, n_iter};
@@ -294,8 +306,8 @@ root_type decimal_root
         return {0.0, root_not_bracketed, n_iter};
         }
 
-    double fc = fb; // Note 1.
-    double c = b;
+    fc = fb; // Note 1.
+    c = b;
     double d = b - a;
     double e = d;
 
@@ -408,8 +420,8 @@ root_type decimal_root
         else
             {
             fb = static_cast<double>(f(b));
-            expatiate(b, fb);
             ++n_iter;
+            expatiate();
             }
         }
 }



reply via email to

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