0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\begin{array}{l}
\mathbf{if}\;im \leq -1.0041652957658585 \cdot 10^{+27}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\
\mathbf{elif}\;im \leq -24653266729299256:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}\\
\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_0 := \sqrt{im \cdot im + re \cdot re}\\
\mathbf{if}\;im \leq -4.892502961362615 \cdot 10^{-55}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + t_0\right)}\\
\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_1 := 0.5 \cdot \frac{\sqrt{2 \cdot \left(im \cdot im\right)}}{\sqrt{re \cdot -2}}\\
\mathbf{if}\;im \leq -1.8591419239134423 \cdot 10^{-59}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;im \leq -8.698128566920095 \cdot 10^{-78}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt[3]{t_0 \cdot \left(t_0 \cdot t_0\right)}\right)}\\
\mathbf{elif}\;im \leq -6.2060713421920385 \cdot 10^{-158}:\\
\;\;\;\;t_1\\
\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_2 := 0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\mathbf{if}\;im \leq -3.9551150572318455 \cdot 10^{-213}:\\
\;\;\;\;t_2\\
\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_3 := \sqrt{t_0 - re}\\
\mathbf{if}\;im \leq -4.196486865911031 \cdot 10^{-281}:\\
\;\;\;\;-0.5 \cdot \frac{im \cdot \sqrt{2}}{t_3}\\
\mathbf{elif}\;im \leq 1.4425503680000377 \cdot 10^{-292}:\\
\;\;\;\;t_2\\
\mathbf{elif}\;im \leq 1.405562579947193 \cdot 10^{-202}:\\
\;\;\;\;0.5 \cdot \frac{\sqrt{2}}{\frac{t_3}{im}}\\
\mathbf{elif}\;im \leq 3.0826956855422842 \cdot 10^{-161}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im + re\right)}\\
\mathbf{elif}\;im \leq 1.6683128854529867 \cdot 10^{+136}:\\
\;\;\;\;\begin{array}{l}
t_4 := \sqrt{t_0}\\
0.5 \cdot \sqrt{2 \cdot \left(re + t_4 \cdot t_4\right)}
\end{array}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot 2}\\
\end{array}\\
\end{array}\\
\end{array}\\
\end{array}\\
\end{array}
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im)
:precision binary64
(if (<= im -1.0041652957658585e+27)
(* 0.5 (sqrt (* 2.0 (- re im))))
(if (<= im -24653266729299256.0)
(* 0.5 (sqrt (* 2.0 (* (/ (* im im) re) -0.5))))
(let* ((t_0 (sqrt (+ (* im im) (* re re)))))
(if (<= im -4.892502961362615e-55)
(* 0.5 (sqrt (* 2.0 (+ re t_0))))
(let* ((t_1 (* 0.5 (/ (sqrt (* 2.0 (* im im))) (sqrt (* re -2.0))))))
(if (<= im -1.8591419239134423e-59)
t_1
(if (<= im -8.698128566920095e-78)
(* 0.5 (sqrt (* 2.0 (+ re (cbrt (* t_0 (* t_0 t_0)))))))
(if (<= im -6.2060713421920385e-158)
t_1
(let* ((t_2 (* 0.5 (* 2.0 (sqrt re)))))
(if (<= im -3.9551150572318455e-213)
t_2
(let* ((t_3 (sqrt (- t_0 re))))
(if (<= im -4.196486865911031e-281)
(* -0.5 (/ (* im (sqrt 2.0)) t_3))
(if (<= im 1.4425503680000377e-292)
t_2
(if (<= im 1.405562579947193e-202)
(* 0.5 (/ (sqrt 2.0) (/ t_3 im)))
(if (<= im 3.0826956855422842e-161)
(* 0.5 (sqrt (* 2.0 (+ im re))))
(if (<= im 1.6683128854529867e+136)
(let* ((t_4 (sqrt t_0)))
(* 0.5 (sqrt (* 2.0 (+ re (* t_4 t_4))))))
(* 0.5 (sqrt (* im 2.0))))))))))))))))))))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 (im <= -1.0041652957658585e+27) {
tmp = 0.5 * sqrt(2.0 * (re - im));
} else if (im <= -24653266729299256.0) {
tmp = 0.5 * sqrt(2.0 * (((im * im) / re) * -0.5));
} else {
double t_0 = sqrt((im * im) + (re * re));
double tmp_1;
if (im <= -4.892502961362615e-55) {
tmp_1 = 0.5 * sqrt(2.0 * (re + t_0));
} else {
double t_1 = 0.5 * (sqrt(2.0 * (im * im)) / sqrt(re * -2.0));
double tmp_2;
if (im <= -1.8591419239134423e-59) {
tmp_2 = t_1;
} else if (im <= -8.698128566920095e-78) {
tmp_2 = 0.5 * sqrt(2.0 * (re + cbrt(t_0 * (t_0 * t_0))));
} else if (im <= -6.2060713421920385e-158) {
tmp_2 = t_1;
} else {
double t_2 = 0.5 * (2.0 * sqrt(re));
double tmp_3;
if (im <= -3.9551150572318455e-213) {
tmp_3 = t_2;
} else {
double t_3 = sqrt(t_0 - re);
double tmp_4;
if (im <= -4.196486865911031e-281) {
tmp_4 = -0.5 * ((im * sqrt(2.0)) / t_3);
} else if (im <= 1.4425503680000377e-292) {
tmp_4 = t_2;
} else if (im <= 1.405562579947193e-202) {
tmp_4 = 0.5 * (sqrt(2.0) / (t_3 / im));
} else if (im <= 3.0826956855422842e-161) {
tmp_4 = 0.5 * sqrt(2.0 * (im + re));
} else if (im <= 1.6683128854529867e+136) {
double t_4 = sqrt(t_0);
tmp_4 = 0.5 * sqrt(2.0 * (re + (t_4 * t_4)));
} else {
tmp_4 = 0.5 * sqrt(im * 2.0);
}
tmp_3 = tmp_4;
}
tmp_2 = tmp_3;
}
tmp_1 = tmp_2;
}
tmp = tmp_1;
}
return tmp;
}




