diff --git a/projects/Math/97/org/apache/commons/math/analysis/BrentSolver.java b/projects/Math/97/org/apache/commons/math/analysis/BrentSolver.java index 766d87e..57f64a9 100644 --- a/projects/Math/97/org/apache/commons/math/analysis/BrentSolver.java +++ b/projects/Math/97/org/apache/commons/math/analysis/BrentSolver.java @@ -135,17 +135,31 @@ public double solve(double min, double max) throws MaxIterationsExceededExceptio // Verify bracketing double sign = yMin * yMax; - if (sign >= 0) { + if (sign > 0) { // check if either value is close to a zero + if (Math.abs(yMin) <= functionValueAccuracy) { + setResult(min, 0); + ret = min; + } else if (Math.abs(yMax) <= functionValueAccuracy) { + setResult(max, 0); + ret = max; + } else { // neither value is close to zero and min and max do not bracket root. throw new IllegalArgumentException ("Function values at endpoints do not have different signs." + " Endpoints: [" + min + "," + max + "]" + " Values: [" + yMin + "," + yMax + "]"); - } else { + } + } else if (sign < 0){ // solve using only the first endpoint as initial guess ret = solve(min, yMin, max, yMax, min, yMin); + } else { // either min or max is a root + if (yMin == 0.0) { + ret = min; + } else { + ret = max; + } } return ret;