Absolute value of complex number in Numerical Recipes

383 Views Asked by At

In numerical recipes in C, absolute value of complex number $a+ib$ is implemented as $b*\sqrt{1+\left(\frac{a}{b}\right)^2}$ if $|b|$ is greater than $|a|$ and $a*\sqrt{1+\left(\frac{b}{a}\right)^2}$ if $|a|$ is greater than $|b|$. Does the numerical error in the absolute value calculation reduce when doing this. If so, how?

2

There are 2 best solutions below

0
On BEST ANSWER

One point in favor of these formulas is that if you just do $\sqrt{a^2+b^2}$ there's a risk of overflow or underflow when you square the $a$ and $b$. The NR formulas will avoid this right up (or down) to the limit of the floating-point representation.

4
On

That is a very strange way of defining the 'absolute value of a complex number' (should be called the modulus of the complex number).

Really, you don't need those conditions. Even if you used the 'wrong' formula, you will still get the exact same answers. In fact, the given formulas introduce conditions about $a$ and $b$ being non-zero, which is most certainly unnecessary (since the modulus can still be computed even if either $a$ or $b$ are zero).

Also, I don't understand what you mean by the 'numerical error' of the modulus. There is no numerical error, the modulus is not an approximation. It gives an exact value.