0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\begin{array}{l}
\mathbf{if}\;re \leq -6.549533459362007 \cdot 10^{+160}:\\
\;\;\;\;0.5 \cdot \frac{\sqrt{\left(im \cdot im\right) \cdot 2}}{\sqrt{re \cdot -2}}\\
\mathbf{elif}\;re \leq -5.543930521314922 \cdot 10^{-300}:\\
\;\;\;\;0.5 \cdot \left(\left(\sqrt{\sqrt{2}} \cdot \left|im\right|\right) \cdot \sqrt{\frac{\sqrt{2}}{\sqrt{im \cdot im + re \cdot re} - re}}\right)\\
\mathbf{elif}\;re \leq 1.4495720106119306 \cdot 10^{-217}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + im\right)}\\
\mathbf{elif}\;re \leq 2.3897943661054227 \cdot 10^{-18}:\\
\;\;\;\;0.5 \cdot \left(\left(\sqrt{\sqrt{2}} \cdot \left|im\right|\right) \cdot \frac{\sqrt{\sqrt{2}}}{\sqrt{\left(-im\right) - 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 -6.549533459362007e+160)
(* 0.5 (/ (sqrt (* (* im im) 2.0)) (sqrt (* re -2.0))))
(if (<= re -5.543930521314922e-300)
(*
0.5
(*
(* (sqrt (sqrt 2.0)) (fabs im))
(sqrt (/ (sqrt 2.0) (- (sqrt (+ (* im im) (* re re))) re)))))
(if (<= re 1.4495720106119306e-217)
(* 0.5 (sqrt (* 2.0 (+ re im))))
(if (<= re 2.3897943661054227e-18)
(*
0.5
(*
(* (sqrt (sqrt 2.0)) (fabs im))
(/ (sqrt (sqrt 2.0)) (sqrt (- (- im) 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 <= -6.549533459362007e+160) {
tmp = 0.5 * (sqrt((im * im) * 2.0) / sqrt(re * -2.0));
} else if (re <= -5.543930521314922e-300) {
tmp = 0.5 * ((sqrt(sqrt(2.0)) * fabs(im)) * sqrt(sqrt(2.0) / (sqrt((im * im) + (re * re)) - re)));
} else if (re <= 1.4495720106119306e-217) {
tmp = 0.5 * sqrt(2.0 * (re + im));
} else if (re <= 2.3897943661054227e-18) {
tmp = 0.5 * ((sqrt(sqrt(2.0)) * fabs(im)) * (sqrt(sqrt(2.0)) / sqrt(-im - re)));
} else {
tmp = 0.5 * sqrt(2.0 * (re + re));
}
return tmp;
}




Bits error versus re




Bits error versus im
Results
| Original | 38.8 |
|---|---|
| Target | 33.6 |
| Herbie | 23.5 |
if re < -6.5495334593620073e160Initial program 64.0
rmApplied flip-+_binary64_241064.0
Applied associate-*r/_binary64_238064.0
Applied sqrt-div_binary64_245264.0
Simplified48.6
Taylor expanded around -inf 17.6
Simplified17.6
if -6.5495334593620073e160 < re < -5.543930521314922e-300Initial program 40.5
rmApplied flip-+_binary64_241040.4
Applied associate-*r/_binary64_238040.4
Applied sqrt-div_binary64_245240.5
Simplified30.4
rmApplied *-un-lft-identity_binary64_243630.4
Applied sqrt-prod_binary64_245130.4
Applied sqrt-prod_binary64_245130.5
Applied times-frac_binary64_244230.5
Simplified20.9
Simplified20.9
rmApplied *-un-lft-identity_binary64_243620.9
Applied sqrt-prod_binary64_245120.9
Applied add-sqr-sqrt_binary64_245720.9
Applied times-frac_binary64_244220.9
Applied associate-*r*_binary64_237820.9
Simplified20.9
rmApplied sqrt-undiv_binary64_245620.8
if -5.543930521314922e-300 < re < 1.4495720106119306e-217Initial program 28.1
Taylor expanded around 0 31.9
if 1.4495720106119306e-217 < re < 2.3897943661054227e-18Initial program 21.4
rmApplied flip-+_binary64_241037.4
Applied associate-*r/_binary64_238037.5
Applied sqrt-div_binary64_245237.8
Simplified37.8
rmApplied *-un-lft-identity_binary64_243637.8
Applied sqrt-prod_binary64_245137.8
Applied sqrt-prod_binary64_245137.8
Applied times-frac_binary64_244237.8
Simplified37.4
Simplified37.4
rmApplied *-un-lft-identity_binary64_243637.4
Applied sqrt-prod_binary64_245137.4
Applied add-sqr-sqrt_binary64_245737.4
Applied times-frac_binary64_244237.4
Applied associate-*r*_binary64_237837.4
Simplified37.4
Taylor expanded around -inf 43.2
Simplified43.2
if 2.3897943661054227e-18 < re Initial program 38.5
Taylor expanded around inf 15.4
Final simplification23.5
herbie shell --seed 2020292
(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)))))