floating point - Matlab - "0:.1:1" unexpected precision behaviour -
possible duplicate:
matlab gives wrong answer
can explain me why following happens, when use 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 veca(4) not equal 0.3? look quite 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 problem precision here? or have problem in understanding?
computers binary, native floating-point format can't store decimal fractions. (you use ratio type, or fixed-point decimal type, computations using these far slower.)
as consequence, testing floating-point values equality practically useless. check absolute value of difference instead.
you should read what every computer scientist should know floating point arithmetic
(there simpler explanations, such http://floating-point-gui.de/ should use these understand goldberg's paper, not replace it)
what seeing in case 0.2 + 0.1 != 0.3
(the range uses first version, veca(3) = veca(2) + step
)
Comments
Post a Comment