subtract two identical float number it gives a very small number not a zero
이전 댓글 표시
suposed I have matrix A
A =
0.9500 0 0
0.9500 0 0
0.9500 0 0
0.9500 0 0
0.9500 0 0
0.9500 0 0
0.9500 0 0
0.9500 0 0
and I calculate the mean of A as
AA =mean(A)
AA =
0.9500 0 0
When I do that AA(1,1) - AA(1)
ans =
-1.1102e-16
Why I get this number and not zero ?? Please could you please help?
답변 (3개)
Roger Stafford
2014년 12월 8일
Matlab's double precision numbers are in binary form, and therefore cannot represent the quantity 0.9500 exactly. Using binary notation the closest it could come to that number would be:
.11110011001100110011001100110011001100110011001100110
since it is restricted to a maximum significand (mantissa) of 53 bits. The exact binary expression for 0.9500 would be an infinitely long string analogous to, say, the infinite string for 1/3 in decimal: 1/3 = 0.33333333333333333 ....
When eight such inexact numbers representing 0.9500 are added together one-at-a-time with rounding to 53 bits at each step and the sum divided by 8 the accumulated error is sufficient to show up as a difference in the least significant bit:
.11110011001100110011001100110011001100110011001100111
That least significant bit has a value of 1.1102e-16, which is what you observed.
David Young
2014년 12월 8일
0 개 추천
Image Analyst
2014년 12월 8일
0 개 추천
Additional info in the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!