TITLE: Numerical Analysis
QUESTION [5 upvotes]: I am trying to determine some numerical difficulties that arise from a couple problems, and a good way to re-write them to avoid those errors.
For instance, I have: 
1) $\sqrt{x+\dfrac{1}{x}} - \sqrt{x-\dfrac{1}{x}}$ where $x\gg 1$
I think that since these two terms approximately equal each other, there will be cancellation error. So I multiplied the numerator and denominator by the conjugate yielding:
$\dfrac{\dfrac{2}{x}}{\sqrt{x+\dfrac{1}{x}}+\sqrt{x-\dfrac{1}{x}}}$
I think that this should get rid of the cancellation error, does anyone see anything wrong with this attempt?
If this looks right, then I will show my attempt on the second problem, but I hope to verify my method first.
2) $\sqrt{\dfrac{1}{a^2}+\dfrac{1}{b^2}}$ where $a\approx 0$ and $b\approx 1$
Thanks!

REPLY [3 votes]: For 1, you have successfully avoided the cancellation.  If you want, you could go to $$\frac {\frac 2x}{\sqrt x (\sqrt{1+\frac 1{x^2} }+\sqrt{1-\frac 1{x^2} })}=\frac 2{x^{\frac 32} (\sqrt{1+\frac 1{x^2} }+\sqrt{1-\frac 1{x^2} })}\approx x^{-\frac 32}$$  but I am not sure that is an improvement.
For 2, you could have $\frac 1{a^2}$ overflow where $\frac 1a$ does not.  To avoid this, you could rewrite it as $\frac 1a \sqrt {1+\frac {a^2}{b^2}}$.  That still squares $a$, but if it underflows maybe it gets set to zero and you are OK.