3
$\begingroup$

So if the following function is evaluated with the floating-point arithmetic, we get poor results for certain range of values of $x$. Therefore, I need to provide an alternate function that can be used for those values of $x$. The function is:

$ f(x)= \sqrt{1+x}-\sqrt{1-x} $

I have found the range for this and it is: $-\sqrt{2} \le y \le \sqrt{2}$

So how would I make an alternate expression or function for this.

Do I just multiply by its conjugate.

  • 0
    @J.M., I see now, thanks for your explanation.2012-02-13

2 Answers 2

3

The problem with this function is that for small $|x|$ the value $f(x)$ is the difference of two almost equal numbers, as has been noted by other readers. Very often this poses serious problems (as, e.g., in numerical differentiation), but here is an easy way out (you have hinted at it yourself): Use the formula $a-b ={a^2-b^2\over a+b}$ in order to obtain an alternative expression for $f$: $f(x)\ =\ {2x\over \sqrt{1+x}+\sqrt{1-x}}\ .$

  • 0
    This is more likely to be the expected solution than my attempt. I had considered something like this briefly but discarded it because I was afraid that rounding errors in the $1+x$ and $1-x$ subexpressions might do harm for small $x$. With a bit more thought I see that this isn't likely to be the case. (And an arcsine computation would likely be based on a hardware arc_tangent_ and have a $\sqrt{1-x^2}$ intermediate anyway).2012-02-15
2

A good first step in getting a handle on a problem of this kind is to graph the function. We see (or would see, if Wolfram Alpha didn't insist on having the x and y axis scale independently) that $f(x)\approx x$ for small $x$. So the risk is that for very small $x$, adding to and subtracting from $1$ would throw away many bits of $x$, which we then won't get again by subtracting the square roots.

HINT: Write down an expression for $f(x)^2$. Does anything look trigonometrical? Suppose $x$ is the sine of something? Remember the half-angle formulas.

Edit: Here's the solution alluded to in the above hint: Let's see if we can do something about those square roots. The natural thing to try in order to make them go away is to square the entire thing (and worry about signs later): $f(x)^2 = (1+x)+(1-x)-2\sqrt{(1+x)(1-x)} = 2 - 2\sqrt{1-x^2} $ That doesn't immediately seem to help, but the square root that remains looks distinctly trigonometrical. Let's see if it helps to define $x=\sin \theta$. Then we can compute $f(x)^2 = 2 - 2\sqrt{1-\sin^2\theta} = 2 - 2\cos\theta = 4\sin^2\left(\frac12\theta\right)$ So we get (checking now that the signs are right): $f(x) = 2\sin\left(\frac12\arcsin x\right)$ A self-respecting floating-point implementation ought to be able to compute sines and arcsines of small arguments without significant loss of precision. Closer to $x=\pm 1$ you probably want to switch over to the naive formula, though.

  • 0
    @J.M.: I am not too familiar with them. Thanks for mentioning those!2012-02-15