0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\begin{array}{l}
\mathbf{if}\;im \leq -3.0199405747825176 \cdot 10^{+77}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\
\mathbf{elif}\;im \leq -1.5611223038770802 \cdot 10^{-162}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)}\\
\mathbf{elif}\;im \leq -7.061996127152542 \cdot 10^{-306}:\\
\;\;\;\;0.5 \cdot \left(\left|im\right| \cdot \frac{\sqrt{2}}{\sqrt{re \cdot -2}}\right)\\
\mathbf{elif}\;im \leq 7.019789919272511 \cdot 10^{-278}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\mathbf{elif}\;im \leq 8.074975228724846 \cdot 10^{-250}:\\
\;\;\;\;0.5 \cdot \left(\left|im\right| \cdot \frac{\sqrt{2}}{\sqrt{re \cdot -2}}\right)\\
\mathbf{elif}\;im \leq 2.10816646233924 \cdot 10^{-162}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\mathbf{elif}\;im \leq 3.235045464145441 \cdot 10^{-105}:\\
\;\;\;\;0.5 \cdot \left(\left|im\right| \cdot \sqrt{\frac{2}{\sqrt{re \cdot re + im \cdot im} - re}}\right)\\
\mathbf{elif}\;im \leq 1.1890627631191766 \cdot 10^{-70}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\right)\\
\mathbf{elif}\;im \leq 3.279790948248637 \cdot 10^{+82}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(\left|im\right| \cdot \frac{\sqrt{2}}{\sqrt{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 -3.0199405747825176e+77)
(* 0.5 (sqrt (* 2.0 (- re im))))
(if (<= im -1.5611223038770802e-162)
(* 0.5 (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))))
(if (<= im -7.061996127152542e-306)
(* 0.5 (* (fabs im) (/ (sqrt 2.0) (sqrt (* re -2.0)))))
(if (<= im 7.019789919272511e-278)
(* 0.5 (* 2.0 (sqrt re)))
(if (<= im 8.074975228724846e-250)
(* 0.5 (* (fabs im) (/ (sqrt 2.0) (sqrt (* re -2.0)))))
(if (<= im 2.10816646233924e-162)
(* 0.5 (* 2.0 (sqrt re)))
(if (<= im 3.235045464145441e-105)
(*
0.5
(*
(fabs im)
(sqrt (/ 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))
(if (<= im 1.1890627631191766e-70)
(* 0.5 (* 2.0 (sqrt re)))
(if (<= im 3.279790948248637e+82)
(* 0.5 (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))))
(*
0.5
(* (fabs im) (/ (sqrt 2.0) (sqrt (- 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 <= -3.0199405747825176e+77) {
tmp = 0.5 * sqrt(2.0 * (re - im));
} else if (im <= -1.5611223038770802e-162) {
tmp = 0.5 * sqrt(2.0 * (re + sqrt((re * re) + (im * im))));
} else if (im <= -7.061996127152542e-306) {
tmp = 0.5 * (fabs(im) * (sqrt(2.0) / sqrt(re * -2.0)));
} else if (im <= 7.019789919272511e-278) {
tmp = 0.5 * (2.0 * sqrt(re));
} else if (im <= 8.074975228724846e-250) {
tmp = 0.5 * (fabs(im) * (sqrt(2.0) / sqrt(re * -2.0)));
} else if (im <= 2.10816646233924e-162) {
tmp = 0.5 * (2.0 * sqrt(re));
} else if (im <= 3.235045464145441e-105) {
tmp = 0.5 * (fabs(im) * sqrt(2.0 / (sqrt((re * re) + (im * im)) - re)));
} else if (im <= 1.1890627631191766e-70) {
tmp = 0.5 * (2.0 * sqrt(re));
} else if (im <= 3.279790948248637e+82) {
tmp = 0.5 * sqrt(2.0 * (re + sqrt((re * re) + (im * im))));
} else {
tmp = 0.5 * (fabs(im) * (sqrt(2.0) / sqrt(im - re)));
}
return tmp;
}




Bits error versus re




Bits error versus im
Results
| Original | 39.0 |
|---|---|
| Target | 33.7 |
| Herbie | 22.9 |
if im < -3.0199405747825176e77Initial program 49.6
Taylor expanded around -inf 10.9
if -3.0199405747825176e77 < im < -1.5611223038770802e-162 or 1.18906276311917661e-70 < im < 3.2797909482486371e82Initial program 25.5
if -1.5611223038770802e-162 < im < -7.06199612715254226e-306 or 7.0197899192725114e-278 < im < 8.0749752287248463e-250Initial program 44.9
rmApplied flip-+_binary64_209860.5
Applied associate-*r/_binary64_206660.5
Applied sqrt-div_binary64_214160.8
Simplified53.5
rmApplied *-un-lft-identity_binary64_212453.5
Applied sqrt-prod_binary64_214053.5
Applied sqrt-prod_binary64_214053.5
Applied times-frac_binary64_213053.5
Simplified38.2
Simplified38.2
Taylor expanded around -inf 34.0
Simplified34.0
if -7.06199612715254226e-306 < im < 7.0197899192725114e-278 or 8.0749752287248463e-250 < im < 2.10816646233923992e-162 or 3.2350454641454411e-105 < im < 1.18906276311917661e-70Initial program 41.0
Taylor expanded around 0 37.0
Simplified36.4
if 2.10816646233923992e-162 < im < 3.2350454641454411e-105Initial program 32.0
rmApplied flip-+_binary64_209846.2
Applied associate-*r/_binary64_206646.2
Applied sqrt-div_binary64_214146.3
Simplified30.2
rmApplied *-un-lft-identity_binary64_212430.2
Applied sqrt-prod_binary64_214030.2
Applied sqrt-prod_binary64_214030.2
Applied times-frac_binary64_213030.2
Simplified29.3
Simplified29.3
rmApplied sqrt-undiv_binary64_214529.2
if 3.2797909482486371e82 < im Initial program 47.9
rmApplied flip-+_binary64_209848.6
Applied associate-*r/_binary64_206648.6
Applied sqrt-div_binary64_214148.7
Simplified47.7
rmApplied *-un-lft-identity_binary64_212447.7
Applied sqrt-prod_binary64_214047.7
Applied sqrt-prod_binary64_214047.7
Applied times-frac_binary64_213047.7
Simplified46.7
Simplified46.7
Taylor expanded around inf 10.8
Final simplification22.9
herbie shell --seed 2020357
(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)))))