I encounted the following precision issue. I define c = a + b, but the result for 'c - a - b' does not equal zero. I am not sure how to resolve this.
'''
In[1]:=
a = N[Exp[3]];
b = N[Exp[4]];
c = a + b;
c - a - b
Out[4]= 7.10543*10^-15
'''
I encounted the following precision issue. I define c = a + b, but the result for 'c - a - b' does not equal zero. I am not sure how to resolve this.
'''
In[1]:=
a = N[Exp[3]];
b = N[Exp[4]];
c = a + b;
c - a - b
Out[4]= 7.10543*10^-15
'''
This phenomenon occurs for simple cases too:
0.3 + 1.1 - 1.4
2.22045*10^-16`
These numbers do not have finite length in binary and so they're truncated when expressed as an inexact number. We can see this with RealDigits:
RealDigits[1.1, 2]
{{1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0}, 1}
The identity $1.1_{10} = 1.0001100110011_2...$ gets truncated to $53$ digits to the right of the decimal (on a $64$ bit machine). The same happens for $0.3$ and $1.4$ — and in fact only dyadic rationals can be represented exactly in floating point arithmetic.
We can see the error by expressing the truncated reals as rational numbers:
SetPrecision[0.3 + 1.1, ∞] - SetPrecision[1.4, ∞]
1/4503599627370496
N[%]
2.22045*10^-16
Chop[%]on the final output? does it become zero? see help onChopor you can use infinite precision.a = N[Exp[3], Infinity]; b = N[Exp[4], Infinity];and you will get exact zero without doingChop– Nasser Sep 27 '21 at 07:47N[Exp[3], Infinity]just keeps it asExp[3]and will not make it real number. Once you convert things to approximate numbers, you'll get approximate numbers as result of computation. – Nasser Sep 27 '21 at 07:56N[...]use machine precision (unless you give second argument toN). on same PC, I typed the same thing on Matlab, and got same output:a=exp(3); b=exp(4); c=a+b; c-a-bgives7.1054e-15This is the nature of using real numbers.c - (a + b)instead ofc - a - b. Now you will get0.as output. This is because the order makes difference in terms of real numbers computations (large number - small number, vs. difference of two numbers close to each others). seeWhat Every Computer Scientist Should Know About Floating-Point Arithmetic– Nasser Sep 27 '21 at 08:11