0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\begin{array}{l}
t_0 := re + \sqrt{re \cdot re + im \cdot im}\\
\mathbf{if}\;t_0 \leq -7.31236209128368 \cdot 10^{-302}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \mathsf{fma}\left(\sqrt[3]{re} \cdot \sqrt[3]{re}, \sqrt[3]{re}, \mathsf{hypot}\left(re, im\right)\right)}\\
\mathbf{elif}\;t_0 \leq 0:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot {\left(2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)\right)}^{0.5}\\
\end{array}
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im)
:precision binary64
(let* ((t_0 (+ re (sqrt (+ (* re re) (* im im))))))
(if (<= t_0 -7.31236209128368e-302)
(*
0.5
(sqrt (* 2.0 (fma (* (cbrt re) (cbrt re)) (cbrt re) (hypot re im)))))
(if (<= t_0 0.0)
(* 0.5 (sqrt (* 2.0 (* -0.5 (/ (pow im 2.0) re)))))
(* 0.5 (pow (* 2.0 (+ re (hypot re im))) 0.5))))))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 t_0 = re + sqrt((re * re) + (im * im));
double tmp;
if (t_0 <= -7.31236209128368e-302) {
tmp = 0.5 * sqrt(2.0 * fma((cbrt(re) * cbrt(re)), cbrt(re), hypot(re, im)));
} else if (t_0 <= 0.0) {
tmp = 0.5 * sqrt(2.0 * (-0.5 * (pow(im, 2.0) / re)));
} else {
tmp = 0.5 * pow((2.0 * (re + hypot(re, im))), 0.5);
}
return tmp;
}




Bits error versus re




Bits error versus im
| Original | 38.2 |
|---|---|
| Target | 33.4 |
| Herbie | 9.6 |
if (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < -7.3123620912836802e-302Initial program 64.0
Simplified28.4
Applied add-cube-cbrt_binary6428.2
Applied fma-def_binary6428.1
if -7.3123620912836802e-302 < (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) < 0.0Initial program 57.7
Simplified57.2
Taylor expanded in re around -inf 31.2
if 0.0 < (+.f64 (sqrt.f64 (+.f64 (*.f64 re re) (*.f64 im im))) re) Initial program 34.7
Simplified6.1
Applied sqrt-prod_binary646.4
Applied pow1/2_binary646.4
Applied pow1/2_binary646.4
Applied pow-prod-down_binary646.1
Final simplification9.6
herbie shell --seed 2021329
(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)))))