Reduce error when $x$ is much smaller than 1 I have the following function $$\frac{1}{1+2x}-\frac{1-x}{1+x} $$
How to find equivalent way to compute it but when $x$ is much smaller than 1? I assume the problem here is with $1+x$ since it probably would be equal to 1. I don't know if multiplying by $(1-x)$ would be helpful as it would be
$$ \frac{1-x}{1+x-2x^2}-\frac{(1-x)^2}{1-x^2} $$ so there's still term $1+x$.
 A: The formula as written has numerically acceptable behavior in the vicinity of the singularities. Outside this region it can be transformed algebraically into the rational function $\frac{2x^{2}}{2x^{2}+3x+1}$. But in this form, there is an issue with premature overflow in intermediate computation for $x$ large in magnitude. This can be avoided by dividing the numerator and denominator by $2x$. The most advantageous switchover points between the two computations can be estimated, supported by a few experiments. In summary:
For $x$ in $[-\frac{3}{2}, -\frac{11}{32}]$: $$f(x) := \frac{1}{2x+1} - \frac{1-x}{1+x}$$ else: $$f(x) := \frac{x}{(\frac{3}{2} + \frac{1}{2x}) + x}$$
Note evaluation order of additions in the denominator is indicated by parenthesis. When evaluated with IEEE-754 floating-point arithmetic, using fairly extensive testing, the maximum error in the positive half-plane was found to be less than 3 ulp, while the maximum error in the negative half-plane was found to be less than 5 ulp.
A: I think the purpose of the exercise is simply to provide an alternative, but equivalent, expression for $f$. For this reason, I would rule out power series approximations (they do the job quite well but strictly speaking, they are not equivalent to the original expression). If you simply reduce to the same denominator, you no longer have a subtractive cancellation ruining the relative error and an acceptable (although not optimal) possibility would simply be
$$
f(x)=\dfrac{2x^2}{(1+2x)(1+x)}.
$$
A: If the therms in $x^2$ can be neglegted:
$$\frac{1}{1+2x}\approx\frac{1-4x^2}{1+2x}=1-2x$$
$$\frac{1-x}{1+x}\approx\frac{(1-x)(1-x^2)}{1+x}=(1-x)^2\rightarrow$$
$$f(x)\approx1-2x-1+2x-x^2=-x^2$$
If therm in $x^3$ can be neglegted:
$$\frac{1}{1+2x}\approx\frac{1-8x^3}{1+2x}=1-2x+4x^2$$
$$\frac{1}{1+x}\approx\frac{1-x^3}{1+x}=1-x+x^2$$
And so on...
A: You can replace the fractions by geometric progressions, giving
$$\sum_{k=0}^\infty(-2x)^k-\sum_{k=0}^\infty(-x)^k-\sum_{k=0}^\infty(-x)^{k+1}=
\sum_{k=2}^\infty(-1)^k(2^k-2)x^k\\
=2x^2-6x^3+14x^4-30x^5\cdots.$$ Now truncate these sums to some power.
