0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\begin{array}{l}
\mathbf{if}\;re \leq -1.3360753823747522 \cdot 10^{+154}:\\
\;\;\;\;0.5 \cdot \left(\left(\sqrt{\left|im\right|} \cdot \sqrt{\sqrt{2}}\right) \cdot \frac{\sqrt{\sqrt{2}}}{\frac{\sqrt{\left(-re\right) - re}}{\sqrt{\left|im\right|}}}\right)\\
\mathbf{elif}\;re \leq 6.993600331950945 \cdot 10^{-261}:\\
\;\;\;\;0.5 \cdot \left(\sqrt{\left|im\right| \cdot \sqrt{2}} \cdot \frac{\sqrt{\sqrt{2}}}{\frac{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}{\sqrt{\left|im\right|}}}\right)\\
\mathbf{elif}\;re \leq 9.000466420424405 \cdot 10^{-202}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \left(im + 0.5 \cdot \frac{re \cdot re}{im}\right)\right)}\\
\mathbf{elif}\;re \leq 1.0073950938736855 \cdot 10^{-164} \lor \neg \left(re \leq 2.632424878302857 \cdot 10^{+113}\right):\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot 2\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\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.3360753823747522e+154)
(*
0.5
(*
(* (sqrt (fabs im)) (sqrt (sqrt 2.0)))
(/ (sqrt (sqrt 2.0)) (/ (sqrt (- (- re) re)) (sqrt (fabs im))))))
(if (<= re 6.993600331950945e-261)
(*
0.5
(*
(sqrt (* (fabs im) (sqrt 2.0)))
(/
(sqrt (sqrt 2.0))
(/ (sqrt (- (sqrt (+ (* re re) (* im im))) re)) (sqrt (fabs im))))))
(if (<= re 9.000466420424405e-202)
(* 0.5 (sqrt (* 2.0 (+ re (+ im (* 0.5 (/ (* re re) im)))))))
(if (or (<= re 1.0073950938736855e-164)
(not (<= re 2.632424878302857e+113)))
(* 0.5 (sqrt (* 2.0 (* re 2.0))))
(* 0.5 (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im))))))))))))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.3360753823747522e+154) {
tmp = 0.5 * ((sqrt(fabs(im)) * sqrt(sqrt(2.0))) * (sqrt(sqrt(2.0)) / (sqrt(-re - re) / sqrt(fabs(im)))));
} else if (re <= 6.993600331950945e-261) {
tmp = 0.5 * (sqrt(fabs(im) * sqrt(2.0)) * (sqrt(sqrt(2.0)) / (sqrt(sqrt((re * re) + (im * im)) - re) / sqrt(fabs(im)))));
} else if (re <= 9.000466420424405e-202) {
tmp = 0.5 * sqrt(2.0 * (re + (im + (0.5 * ((re * re) / im)))));
} else if ((re <= 1.0073950938736855e-164) || !(re <= 2.632424878302857e+113)) {
tmp = 0.5 * sqrt(2.0 * (re * 2.0));
} else {
tmp = 0.5 * sqrt(2.0 * (re + sqrt((re * re) + (im * im))));
}
return tmp;
}




Bits error versus re




Bits error versus im
Results
| Original | 38.3 |
|---|---|
| Target | 33.2 |
| Herbie | 18.1 |
if re < -1.3360753823747522e154Initial program 64.0
rmApplied flip-+_binary64_414464.0
Applied associate-*r/_binary64_411264.0
Applied sqrt-div_binary64_418764.0
Simplified48.6
rmApplied sqrt-prod_binary64_418648.6
Applied associate-/l*_binary64_411548.6
Simplified48.2
rmApplied add-sqr-sqrt_binary64_419248.2
Applied *-un-lft-identity_binary64_417048.2
Applied sqrt-prod_binary64_418648.2
Applied times-frac_binary64_417648.2
Applied add-sqr-sqrt_binary64_419248.2
Applied times-frac_binary64_417648.2
Simplified48.2
Taylor expanded around -inf 7.3
if -1.3360753823747522e154 < re < 6.993600331950945e-261Initial program 39.4
rmApplied flip-+_binary64_414439.3
Applied associate-*r/_binary64_411239.3
Applied sqrt-div_binary64_418739.5
Simplified30.2
rmApplied sqrt-prod_binary64_418630.2
Applied associate-/l*_binary64_411530.2
Simplified21.2
rmApplied add-sqr-sqrt_binary64_419221.3
Applied *-un-lft-identity_binary64_417021.3
Applied sqrt-prod_binary64_418621.3
Applied times-frac_binary64_417621.3
Applied add-sqr-sqrt_binary64_419221.3
Applied times-frac_binary64_417621.1
Simplified21.1
rmApplied sqrt-unprod_binary64_419021.0
if 6.993600331950945e-261 < re < 9.00046642042440462e-202Initial program 29.7
Taylor expanded around 0 33.2
Simplified33.2
if 9.00046642042440462e-202 < re < 1.00739509387369e-164 or 2.63242487830285688e113 < re Initial program 49.0
Taylor expanded around inf 17.8
Simplified17.8
if 1.00739509387369e-164 < re < 2.63242487830285688e113Initial program 15.5
rmApplied pow1_binary64_423115.5
Final simplification18.1
herbie shell --seed 2021075
(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)))))