MATLAB: Different results for two equal functions

approximation errorround-offrounding

I am comparing these two functions:
f = sqrt(x.^2+1.0)-1.0
g = x.^2./(sqrt(x.^2+1.0)+1.0)
x = [8^-6 8^-7, 8^-8]
Even though f = g, I get slightly different values e.g. for x = 8^-7 I get f(x) = 1.136868377216 160 e-13 and g(x) = 1.136868377216 096 e-13.
I'm guessing this has something to do with the reduction of significant digits, however I don't know which results are more reliable. My guess is that function f = sqrt(x.^2+1.0)-1.0 is more precise because we only have one x in it but I am not sure…

Best Answer

The two functions you show algebraically yield identical values only for x = 0.