0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}
\begin{array}{l}
\mathbf{if}\;\sqrt{re \cdot re + im \cdot im} - re \leq 0:\\
\;\;\;\;\begin{array}{l}
t_0 := \sqrt{\sqrt{2}}\\
0.5 \cdot \left(\left(\sqrt{0.5} \cdot \left(im \cdot \left(t_0 \cdot t_0\right)\right)\right) \cdot \sqrt{\frac{1}{re}}\right)
\end{array}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\mathsf{hypot}\left(re, im\right) - 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 (<= (- (sqrt (+ (* re re) (* im im))) re) 0.0)
(let* ((t_0 (sqrt (sqrt 2.0))))
(* 0.5 (* (* (sqrt 0.5) (* im (* t_0 t_0))) (sqrt (/ 1.0 re)))))
(* 0.5 (sqrt (* 2.0 (- (hypot re 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 ((sqrt((re * re) + (im * im)) - re) <= 0.0) {
double t_0_1 = sqrt(sqrt(2.0));
tmp = 0.5 * ((sqrt(0.5) * (im * (t_0_1 * t_0_1))) * sqrt(1.0 / re));
} else {
tmp = 0.5 * sqrt(2.0 * (hypot(re, im) - re));
}
return tmp;
}



Bits error versus re



Bits error versus im
Results
if (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0Initial program 58.5
Simplified51.2
Taylor expanded in im around 0 7.0
Applied add-sqr-sqrt_binary646.5
Applied associate-*l*_binary646.6
Applied associate-*r*_binary646.5
if 0.0 < (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) Initial program 34.9
Simplified6.3
Final simplification6.3
herbie shell --seed 2021329
(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)))))