0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\begin{array}{l}
\mathbf{if}\;re \leq -2.539286171271089 \cdot 10^{+70}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{elif}\;re \leq 1.9328056475004172 \cdot 10^{+67}:\\
\;\;\;\;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{im \cdot im + re \cdot re}}}\\
\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.539286171271089e+70)
(* 0.5 (sqrt (* 2.0 (* re -2.0))))
(if (<= re 1.9328056475004172e+67)
(* 0.5 (sqrt (* 2.0 (- im re))))
(*
0.5
(/
(sqrt (* 2.0 (* im im)))
(sqrt (+ re (sqrt (+ (* im im) (* 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.539286171271089e+70) {
tmp = 0.5 * sqrt(2.0 * (re * -2.0));
} else if (re <= 1.9328056475004172e+67) {
tmp = 0.5 * sqrt(2.0 * (im - re));
} else {
tmp = 0.5 * (sqrt(2.0 * (im * im)) / sqrt(re + sqrt((im * im) + (re * re))));
}
return tmp;
}



Bits error versus re



Bits error versus im
Results
if re < -2.5392861712710891e70Initial program 47.2
Taylor expanded around -inf 12.6
Simplified12.6
if -2.5392861712710891e70 < re < 1.9328056475004172e67Initial program 29.8
Taylor expanded around 0 17.8
if 1.9328056475004172e67 < re Initial program 59.5
rmApplied flip--_binary6459.5
Applied associate-*r/_binary6459.5
Applied sqrt-div_binary6459.5
Simplified41.6
Simplified41.6
Final simplification21.5
herbie shell --seed 2020268
(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)))))