floating point arithmetics, mathlab's interpretation of a . after a number

1 visualización (últimos 30 días)
Regarding mathlab's reading of the . operator after a number I know that, for example, that if i introduce it when miltuplying two matrixes A.*B of the same dimentions Mathlab will yield a matrix that has multiplied each entry like so A[i,i]*B[i,i].
When introducing values
the following values:
h=1./2.;
x=2./3.-h;
y=3./5.-h;
e=(x+x+x)-h;
printing e yields -1.110223024625157e-16
but when I do x+x+x it yields = 0.5
Why doesn't e yield 0?
I know it has to do with something of the sort of the matrix problem but i do not know for certain and floating point arithmetic but i do not know for certain.

Respuesta aceptada

Stephen23
Stephen23 el 17 de Oct. de 2020
Editada: Stephen23 el 17 de Oct. de 2020
"Why doesn't e yield 0?"
Because most of your values cannot be exactly represented using binary floating point numbers.
The values stored in your computer's memory are only approximations of those values (to a finite precision), and that approximation means that they include some floating point error. So when you perform arithmetic operations on those values, sometimes that floating point error accumulates during the operation.. .and in some cases, that accumulation can cancel-out completely (important note: this cancelation does not change the precision of the output!). This is exactly what your two examples show: one of the operations happens to accumulate some error which remains in the output, another operation happens to cancel out the error so that the output looks like it is "exact" (in fact in both cases the output precision is much the same).
Lets have a look at the exact values that your computer is calculating with:
>> num2strexact(x)
ans =
0.16666666666666662965923251249478198587894439697265625
>> num2strexact(y)
ans =
9.999999999999997779553950749686919152736663818359375e-2
Learn about binary floating point numbers and how they behave:
This is worth reading as well:

Más respuestas (1)

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam el 17 de Oct. de 2020
Matlab sotere double numbers in 8 bytes (64 bits) . So if you do some aritmatic operations, there is an error due to the limits on the space for each number I you can see some small numbers instead of 0. The number 0.5 in binary format is exact and you can see 0.5 as a result. Also, it is not necessary to use . after numbers. 1. is the same as 1
  2 comentarios
Walter Roberson
Walter Roberson el 19 de Oct. de 2020
Side note: There are cases inside symbolic expressions where 1. is not the same as 1
For example,
>> subs(str2sym({'x^(1/2)', 'x^(1/2.)'}),x,3)
ans =
[ 3^(1/2), 1.7320508075688772935274463415059]
The 2. compared to plain 2 made the difference between treating the calculation as an exact system or as a floating-point calculation to be approximated.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by