TITLE: how do you compute $\|c-a\| - \|b-a\|$ without catastrophic cancellation?
QUESTION [6 upvotes]: Given three points or vectors in the plane:
\begin{align}
    \vec a &= (a_x,a_y) \\
    \vec b &= (b_x,b_x) \\
    \vec c &= (c_x,c_y)
\end{align}
How do you compute $\lVert \vec c - \vec a \rVert - \lVert \vec b - \vec a \rVert$, i.e. "how much farther is it from $\vec a$ to $\vec c$ than from $\vec a$ to $\vec b$"?
For definiteness, all coordinates and computatations are to be in double precision IEEE754 floating point arithmetic.
The answer must be reasonably accurate even if $\vec a$ is very large compared to $\vec b$ and $\vec c$.
Note that the naive expression
$$
    \sqrt{{(c_x-a_x)}^2+{(c_y-a_y)}^2} - \sqrt{{(b_x-a_x)}^2+{(b_y-a_y)}^2},
$$
while mathematically correct, is unsuitable for this computation because it catastrophically cancels
if $\vec a$ has much greater magnitude than $\vec b$ and $\vec c$.
For example, if:
\begin{align}
    \vec a &= (-10^{20},-10^{20}) \\
    \vec b &= (0,0) \\
    \vec c &= (1,1)
\end{align}
then the answer is $\sqrt2$,
but computing it naively will produce $0$ due to catastrophic cancellation.

REPLY [8 votes]: The binomial identity $x^2-y^2=(x+y)(x-y)$ can be extended to euclidean norms and scalar products, which gives
$$
\|c-a\|-\|b-a\|=\frac{\|c-a\|^2-\|b-a\|^2}{\|c-a\|+\|b-a\|}
=\frac{\bigl\langle b+c-2a,\,c-b\bigr\rangle}{\|c-a\|+\|b-a\|}
$$