0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\begin{array}{l}
\mathbf{if}\;re \leq -2.2499199913069257 \cdot 10^{+76}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{elif}\;re \leq -1.9300912174625423 \cdot 10^{-42}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\\
\mathbf{elif}\;re \leq 2.5529104624380686 \cdot 10^{-61}:\\
\;\;\;\;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 -2.2499199913069257e+76)
(* 0.5 (sqrt (* 2.0 (* re -2.0))))
(if (<= re -1.9300912174625423e-42)
(* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re))))
(if (<= re 2.5529104624380686e-61)
(* 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 <= -2.2499199913069257e+76) {
tmp = 0.5 * sqrt(2.0 * (re * -2.0));
} else if (re <= -1.9300912174625423e-42) {
tmp = 0.5 * sqrt(2.0 * (sqrt((re * re) + (im * im)) - re));
} else if (re <= 2.5529104624380686e-61) {
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 < -2.24991999130692574e76Initial program 48.5
Taylor expanded around -inf 10.1
Simplified10.1
if -2.24991999130692574e76 < re < -1.9300912174625423e-42Initial program 18.9
if -1.9300912174625423e-42 < re < 2.55291046243806865e-61Initial program 27.9
Taylor expanded around 0 12.3
if 2.55291046243806865e-61 < re Initial program 54.7
rmApplied flip--_binary64_39454.7
Applied associate-*r/_binary64_36154.7
Applied sqrt-div_binary64_43654.7
Simplified38.6
Simplified38.6
Final simplification20.3
herbie shell --seed 2020352
(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)))))