lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] odd/eraseme_error cf5cced 7/9: Re-break; fail better


From: Greg Chicares
Subject: [lmi-commits] [lmi] odd/eraseme_error cf5cced 7/9: Re-break; fail better
Date: Thu, 8 Jul 2021 16:26:48 -0400 (EDT)

branch: odd/eraseme_error
commit cf5cced10c2db4cd7c0a778ac5f11c24946f3783
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Re-break; fail better
    
    Temporarily reinstate the NaN-producing code. Catch it before it
    iterates an unreasonable number of times.
    
    In 'master', a better-crafted failing test will be wanted.
---
 financial.hpp | 16 +++++++++++++++-
 zero.hpp      |  8 +++++++-
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/financial.hpp b/financial.hpp
index 9e32f32..2525ff2 100644
--- a/financial.hpp
+++ b/financial.hpp
@@ -57,6 +57,7 @@ long double fv
     ,long double i
     )
 {
+#if 0
     if(first == last)
         {
         return 0.0L;
@@ -69,6 +70,19 @@ long double fv
         z *= u;
         }
     return z;
+#endif // 0
+    if(first == last)
+        {
+        return 0.0L;
+        }
+    long double const v = 1.0L / (1.0L + i);
+    long double vn = 1.0L;
+    long double z = 0.0L;
+    for(InputIterator j = first; j != last; ++j)
+        {
+        z += *j * (vn *= v);
+        }
+    return z / (vn * v);
 }
 
 template<typename InputIterator>
@@ -95,7 +109,7 @@ class irr_helper
     long double operator()()
         {
     std::ofstream ofs_trace;
-////ofs_trace.open("trace_irr.txt", ios_out_app_binary());
+    ofs_trace.open("trace_irr.txt", ios_out_app_binary());
     std::ostream os_trace(ofs_trace.rdbuf());
         root_type const z = decimal_root
             (*this
diff --git a/zero.hpp b/zero.hpp
index 8e8fc06..03dcda7 100644
--- a/zero.hpp
+++ b/zero.hpp
@@ -323,7 +323,11 @@ root_type lmi_root
     double d = b - a;
     double e = d;
 
-    for(;;)
+    // A 64-bit double can have no more than 2^64 distinct values, so
+    // bisection can find any one of them in 64 iterations, so Brent's
+    // argument on page 53 of AfMWD (3.4) shows that his method cannot
+    // take more than (64^2 - 2) iterations.
+    for(int n = 0; n < 4096; ++n)
         {
         if((0.0 < fb) == (0.0 < fc))
             {
@@ -459,6 +463,8 @@ root_type lmi_root
             expatiate();
             }
         }
+
+    throw("Zounds!");
 }
 
 /// Return a rounded zero z of a function f within input bounds [a,b].



reply via email to

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