0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\begin{array}{l}
\mathbf{if}\;re \leq -2.05568490709924 \cdot 10^{-302}:\\
\;\;\;\;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 1.540540874146605 \cdot 10^{-163}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{elif}\;re \leq 4.72639369204056 \cdot 10^{+54}:\\
\;\;\;\;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 -2.05568490709924e-302)
(*
0.5
(/ (sqrt (* (* im im) 2.0)) (sqrt (- (sqrt (+ (* im im) (* re re))) re))))
(if (<= re 1.540540874146605e-163)
(* 0.5 (sqrt (* 2.0 (+ re im))))
(if (<= re 4.72639369204056e+54)
(*
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 <= -2.05568490709924e-302) {
tmp = 0.5 * (sqrt((im * im) * 2.0) / sqrt(sqrt((im * im) + (re * re)) - re));
} else if (re <= 1.540540874146605e-163) {
tmp = 0.5 * sqrt(2.0 * (re + im));
} else if (re <= 4.72639369204056e+54) {
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.3 |
|---|---|
| Target | 33.3 |
| Herbie | 26.8 |
if re < -2.05568490709923997e-302Initial program 46.4
rmApplied flip-+_binary6446.3
Applied associate-*r/_binary6446.3
Applied sqrt-div_binary6446.4
Simplified35.3
if -2.05568490709923997e-302 < re < 1.54054087414660497e-163Initial program 29.3
Taylor expanded around 0 33.6
if 1.54054087414660497e-163 < re < 4.72639369204056e54Initial program 15.5
rmApplied add-sqr-sqrt_binary6415.5
Applied sqrt-prod_binary6415.6
if 4.72639369204056e54 < re Initial program 44.1
Taylor expanded around inf 11.4
Final simplification26.8
herbie shell --seed 2020220
(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)))))