# HG changeset patch # User Rik # Date 1452996875 28800 # Node ID 323e92c4589f7a47d67033e689e61dfa510cf911 # Parent 8fdf1f991a32ae184561187d113e104d17897f02 fzero.m: Correctly choose tolerance (eps) based on class of fun and X0 (bug #46658). * fzero.m: Check whether X0 or the result of the evaluation of the function fun results in a variable of class "single". Use an eps which matches X0 or the function evaluation. diff -r 8fdf1f991a32 -r 323e92c4589f scripts/optimization/fzero.m --- a/scripts/optimization/fzero.m Tue Jan 05 12:05:01 2016 -0800 +++ b/scripts/optimization/fzero.m Sat Jan 16 18:14:35 2016 -0800 @@ -137,7 +137,6 @@ nfev = 0; x = fval = a = fa = b = fb = NaN; - eps = eps (class (x0)); ## Prepare... a = x0(1); @@ -194,6 +193,12 @@ u = b; fu = fb; endif + if (isa (x0, "single") || isa (fa, "single")) + macheps = eps ("single"); + else + macheps = eps ("double"); + endif + d = e = u; fd = fe = fu; mba = mu*(b - a); @@ -201,7 +206,7 @@ switch (itype) case 1 ## The initial test. - if (b - a <= 2*(2 * abs (u) * eps + tolx)) + if (b - a <= 2*(2 * abs (u) * macheps + tolx)) x = u; fval = fu; info = 1; break; @@ -266,7 +271,7 @@ endswitch ## Don't let c come too close to a or b. - delta = 2*0.7*(2 * abs (u) * eps + tolx); + delta = 2*0.7*(2 * abs (u) * macheps + tolx); if ((b - a) <= 2*delta) c = (a + b)/2; else @@ -320,7 +325,7 @@ else u = b; fu = fb; endif - if (b - a <= 2*(2 * abs (u) * eps + tolx)) + if (b - a <= 2*(2 * abs (u) * macheps + tolx)) info = 1; break; endif @@ -337,7 +342,7 @@ ## Check solution for a singularity by examining slope if (info == 1) if ((b - a) != 0 - && abs ((fb - fa)/(b - a) / slope0) > max (1e6, 0.5/(eps+tolx))) + && abs ((fb - fa)/(b - a) / slope0) > max (1e6, 0.5/(macheps+tolx))) info = -5; endif endif