CBFalconer wrote:
Frank Heckenbach wrote:
CBFalconer wrote:
Maurice Lombardi wrote:
... snip ...
While reading this thread, I had a look to math.pas and found an other well known issue. The function
Complex_Abs := SqRt (Sqr (Re (z)) + Sqr (Im (z)))
overflows if either Re or Im is in between sqrt(MaxReal) (~10^154) and MaxReal (~10^308), while the result is (usually) below MaxReal.
Why not apply a little trigonometry.
Theta = arctan(Re/Im); C_Abs = Re * cos(theta);
You mean /, not *?
with a few conditionals to avoid zeroes.
Not only zeros. Re/Im might overflow if Im is very small compared to Re.
You might reserve it for when either Re or Im is greater than sqrt(maxreal)/2 if you have qualms about the trigonometric function accuracy.
... and efficiency. I think one SqRt is quite a bit cheaper than two trigonomic functions.
You didn't read it all.
Whoa, you know better than me what I read?
It only comes into play when there is potential overflow.
But Maurice's (or Numerical Recipes's) solution avoids the overflow as well with only one SqRt and some primitive operations (+, *, /, >). That's what I was comparing with.
Frank