Skip to main content

programming - Dr. StrangeNumbers or: How I Learned to Stop Worrying and Love Floating Point Arithmetic


The following is the program.


test[t_, dt_] := 

Module[{}, For[ti = dt, ti <= t, ti = ti + dt, Print[ti];];
Print[MemberQ[{0.01, 0.02}, ti]]; Return[0];]
test[0.01, 0.001]

(* 0.001
...
False
0 *)

Obviously the result is wrong. Copy the above result, you will be surprised to see:



(* ...
0.009000000000000001`
0.010000000000000002`
... *)

Why is this so?




Comments