0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\begin{array}{l}
\mathbf{if}\;re \leq 4.982407308203682 \cdot 10^{-234}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im \cdot \sqrt{2}}{\sqrt{re + \mathsf{hypot}\left(im, re\right)}}\\
\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 4.982407308203682e-234) (* 0.5 (sqrt (* 2.0 (- (hypot re im) re)))) (* 0.5 (/ (* im (sqrt 2.0)) (sqrt (+ re (hypot im 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 <= 4.982407308203682e-234) {
tmp = 0.5 * sqrt(2.0 * (hypot(re, im) - re));
} else {
tmp = 0.5 * ((im * sqrt(2.0)) / sqrt(re + hypot(im, re)));
}
return tmp;
}



Bits error versus re



Bits error versus im
Results
if re < 4.98240730820368227e-234Initial program 31.1
Simplified0.3
if 4.98240730820368227e-234 < re Initial program 48.0
Applied flip--_binary6448.0
Applied associate-*r/_binary6448.0
Applied sqrt-div_binary6448.0
Simplified35.2
Simplified26.7
Taylor expanded in im around 0 0.5
Final simplification0.4
herbie shell --seed 2022104
(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)))))