0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\begin{array}{l}
\mathbf{if}\;re \leq -3.868203887880326 \cdot 10^{-293}:\\
\;\;\;\;0.5 \cdot \frac{\sqrt{\left(im \cdot im\right) \cdot 2}}{\sqrt{\sqrt{im \cdot im + re \cdot re} - re}}\\
\mathbf{elif}\;re \leq 8.82691264352238 \cdot 10^{-44}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{elif}\;re \leq 1.0946873832682728 \cdot 10^{+128}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{\sqrt{im \cdot im + re \cdot re}} \cdot \sqrt{\sqrt{im \cdot im + re \cdot re}}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + 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 (<= re -3.868203887880326e-293)
(*
0.5
(/ (sqrt (* (* im im) 2.0)) (sqrt (- (sqrt (+ (* im im) (* re re))) re))))
(if (<= re 8.82691264352238e-44)
(* 0.5 (sqrt (* 2.0 (+ re im))))
(if (<= re 1.0946873832682728e+128)
(*
0.5
(sqrt
(*
2.0
(+
re
(*
(sqrt (sqrt (+ (* im im) (* re re))))
(sqrt (sqrt (+ (* im im) (* re re)))))))))
(* 0.5 (sqrt (* 2.0 (+ re 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 (re <= -3.868203887880326e-293) {
tmp = 0.5 * (sqrt((im * im) * 2.0) / sqrt(sqrt((im * im) + (re * re)) - re));
} else if (re <= 8.82691264352238e-44) {
tmp = 0.5 * sqrt(2.0 * (re + im));
} else if (re <= 1.0946873832682728e+128) {
tmp = 0.5 * sqrt(2.0 * (re + (sqrt(sqrt((im * im) + (re * re))) * sqrt(sqrt((im * im) + (re * re))))));
} else {
tmp = 0.5 * sqrt(2.0 * (re + re));
}
return tmp;
}




Bits error versus re




Bits error versus im
Results
| Original | 38.6 |
|---|---|
| Target | 33.7 |
| Herbie | 28.5 |
if re < -3.8682038878803262e-293Initial program 45.6
rmApplied flip-+_binary64_207345.5
Applied associate-*r/_binary64_204345.5
Applied sqrt-div_binary64_211545.6
Simplified34.5
if -3.8682038878803262e-293 < re < 8.8269126435223795e-44Initial program 25.1
Taylor expanded around 0 35.7
if 8.8269126435223795e-44 < re < 1.094687383268273e128Initial program 16.5
rmApplied add-sqr-sqrt_binary64_212016.6
if 1.094687383268273e128 < re Initial program 57.0
Taylor expanded around inf 8.9
Final simplification28.5
herbie shell --seed 2020299
(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)))))