subtract two identical float number it gives a very small number not a zero
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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?
0 comentarios
Respuestas (3)
Roger Stafford
el 8 de Dic. de 2014
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.
0 comentarios
Image Analyst
el 8 de Dic. de 2014
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
0 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!