0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\begin{array}{l}
\mathbf{if}\;re \leq -3.7551716130211512 \cdot 10^{+180}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{im \cdot im}{re \cdot -2}}\\
\mathbf{elif}\;re \leq -6.935380485843228 \cdot 10^{-15}:\\
\;\;\;\;0.5 \cdot \frac{\sqrt{2 \cdot \left(im \cdot im\right)}}{\sqrt{\sqrt{im \cdot im + re \cdot re} - re}}\\
\mathbf{elif}\;re \leq 1.2122369579117312 \cdot 10^{+69}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \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.7551716130211512e+180)
(* 0.5 (sqrt (* 2.0 (/ (* im im) (* re -2.0)))))
(if (<= re -6.935380485843228e-15)
(*
0.5
(/
(sqrt (* 2.0 (* im im)))
(sqrt (- (sqrt (+ (* im im) (* re re))) re))))
(if (<= re 1.2122369579117312e+69)
(* 0.5 (sqrt (* 2.0 (+ re (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.7551716130211512e+180) {
tmp = 0.5 * sqrt(2.0 * ((im * im) / (re * -2.0)));
} else if (re <= -6.935380485843228e-15) {
tmp = 0.5 * (sqrt(2.0 * (im * im)) / sqrt(sqrt((im * im) + (re * re)) - re));
} else if (re <= 1.2122369579117312e+69) {
tmp = 0.5 * sqrt(2.0 * (re + 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.7 |
|---|---|
| Target | 33.9 |
| Herbie | 25.6 |
if re < -3.7551716130211512e180Initial program 64.0
rmApplied flip-+_binary64_209864.0
Simplified50.4
Taylor expanded around -inf 32.0
Simplified32.0
if -3.7551716130211512e180 < re < -6.93538048584322783e-15Initial program 51.3
rmApplied flip-+_binary64_209851.3
Simplified34.5
rmApplied associate-*r/_binary64_206634.5
Applied sqrt-div_binary64_214131.6
Simplified31.6
if -6.93538048584322783e-15 < re < 1.21223695791173117e69Initial program 27.4
if 1.21223695791173117e69 < re Initial program 46.0
Taylor expanded around inf 12.6
Final simplification25.6
herbie shell --seed 2020355
(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)))))