0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\begin{array}{l}
\mathbf{if}\;im \leq -4.986259634894712 \cdot 10^{-51}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\
\mathbf{elif}\;im \leq -2.0685955733607066 \cdot 10^{-292}:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \frac{\left|im\right|}{\sqrt{\sqrt{\sqrt{re \cdot re + im \cdot im}} \cdot \sqrt{\sqrt{re \cdot re + im \cdot im}} - re}}\right)\\
\mathbf{elif}\;im \leq 1.395509735790332 \cdot 10^{-172}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\mathbf{elif}\;im \leq 1.6694765103601103 \cdot 10^{-44}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im + 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 (<= im -4.986259634894712e-51)
(* 0.5 (sqrt (* 2.0 (- re im))))
(if (<= im -2.0685955733607066e-292)
(*
0.5
(*
(sqrt 2.0)
(/
(fabs im)
(sqrt
(-
(*
(sqrt (sqrt (+ (* re re) (* im im))))
(sqrt (sqrt (+ (* re re) (* im im)))))
re)))))
(if (<= im 1.395509735790332e-172)
(* 0.5 (* 2.0 (sqrt re)))
(if (<= im 1.6694765103601103e-44)
(* 0.5 (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))))
(* 0.5 (sqrt (* 2.0 (+ 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 (im <= -4.986259634894712e-51) {
tmp = 0.5 * sqrt(2.0 * (re - im));
} else if (im <= -2.0685955733607066e-292) {
tmp = 0.5 * (sqrt(2.0) * (fabs(im) / sqrt((sqrt(sqrt((re * re) + (im * im))) * sqrt(sqrt((re * re) + (im * im)))) - re)));
} else if (im <= 1.395509735790332e-172) {
tmp = 0.5 * (2.0 * sqrt(re));
} else if (im <= 1.6694765103601103e-44) {
tmp = 0.5 * sqrt(2.0 * (re + sqrt((re * re) + (im * im))));
} else {
tmp = 0.5 * sqrt(2.0 * (im + re));
}
return tmp;
}




Bits error versus re




Bits error versus im
Results
| Original | 38.6 |
|---|---|
| Target | 33.6 |
| Herbie | 23.9 |
if im < -4.986259634894712e-51Initial program 40.0
Taylor expanded around -inf 16.6
Simplified16.6
if -4.986259634894712e-51 < im < -2.0685955733607066e-292Initial program 35.9
rmApplied flip-+_binary64_243951.7
Applied associate-*r/_binary64_240751.7
Applied sqrt-div_binary64_248251.9
Simplified42.4
rmApplied *-un-lft-identity_binary64_246542.4
Applied sqrt-prod_binary64_248142.4
Applied sqrt-prod_binary64_248142.4
Applied times-frac_binary64_247142.4
Simplified42.4
Simplified35.3
rmApplied add-sqr-sqrt_binary64_248735.2
if -2.0685955733607066e-292 < im < 1.39550973579033192e-172Initial program 42.5
Taylor expanded around 0 35.4
Simplified34.8
if 1.39550973579033192e-172 < im < 1.66947651036011032e-44Initial program 30.8
if 1.66947651036011032e-44 < im Initial program 40.4
Taylor expanded around 0 16.1
Final simplification23.9
herbie shell --seed 2021022
(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)))))