CBFalconer wrote:
Maurice Lombardi wrote:
Frank Heckenbach wrote:
Emil Jerabek wrote: If you find other things to improve in the maths algorithms, just go ahead (numerics has never been my favourite area ;-).
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.
Frank