# Floating point operations — division vs multiplication; how do they affect accuracy?

Given two functions:

f[x_] := x ((x + 1)^(1/2) - x^(1/2))
g[x_] := x/((x + 1)^(1/2) + x^(1/2))


Which one is more accurate?

Side note: If you could explain why, that would really help me out, I have a feeling it's f(x) as there isn't a denominator but I'm not 100% certain.

• I believe accuracy would be the same for both division and multiplication, because the main culprit for loss of precision is adding (or subtracting) a very large number to a very small number, as opposed to adding two numbers of comparable size. Division and multiplication have near the same effect on precision, so there is no expected difference in precision between your f and g. – Manuel --Moe-- G Jan 29 '15 at 18:37
• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – user9660 Jan 29 '15 at 18:46
• More accurate at doing what ? – Sektor Jan 29 '15 at 18:49
• @Sektor Just when x=500. I know that f(x) can be re-arranged to be g(x) but when computed with a calculator, there are rounding errors. I'm not quite sure which one is more "accurate" – Claire Blackman Jan 29 '15 at 19:10
• Are you sure this question is about Mathematica and not Mathematics, in general ? – Sektor Jan 29 '15 at 19:24

ybeltukov showed empirically that $g(x)$ is numerically more precise than $f(x)$, despite the two expressions being formally mathematically equal. Why is this the case?

In your original question, you guessed that

If you could explain why, that would really help me out, I have a feeling it's f(x) as there isn't a denominator but I'm not 100% certain.

To explain why $g(x)$ is more accurate, recall Manuel --Moe-- G's comment:

...the main culprit for loss of precision is adding (or subtracting) a very large number to a very small number, as opposed to adding two numbers of comparable size. Division and multiplication have near the same effect

In the case of $f(x)$, for large values of $x$ we have $x$ (a large number), times $\sqrt{x+1}-\sqrt{x}$, which tends towards zero as $x\to\infty$. Thus, there is catastrophic loss of precision for large $x$, which is consistent with the first figure that ybeltukov's answer provided.

In contrast, $g(x)$ is the ratio of two large numbers whose sizes aren't that much different (or at least, become more different at a slower rate than in the case of $f(x)$).

### More formal explanation

First, examine $f(x)$. The first term $x$ asymptotically tends as $x$, whereas the second term $\sqrt{x+1}-\sqrt{x}$ asymptotically tends as $1/\sqrt{x}$:

Series[Sqrt[x + 1] - Sqrt[x], {x, Infinity, 2}]
(*Sqrt[1/x]/2 - 1/8 (1/x)^(3/2)*)


This means that the relative sizes of the terms diverge as $x^{3/2}$.

Now examine $g(x)$. The first term $x$ asymptotically tends as $x$, whereas the second term $\sqrt{x}+\sqrt{x+1}$ tends as $\sqrt{x}$:

Series[Sqrt[x + 1] + Sqrt[x], {x, Infinity, 2}]
(*2 Sqrt[x] + Sqrt[1/x]/2 - 1/8 (1/x)^(3/2)*)


This means that their relative sizes diverge as $x^{1/2}$, which is slower than in the case of $f(x)$. This means that $g(x)$ will be more accurate for large $x$.

You can compare the precision empirically:

prec[f_, x_] := Abs[SetPrecision[f[x], 30] - f[SetPrecision[x, 30]]]/
Abs@f[SetPrecision[x, 30]];

LogPlot[{prec[f, x], $MachineEpsilon}, {x, -10, 10}] LogPlot[{prec[g, x],$MachineEpsilon}, {x, -10, 10}] So g has better numerical precision (around \$MachineEpsilon). It is because f contains the difference of potentially big numbers.

• I love your prec function, but can you elaborate a bit on how does it actually work? – plasmacel Jun 4 '18 at 19:49