I tried computing the real part of clog(a + i*b) using the following approach
Consider 'x' to be the complex number. x = a + i*b Let z be the complex log of x.
real(x) = 0.5 * log(a^2 + b^2)
This approach gives a huge error in terms of ULP for values between 0.5 and 1.0 especially.
I tried other approaches to avoid squaring of both the real and imaginary parts such as
Let t = b / a; real(x) = log(a) + 0.5 * log1p(t*t)
The error continued to persist with this approach as well.
I understand that the error is likely from the squaring of a and b and hence I tried using fma()
operations to get the error due to the squaring of 'a' and 'b'
Let a2 = a * a b2 = b * b
err_a2 = fma(a,a, -a2)
err_b2 = fma(b,b,-b2)
I then tried 0.5 * log(((err_a1 + err_b2) + a2) + b2)
to get the real value of the complex log of x.
But the result is still inaccurate.
How can I compute log(sqrt(a^2 + b^2))
accurately (error within 2 ULP). I think I need to compute the square root of a^2 + b^2
in higher precision at a higher precision but I am not sure how to proceed from here.