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$.
-
2The issue here is loss of significance since you're taking the difference of two numbers that are very close together. Try simply combining the fractions. – Mark McClure Oct 31 '21 at 12:52
-
You could compute the series expansion using the geometric series for the denominators. Then the constant and linear terms will cancel. – Lutz Lehmann Oct 31 '21 at 12:56
-
So after combining I get $\frac{-2x-2x^2}{2x^2+3x+1}$. I'm a little uneasy about the denominator though (however, maybe it it fine). I'll try to do it with geometric series now and see what I'll get. – Awerde Oct 31 '21 at 12:58
-
1You can expand the second term with $1+x$ to get the more comparable expressions $\frac{1}{1+2x}-\frac{1-x^2}{1+2x+x^2}$. This shows at least that the difference is $O(x^2)$. – Lutz Lehmann Oct 31 '21 at 13:00
4 Answers
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.
- 1,768
-
+1 But your concern with the premature overflow for large $x$ is clearly out of the scope of the question. – PierreCarre Nov 02 '21 at 16:38
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)}. $$
- 20,974
- 1
- 18
- 34
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...
- 344
-
The OP asks for an equivalent expression, so I'd rule out dropping terms in power series. – PierreCarre Nov 02 '21 at 16:40
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.
- 202