This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git


The following commit(s) were added to refs/heads/master by this push:
     new 20f1db2  Avoid underflow computing the initial value
20f1db2 is described below

commit 20f1db2f96d05e7b1889bf1e9b2fdc57d3da5c7a
Author: aherbert <[email protected]>
AuthorDate: Thu Dec 16 14:08:36 2021 +0000

    Avoid underflow computing the initial value
    
    This is an extreme edge case for the solver called with no bracketing.
    It ensures the bracket can be used to return a value if within the
    current function accuracy tolerance.
---
 .../commons/numbers/rootfinder/BrentSolver.java    |  7 +++++-
 .../numbers/rootfinder/BrentSolverTest.java        | 26 ++++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git 
a/commons-numbers-rootfinder/src/main/java/org/apache/commons/numbers/rootfinder/BrentSolver.java
 
b/commons-numbers-rootfinder/src/main/java/org/apache/commons/numbers/rootfinder/BrentSolver.java
index 1cf8a1f..ad58019 100644
--- 
a/commons-numbers-rootfinder/src/main/java/org/apache/commons/numbers/rootfinder/BrentSolver.java
+++ 
b/commons-numbers-rootfinder/src/main/java/org/apache/commons/numbers/rootfinder/BrentSolver.java
@@ -74,7 +74,12 @@ public class BrentSolver {
                            double min,
                            double max) {
         // Avoid overflow computing the initial value: 0.5 * (min + max)
-        return findRoot(func, min, 0.5 * min + 0.5 * max, max);
+        // Note: This sum is invalid if min == max == Double.MIN_VALUE
+        // so detect this edge case. It will raise a bracketing exception
+        // if min is not the root within the configured function accuracy;
+        // otherwise min is returned.
+        final double initial = min == max ? min : 0.5 * min + 0.5 * max;
+        return findRoot(func, min, initial, max);
     }
 
     /**
diff --git 
a/commons-numbers-rootfinder/src/test/java/org/apache/commons/numbers/rootfinder/BrentSolverTest.java
 
b/commons-numbers-rootfinder/src/test/java/org/apache/commons/numbers/rootfinder/BrentSolverTest.java
index 234fe48..07226d3 100644
--- 
a/commons-numbers-rootfinder/src/test/java/org/apache/commons/numbers/rootfinder/BrentSolverTest.java
+++ 
b/commons-numbers-rootfinder/src/test/java/org/apache/commons/numbers/rootfinder/BrentSolverTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.numbers.rootfinder;
 
+import java.util.Locale;
 import java.util.function.DoubleUnaryOperator;
 
 import org.junit.jupiter.api.Assertions;
@@ -315,4 +316,29 @@ class BrentSolverTest {
         final double result3 = solver.findRoot(func3, -Double.MAX_VALUE, 
Double.MAX_VALUE);
         Assertions.assertEquals(target3, result3, Math.ulp(target3));
     }
+
+    @Test
+    void testMinValueNoBracket() {
+        final BrentSolver solver = new BrentSolver(DEFAULT_RELATIVE_ACCURACY,
+                                                   DEFAULT_ABSOLUTE_ACCURACY,
+                                                   DEFAULT_FUNCTION_ACCURACY);
+        final double min = Double.MIN_VALUE;
+
+        // No bracket
+        final DoubleUnaryOperator func1 = x -> 1;
+        SolverException ex1 = Assertions.assertThrows(SolverException.class,
+            () -> solver.findRoot(func1, min, min));
+        
Assertions.assertTrue(ex1.getMessage().toLowerCase(Locale.ROOT).contains("no 
bracket"));
+
+        // Still no bracket but the function is within the function accuracy 
of zero
+        final DoubleUnaryOperator func2 = x -> 1e-100;
+        Assertions.assertEquals(min, solver.findRoot(func2, min, min));
+
+        // Ensure the correct exception is raised for a bad bracket
+        final double lo = 2;
+        final double hi = 1;
+        SolverException ex2 = Assertions.assertThrows(SolverException.class,
+            () -> solver.findRoot(func1, lo, hi));
+        Assertions.assertEquals(String.format(SolverException.TOO_LARGE, lo, 
hi), ex2.getMessage());
+    }
 }

Reply via email to