0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\begin{array}{l}
\mathbf{if}\;re \leq -4.324366802345398 \cdot 10^{+121}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{elif}\;re \leq -7.62416769797486 \cdot 10^{+69}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
\mathbf{elif}\;re \leq -1.5169450701399806 \cdot 10^{-156}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{\sqrt{re \cdot re + im \cdot im}} \cdot \sqrt{\sqrt{re \cdot re + im \cdot im}} - re\right)}\\
\mathbf{elif}\;re \leq 4.9416823499771336 \cdot 10^{-86}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(im - re\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{\sqrt{2 \cdot \left(im \cdot im\right)}}{\sqrt{re + \sqrt{re \cdot re + im \cdot im}}}\\
\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 -4.324366802345398e+121)
(* 0.5 (sqrt (* 2.0 (* re -2.0))))
(if (<= re -7.62416769797486e+69)
(* 0.5 (sqrt (* 2.0 (- im re))))
(if (<= re -1.5169450701399806e-156)
(*
0.5
(sqrt
(*
2.0
(-
(*
(sqrt (sqrt (+ (* re re) (* im im))))
(sqrt (sqrt (+ (* re re) (* im im)))))
re))))
(if (<= re 4.9416823499771336e-86)
(* 0.5 (sqrt (* 2.0 (- im re))))
(*
0.5
(/
(sqrt (* 2.0 (* im im)))
(sqrt (+ 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 <= -4.324366802345398e+121) {
tmp = 0.5 * sqrt(2.0 * (re * -2.0));
} else if (re <= -7.62416769797486e+69) {
tmp = 0.5 * sqrt(2.0 * (im - re));
} else if (re <= -1.5169450701399806e-156) {
tmp = 0.5 * sqrt(2.0 * ((sqrt(sqrt((re * re) + (im * im))) * sqrt(sqrt((re * re) + (im * im)))) - re));
} else if (re <= 4.9416823499771336e-86) {
tmp = 0.5 * sqrt(2.0 * (im - re));
} else {
tmp = 0.5 * (sqrt(2.0 * (im * im)) / sqrt(re + sqrt((re * re) + (im * im))));
}
return tmp;
}



Bits error versus re



Bits error versus im
Results
if re < -4.32436680234539771e121Initial program 55.1
Taylor expanded around -inf 9.7
Simplified9.7
if -4.32436680234539771e121 < re < -7.6241676979748601e69 or -1.5169450701399806e-156 < re < 4.9416823499771336e-86Initial program 29.5
Taylor expanded around 0 12.3
if -7.6241676979748601e69 < re < -1.5169450701399806e-156Initial program 15.7
rmApplied add-sqr-sqrt_binary6415.7
Applied sqrt-prod_binary6415.8
if 4.9416823499771336e-86 < re Initial program 53.8
rmApplied flip--_binary6453.8
Applied associate-*r/_binary6453.8
Applied sqrt-div_binary6453.9
Simplified37.9
Simplified37.9
Final simplification20.7
herbie shell --seed 2020224
(FPCore (re im)
:name "math.sqrt on complex, imaginary part, im greater than 0 branch"
:precision binary64
:pre (> im 0.0)
(* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))