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




Bits error versus re




Bits error versus im
| Original | 38.7 |
|---|---|
| Target | 33.7 |
| Herbie | 10.9 |
if (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) < 0.0Initial program 57.2
Simplified57.2
Applied sqrt-prod_binary6457.2
Applied add-sqr-sqrt_binary6457.2
Applied associate-*l*_binary6457.2
Applied pow1_binary6457.2
Applied sqrt-pow1_binary6457.2
Applied pow1_binary6457.2
Applied sqrt-pow1_binary6457.2
Applied pow-prod-down_binary6457.2
Applied pow1_binary6457.2
Applied sqrt-pow1_binary6457.2
Applied pow-prod-down_binary6457.2
Simplified57.2
Taylor expanded in re around -inf 28.8
Simplified34.8
if 0.0 < (sqrt.f64 (*.f64 2 (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re))) Initial program 36.1
Simplified7.4
Applied sqrt-prod_binary647.7
Applied add-sqr-sqrt_binary647.8
Applied associate-*l*_binary647.7
Applied sqrt-unprod_binary647.6
Final simplification10.9
herbie shell --seed 2021215
(FPCore (re im)
:name "math.sqrt on complex, real part"
:precision binary64
:herbie-target
(if (< re 0.0) (* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- (sqrt (+ (* re re) (* im im))) re))))) (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))