Strange result in subtraction operation

4 visualizaciones (últimos 30 días)
Marcio Coelho de Mattos
Marcio Coelho de Mattos el 11 de Jul. de 2022
Comentada: Marcio Coelho de Mattos el 11 de Jul. de 2022
I have made the following operation in Matlab: 4.238257316854621e+00 - 4.238257316854609e+00
The obvoius result is 1.12e-14, but the answer returned was 1.154631945610163e-14.
I know that math operations are affected by the limited precision of calculations (floating point representation) but this result is really surprising to me.
Can anyone provide some explanation? Is there any way to improve the accuracy of this calculation?

Respuestas (2)

Les Beckham
Les Beckham el 11 de Jul. de 2022
Editada: Les Beckham el 11 de Jul. de 2022
The "obvious" result is 1.2e-14 not 1.12e-14 (the different last two digits in your original numbers at the 14 decimal place give 2.1 - 0.9 = 1.2). So 1.154631945610163e-14 is within 4.5e-16 of the "obvious" result which is consistent with the expected floating point precision.

Jan
Jan el 11 de Jul. de 2022
Editada: Jan el 11 de Jul. de 2022
format long g
a = 4.238257316854621;
b = 4.238257316854609;
wanted = 1.12e-14;
got = a - b
got =
1.15463194561016e-14
drift = max(eps(a), eps(b)) % Expected error:
drift =
8.88178419700125e-16
got - wanted
ans =
3.46319456101627e-16
So the found result is inside the expected error range for IEEE754 doubles. Remember, that you cannot store 16 digits reliably:
digits(20)
a = vpa(4.238257316854621)
a = 
4.2382573168546207043
You see, the double value is not store accurately already. To do so, you need a char vector as input:
aa = vpa('4.238257316854621')
aa = 
4.238257316854621
No, there is no way to improve the accuracy except for increasing the precision, e.g. by using SYM objects or quadrupel precision.
bb = vpa('4.238257316854609')
bb = 
4.238257316854609
aa - bb
ans = 
0.000000000000011999999999999544971

Categorías

Más información sobre Numbers and Precision 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!

Translated by