lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 7bd1245 01/22: Add a unit test: root of anoth


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 7bd1245 01/22: Add a unit test: root of another unfriendly function
Date: Sun, 6 Jun 2021 21:37:58 -0400 (EDT)

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

    Add a unit test: root of another unfriendly function
---
 zero_test.cpp | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/zero_test.cpp b/zero_test.cpp
index 5657440..a5b2f94 100644
--- a/zero_test.cpp
+++ b/zero_test.cpp
@@ -131,6 +131,18 @@ double eq_2_1(double x)
         ;
 }
 
+/// A function for which bisection is optimal.
+///
+/// Return signum(argument + 1/3). Adding the constant makes it less
+/// likely that the root will be found by accident, e.g. between
+/// bounds such as (0,k) or (-k,k).
+
+double signum_offset(double d)
+{
+    double z = d + 1.0 / 3.0;
+    return (0.0 == z) ? 0.0 : std::signbit(z) ? -1.0 : 1.0;
+}
+
 // This problem once arose in a unit test for irr calculations.
 // Minimal test case:
 //
@@ -243,6 +255,10 @@ int test_main(int, char*[])
     LMI_TEST(root_is_valid == r.second);
     LMI_TEST(-100.0 <= r.first && r.first <= -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));
+
     e_former_rounding_problem e_frp;
     r = decimal_root(0.12609, 0.12611, bias_lower, 5, e_frp);
 #if !defined LMI_COMO_WITH_MINGW



reply via email to

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