0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\begin{array}{l}
\mathbf{if}\;re \leq -1.3361067715788965 \cdot 10^{+93}:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \frac{\left|im\right|}{\sqrt{\left(-re\right) - re}}\right)\\
\mathbf{elif}\;re \leq -3.4201707617626984 \cdot 10^{-214}:\\
\;\;\;\;0.5 \cdot \left(\left(\sqrt{2} \cdot \left|im\right|\right) \cdot \frac{1}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}\right)\\
\mathbf{elif}\;re \leq 3.1846958944297933 \cdot 10^{-243}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\
\mathbf{elif}\;re \leq 4.484644676042966 \cdot 10^{+96}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{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 -1.3361067715788965e+93)
(* 0.5 (* (sqrt 2.0) (/ (fabs im) (sqrt (- (- re) re)))))
(if (<= re -3.4201707617626984e-214)
(*
0.5
(*
(* (sqrt 2.0) (fabs im))
(/ 1.0 (sqrt (- (sqrt (+ (* re re) (* im im))) re)))))
(if (<= re 3.1846958944297933e-243)
(* 0.5 (sqrt (* 2.0 (- re im))))
(if (<= re 4.484644676042966e+96)
(* 0.5 (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))))
(* 0.5 (* 2.0 (sqrt 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 <= -1.3361067715788965e+93) {
tmp = 0.5 * (sqrt(2.0) * (fabs(im) / sqrt(-re - re)));
} else if (re <= -3.4201707617626984e-214) {
tmp = 0.5 * ((sqrt(2.0) * fabs(im)) * (1.0 / sqrt(sqrt((re * re) + (im * im)) - re)));
} else if (re <= 3.1846958944297933e-243) {
tmp = 0.5 * sqrt(2.0 * (re - im));
} else if (re <= 4.484644676042966e+96) {
tmp = 0.5 * sqrt(2.0 * (re + sqrt((re * re) + (im * im))));
} else {
tmp = 0.5 * (2.0 * sqrt(re));
}
return tmp;
}




Bits error versus re




Bits error versus im
Results
| Original | 39.1 |
|---|---|
| Target | 34.2 |
| Herbie | 18.1 |
if re < -1.33610677157889653e93Initial program 61.1
rmApplied flip-+_binary64_278061.1
Applied associate-*r/_binary64_274861.1
Applied sqrt-div_binary64_282361.1
Simplified44.0
rmApplied *-un-lft-identity_binary64_280644.0
Applied sqrt-prod_binary64_282244.0
Applied sqrt-prod_binary64_282244.0
Applied times-frac_binary64_281244.0
Simplified44.0
Simplified41.0
Taylor expanded around -inf 10.9
Simplified10.9
if -1.33610677157889653e93 < re < -3.4201707617626984e-214Initial program 40.3
rmApplied flip-+_binary64_278040.3
Applied associate-*r/_binary64_274840.3
Applied sqrt-div_binary64_282340.4
Simplified31.2
rmApplied *-un-lft-identity_binary64_280631.2
Applied sqrt-prod_binary64_282231.2
Applied sqrt-prod_binary64_282231.3
Applied times-frac_binary64_281231.3
Simplified31.3
Simplified18.8
rmApplied div-inv_binary64_280318.8
Applied associate-*r*_binary64_274618.8
Simplified18.8
if -3.4201707617626984e-214 < re < 3.1846958944297933e-243Initial program 32.2
Taylor expanded around -inf 33.3
if 3.1846958944297933e-243 < re < 4.48464467604296578e96Initial program 20.2
if 4.48464467604296578e96 < re Initial program 50.3
Taylor expanded around 0 11.3
Simplified10.3
Final simplification18.1
herbie shell --seed 2021025
(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)))))