# Floating point number fractions excercise

During my preparation for an exam, I've come across an exercise that was focused on precision calculation with floating point numbers. It goes like this:

Consider the expression : $$\frac{1}{1-x}-\frac{1}{1+x}, x\neq \pm1$$ For what range of values $$x$$ is it difficult to compute the expression accurately in foating-point arithmetic? Give a re-arrangement of the terms such that, for the range of values from part (a), the computation is more accurate in foating-point arithmetic.

I have some basics covered on floating point number systems, so I've tried to set $$\pm x=0.9999...$$, with a different number of decimal digits. The calculations are indeed more and more imprecise, however I don't know if this is the only problematic 'range'. Also, I have no idea how to transform this expression to be more accurate. I have tried $$\frac{2x}{1-x^{2}}$$ and $$e^{\ln{2x}-\ln(1-x^{2})}$$, but the results were pretty much the same.

Thanks for help!

## 1 Answer

Actually, floating point arithmetic according to the IEEE 754 standard will calculate 1-x exactly, without any rounding error, if 0.5 ≤ x ≤ 2.0. So for x close to 1, 1-x is calculated exactly, 1 / (1-x) is calculated accurately, and 1 / (1 + x) and the sum will be calculated accurately as well.

What you are likely observing is the rounding error when 0.999999 for example is converted to a floating point number. x will not be equal to 0.999999, but a bit off. And the function itself as a function of real numbers is very sensitive to changes in the argument. That also explains why re-arranging the function can't help, because the function is sensitive to the arguments, and the arguments are not what you think.

The same applies for x close to -1, since f(x) = f(-x).

You will actually have a problem if x is close to 0. 1 / (1-x) and 1 / (1+x) can both be calculated with a very small absolute error, the difference is calculated exactly because both have about the same size, but because the result is close to zero, the small absolute errors turn into a rather large relative error.

When x is close to zero, 1 / (1 - x) = 1 + x + x^2 + x^3 ..., and a / (1 + x) = 1 - x + x^2 - x^3 ..., the mathematical difference is 2x + 2x^3 + 2x^5 ... ≈ 2x.

Let u be the value of the last bit in a number 1 ≤ x < 2, then IEEE arithmetic has rounding errors up to u/2. 1 / (1 - u) ≥ 1 is calculated with an error up to u/2, 1 / (1 + u) has an error up to u/4. If you pick x^2 slightly larger than u/4, then 1 / (1 - x) is rounded to 1+x, 1 / (1 + x) is rounded to 1 - x + u/4, and the difference that is calculated is 2x - u/4, instead of 2x + x^3 + 2x^5 ..., so the absolute error is > u/4, and the relative error about $$(u/4)^{1/2}$$.

You can fix this easily by subtracting 1 from both sides, giving $$x / (1 - x) + x / (1 + x)$$.