The code below does not work. Can anyone tell me why? Thanks!
For[t = -1, t <= 1, t = t + 0.01,
For[z = -1, z <= 1, z = z + 0.01, If[t*z == 0, Print[t, ", ", z]]]]
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityIt's the result of error accumulation of float number:
Nest[# + 0.01 &, -1, 100]
% == 0
(* 7.5287*10^-16 *)
(* False *)
There're many ways to fix this, for example, using arbitrary-precision number. In your case a Precision
of 1.
is enough:
0.01`1 // Precision
Nest[# + 0.01`1 &, -1, 100]
% == 0
(* 1. *)
(* 0.*10^-1 *)
(* True *)
Or simply use exact number:
Nest[# + 1/100 &, -1, 100]
% == 0
(* 0 *)
(* True *)