da Silva, Joe wrote:
To be honest, the revised Complex_SqRt function you present is a bit too complicated for me. <G>
So, my thinking is that, instead of re-writing the algorithm of Complex_SqRt and similar routines, it would be simpler and less error-prone in these situations, to utilize a new, alternative SqRt function, eg. :
Function Clipped_SqRt (x : double) : double; {Alternative SqRt : Clips negative arguments, returning zero} begin {Clipped_SqRt} if x > 0 then Clipped_SqRt := SqRt (x) else Clipped_SqRt := 0 end; {Clipped_SqRt}
Of course, you would only apply this alternative to SqRt when the argument cannot theoretically be negative ...
I don't think so. This seems more like a kludge to me (first compute a wrong value, and then second-guess in a subroutine what it could really be). Besides, it would only avoid the problem in the "< 0" case -- as Emil noted, "the patch above gives more accurate results (even for arguments which do not trigger the bug)."
BTW, I checked his code carefully to make sure it's mathematically correct.
Frank