0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\begin{array}{l}
\mathbf{if}\;re \leq 1.6248919274963702 \cdot 10^{-114}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5 \cdot im}{\sqrt{re}}\\
\end{array}
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im) :precision binary64 (if (<= re 1.6248919274963702e-114) (* 0.5 (sqrt (* 2.0 (- (hypot re im) re)))) (/ (* 0.5 im) (sqrt re))))
double code(double re, double im) {
return 0.5 * sqrt(2.0 * (sqrt((re * re) + (im * im)) - re));
}
double code(double re, double im) {
double tmp;
if (re <= 1.6248919274963702e-114) {
tmp = 0.5 * sqrt(2.0 * (hypot(re, im) - re));
} else {
tmp = (0.5 * im) / sqrt(re);
}
return tmp;
}



Bits error versus re



Bits error versus im
Results
if re < 1.6248919274963702e-114Initial program 30.8
Simplified2.3
if 1.6248919274963702e-114 < re Initial program 52.4
Simplified34.6
Taylor expanded in re around inf 38.8
Applied associate-*r/_binary6438.9
Applied associate-*r/_binary6438.8
Applied sqrt-div_binary6433.3
Applied associate-*r/_binary6433.3
Simplified20.3
Final simplification8.5
herbie shell --seed 2022082
(FPCore (re im)
:name "math.sqrt on complex, imaginary part, im greater than 0 branch"
:precision binary64
:pre (> im 0.0)
(* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))