Possible Duplicate:
Matlab gives wrong answer
Can anyone explain to me why the following happens, when I use the 0:.1:1-range function?
>> vecA = 0:.1:1;
>> vecB = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1];
>> vecA == vecB
ans =
1 1 1 0 1 1 1 1 1 1 1
Why is vecA(4) not equal to 0.3? They look quite the same 😉
vecA =
Columns 1 through 7
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000
Columns 8 through 11
0.7000 0.8000 0.9000 1.0000
>> vecB
vecB =
Columns 1 through 7
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000
Columns 8 through 11
0.7000 0.8000 0.9000 1.0000
I think there is a problem with the precision here? Or do I have a problem in my understanding?
Computers are binary, their native floating-point format can’t exactly store decimal fractions. (You could use a ratio type, or a fixed-point decimal type, but computations using these are far slower.)
As a consequence, testing floating-point values for equality is practically useless. Check the absolute value of the difference instead.
You should definitely read What Every Computer Scientist Should Know About Floating Point Arithmetic
(There are also some simpler explanations, such as http://floating-point-gui.de/ but you should use these to help you understand Goldberg’s paper, not to replace it)
What you are actually seeing in this case is that
0.2 + 0.1 != 0.3(the range uses the first version,vecA(3) = vecA(2) + step)