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 \frac{\sqrt{0.5} \cdot \left(t_0 \cdot \left(im \cdot t_0\right)\right)}{\sqrt{re}}
\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) (* t_0 (* im t_0))) (sqrt 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) * (t_0_1 * (im * t_0_1))) / sqrt(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 59.0
Simplified51.6
Taylor expanded in im around 0 7.0
Applied add-sqr-sqrt_binary646.5
Applied associate-*l*_binary646.6
Applied sqrt-div_binary646.6
Applied associate-*r/_binary646.5
if 0.0 < (-.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) Initial program 35.1
Simplified6.3
Final simplification6.3
herbie shell --seed 2021313
(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)))))