0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\begin{array}{l}
\mathbf{if}\;re \leq -2.4801779605399377 \cdot 10^{+149}:\\
\;\;\;\;0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \left(\sqrt{\sqrt{2}} \cdot \frac{\left|im\right|}{\sqrt{re \cdot -2}}\right)\right)\\
\mathbf{elif}\;re \leq -1.3429819466281329 \cdot 10^{-189}:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \frac{\left|im\right|}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}\right)\\
\mathbf{elif}\;re \leq -1.9885990575258114 \cdot 10^{-292}:\\
\;\;\;\;0.5 \cdot \left(\sqrt{\sqrt{2}} \cdot \left(\sqrt{\sqrt{2}} \cdot \frac{\left|im\right|}{\sqrt{im - re}}\right)\right)\\
\mathbf{elif}\;re \leq 1.44375492704954 \cdot 10^{-163}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re - im\right)}\\
\mathbf{elif}\;re \leq 2.3008799010294317 \cdot 10^{+129}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\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 -2.4801779605399377e+149)
(*
0.5
(*
(sqrt (sqrt 2.0))
(* (sqrt (sqrt 2.0)) (/ (fabs im) (sqrt (* re -2.0))))))
(if (<= re -1.3429819466281329e-189)
(*
0.5
(*
(sqrt 2.0)
(/ (fabs im) (sqrt (- (sqrt (+ (* re re) (* im im))) re)))))
(if (<= re -1.9885990575258114e-292)
(*
0.5
(*
(sqrt (sqrt 2.0))
(* (sqrt (sqrt 2.0)) (/ (fabs im) (sqrt (- im re))))))
(if (<= re 1.44375492704954e-163)
(* 0.5 (sqrt (* 2.0 (- re im))))
(if (<= re 2.3008799010294317e+129)
(* 0.5 (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))))
(* 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 <= -2.4801779605399377e+149) {
tmp = 0.5 * (sqrt(sqrt(2.0)) * (sqrt(sqrt(2.0)) * (fabs(im) / sqrt(re * -2.0))));
} else if (re <= -1.3429819466281329e-189) {
tmp = 0.5 * (sqrt(2.0) * (fabs(im) / sqrt(sqrt((re * re) + (im * im)) - re)));
} else if (re <= -1.9885990575258114e-292) {
tmp = 0.5 * (sqrt(sqrt(2.0)) * (sqrt(sqrt(2.0)) * (fabs(im) / sqrt(im - re))));
} else if (re <= 1.44375492704954e-163) {
tmp = 0.5 * sqrt(2.0 * (re - im));
} else if (re <= 2.3008799010294317e+129) {
tmp = 0.5 * sqrt(2.0 * (re + sqrt((re * re) + (im * im))));
} 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.5 |
| Herbie | 18.2 |
if re < -2.48017796053993768e149Initial program 63.7
rmApplied flip-+_binary64_312163.7
Applied associate-*r/_binary64_308963.7
Applied sqrt-div_binary64_316463.7
Simplified49.9
rmApplied *-un-lft-identity_binary64_314749.9
Applied sqrt-prod_binary64_316349.9
Applied sqrt-prod_binary64_316349.9
Applied times-frac_binary64_315349.9
Simplified49.9
Simplified49.4
rmApplied add-sqr-sqrt_binary64_316949.4
Applied associate-*l*_binary64_308849.4
Simplified49.4
Taylor expanded around -inf 8.0
Simplified8.0
if -2.48017796053993768e149 < re < -1.3429819466281329e-189Initial program 43.3
rmApplied flip-+_binary64_312143.2
Applied associate-*r/_binary64_308943.2
Applied sqrt-div_binary64_316443.3
Simplified29.5
rmApplied *-un-lft-identity_binary64_314729.5
Applied sqrt-prod_binary64_316329.5
Applied sqrt-prod_binary64_316329.5
Applied times-frac_binary64_315329.5
Simplified29.5
Simplified18.0
if -1.3429819466281329e-189 < re < -1.98859905752581141e-292Initial program 30.1
rmApplied flip-+_binary64_312129.4
Applied associate-*r/_binary64_308929.4
Applied sqrt-div_binary64_316429.6
Simplified29.6
rmApplied *-un-lft-identity_binary64_314729.6
Applied sqrt-prod_binary64_316329.6
Applied sqrt-prod_binary64_316329.7
Applied times-frac_binary64_315329.7
Simplified29.7
Simplified27.8
rmApplied add-sqr-sqrt_binary64_316927.8
Applied associate-*l*_binary64_308827.8
Simplified27.8
Taylor expanded around 0 35.0
if -1.98859905752581141e-292 < re < 1.44375492704953985e-163Initial program 29.5
Taylor expanded around -inf 34.0
if 1.44375492704953985e-163 < re < 2.30087990102943175e129Initial program 16.1
if 2.30087990102943175e129 < re Initial program 56.7
Taylor expanded around inf 7.8
Final simplification18.2
herbie shell --seed 2021058
(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)))))