Search code examples
maple

Compare Two Floats Not Working Maple 18


I am trying to compare two floating point numbers and if it's greater, set the tempdiffer to differ, but it is always ending up with the last function value. This is pretty basic, but it is not setting differ to the highest value. Can anyone point me in the right direction?

with(orthopoly):
chebpade(BesselJ(1, x), x = -1 .. 1, [3, 4]):
Ttest := subs(T = orthopoly[T], chebpade(BesselJ(0, x), x = -1 .. 1, [3, 4])):
plot([Ttest, BesselJ(0, x)], x = -1 .. 1);
b := evalf(subs(x = -1, BesselJ(0,x))):
t := evalf(subs(x = -1, Ttest)):
differ := abs(t-b/t):
for i from -1 by 1 to 1 do 
    b := evalf(subs(x = i, BesselJ(0,x))):
    t := evalf(subs(x = i, Ttest)):
    tempdiffer := abs(t-b/t):
    if tempdiffer > differ then
        differ := tempdiffer;
    end if:
end do;

Solution

  • You need parens around t-b.

    You are computing abs(t - (b / t)), which gives you t give or take a small fraction (assuming b and t are nearly equal). What you want is abs((t - b) / t), which will give you their difference as a fraction of t.