I ran into a problem with Ted Ersek's RootSearch (https://library.wolfram.com/infocenter/Demos/4482/) while solving the problem described here. RootSearch sometimes hangs after reporting that a number is less than the smallest number allowed in the Wolfram Mathematica. The reason turned out to be the internal function Ulp2. It entered into an infinite loop if its first argument x1 was equal to 0. (That's right: zero with a dot). Here is the corrected version of this function:
Ulp2=Function[{x1,x2},
Module[{eps,sgn=Sign2[x2-x1],tol},
(*Print["RootSearch Ulp2: sgn=",sgn,", x1=",x1,", x2=",x2];*)
Which[
(*test1*)sgn==0,
(*Print["RootSearch Ulp2-0: sgn=",sgn,", x1=",x1,", x2=",x2];*)
Message[Ulp2::fail];Throw[False],
(*test2*)MachineNumberQ[x1],
(*Print["***RootSearch Ulp2-test2: MachineNumberQ[x1]=",MachineNumberQ[x1]];*)
(*tol=$MachineEpsilon*Max[Abs[x1],$MinMachineNumber]/2;*)
tol=Max[$MachineEpsilon*Abs[x1],$MinMachineNumber];
(*Print["***RootSearch Ulp2-test2: tol=",NumberForm[tol,{40,36}]];*)
While[(eps=Plus@@{x1+sgn*tol,-x1})==0.0,tol*=2];
(*Print["***RootSearch Ulp2-test2: eps=",eps];*)
Abs[eps],
(*test3*)True,
(*Print["***RootSearch Ulp2-test3: sgn=",sgn];*)
Block[{$MinPrecision=0},
tol=SetPrecision[16*10^-Accuracy[x1],20];
While[(eps=Plus@@{x1+sgn*tol,-x1};Precision[eps]<=0),
tol*=2];
SetPrecision[Abs[eps],20]
]
]
]];
Diagnostics came from the line
tol=$MachineEpsilon*Max[Abs[x1],$MinMachineNumber]/2;
The tol paramter was evaluated to 0 in this line so the next line enters to endless cycle:
While[(eps=Plus@@{x1+sgn*tol,-x1})==0.0,tol*=2];
I have changed definition of tol as follows:
tol=Max[$MachineEpsilon*Abs[x1],$MinMachineNumber];
which seems to me more adequate. Can someone propose a more robust solution?
RootSearchis not part of Mathematica itself, then please include a link, otherwise everyone interested will have to use a search engine. Also, I understand the tag "bugs" to be for bugs in Mathematica itself, but I am just a random user. – user293787 Jul 03 '22 at 06:54$MachineEpsilon*$MinMachineNumberwhich produces a warning message, so I agree that that does not make much sense. But numerical code always has edge cases, even more so if it is by a third party. So if your solution works, and you can validate it, why not run with it. – user293787 Jul 03 '22 at 08:11