Bits error versus re




Bits error versus im
Results
| Original | 38.6 |
|---|---|
| Target | 33.8 |
| Herbie | 24.2 |
if im < -1.0041652957658585e27Initial program 43.7
Taylor expanded around -inf 12.6
Simplified12.6
if -1.0041652957658585e27 < im < -24653266729299256Initial program 21.9
Taylor expanded around -inf 45.9
Simplified45.9
if -24653266729299256 < im < -4.8925029613626153e-55Initial program 23.6
if -4.8925029613626153e-55 < im < -1.8591419239134423e-59 or -8.6981285669200955e-78 < im < -6.2060713421920385e-158Initial program 30.1
rmApplied flip-+_binary6444.4
Applied associate-*r/_binary6444.4
Applied sqrt-div_binary6444.5
Simplified31.7
Taylor expanded around -inf 41.5
Simplified41.5
if -1.8591419239134423e-59 < im < -8.6981285669200955e-78Initial program 23.5
rmApplied add-cbrt-cube_binary6426.5
if -6.2060713421920385e-158 < im < -3.9551150572318455e-213 or -4.19648686591103086e-281 < im < 1.4425503680000377e-292Initial program 42.7
Taylor expanded around 0 35.9
Simplified35.4
if -3.9551150572318455e-213 < im < -4.19648686591103086e-281Initial program 45.0
rmApplied flip-+_binary6460.8
Applied associate-*r/_binary6460.8
Applied sqrt-div_binary6461.2
Simplified53.9
Taylor expanded around -inf 38.5
Simplified38.5
if 1.4425503680000377e-292 < im < 1.4055625799471929e-202Initial program 42.3
rmApplied flip-+_binary6460.9
Applied associate-*r/_binary6460.9
Applied sqrt-div_binary6461.1
Simplified52.5
rmApplied sqrt-prod_binary6452.5
Applied associate-/l*_binary6452.5
Simplified38.0
if 1.4055625799471929e-202 < im < 3.08269568554228425e-161Initial program 44.3
Taylor expanded around 0 47.0
if 3.08269568554228425e-161 < im < 1.66831288545298666e136Initial program 24.3
rmApplied add-sqr-sqrt_binary6424.4
if 1.66831288545298666e136 < im Initial program 58.8
Taylor expanded around 0 8.2
Final simplification24.2
herbie shell --seed 2021196
(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)))))