subtract two identical float number it gives a very small number not a zero

9 visualizzazioni (ultimi 30 giorni)
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?

Risposte (3)

Roger Stafford
Roger Stafford il 8 Dic 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.

David Young
David Young il 8 Dic 2014
It's round-off error. See, for example this question.

Image Analyst
Image Analyst il 8 Dic 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by