da Silva, Joe wrote:
OK, if you're happy that the code is correct ... I guess I just don't understand what the "a := Abs(Im(z)) / (2 * b)" statement does ...
I could write down the computations if you like (unless Emil has them readily available already) ...
Anyway, although you might say the Clipped_SqRt solution is a kludge, it is certainly not second-guessing. It would only be applied where the minimum theoretical argument was zero, in which case it would avoid (and eliminate) rounding errors which produced a slightly negative "zero".
As I said, I'm no expert on numeric algorithms, but I think if an algorithm can produce such wrong results, it might also be inaccurate for other values and should be improved (such as Complex_Sqrt). After all, normal rounding doesn't easily make a positive value (even very close to 0) negative. So it might be better if the programmer sees the problem clearly with an error message. If it really turns out that an algorithm is as good as possible (numerically) and still prone to this kind of problem, something like Clipped_SqRt might be in order. But I guess this is rather an exceptional situation, so Clipped_SqRt does not seem a good candidate for a general library routine.
But perhaps Emil can comment better on these matters ...
Frank