I am a beginner studying scientific computation, more specifically floating point numbers and precision in matlab. When testing the outputs of 2 of the following equations, I am not sure how matlab computed the results and why it is computed this way.
y = @(x) (exp(x)-1-x)/x^2;
z = @(x) (exp(x)-x-1)/x^2;
x = 1e-10;
fprintf(’x=%.5e\n y=%.5e\n z=%.5e\n’, x, y(x), z(x))
Result:
x=1.00000e-10
y=8.27404e+02
z=0.00000e+00
As you can see, the only difference between y and z is that the numerator's order placement changed from $\exp(x)-1-x$ to $\exp(x)-x-1$. Is subtraction not being associative the reason why the result of y and z are different?
Why is z's result exactly 0 instead of a precise number? Is it because matlab's double precision arithmetic rounded it to 0?
Any clarification would be appreciated.
$x=10^{-10}$ has a "filled" mantissa in the binary floating-point encoding, as it is not a power of $2$.
As $x^2=10^{-20}$ is zero relative to $1$ in floating point precision, $\exp(x)$ is $1+x$ rounded to floating point precision (or 1 ulp larger - unit-in-the-last-place).
$\exp(x)-1$ returns thus a truncated version of $x$, essentially its leading 5 or 6 digits. Consequently $(\exp(x)-1)-x$ will return the remainder of $x$ to this truncation.
In $(\exp(x)-x)-1$ the floating point errors are undone in the reverse order that they were introduced, so the result is zero (remember that $\exp(x)=1+x$ at this level).