0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\begin{array}{l}
\mathbf{if}\;re \leq -6.500985051720089 \cdot 10^{+126}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{elif}\;re \leq -5.707305022353321 \cdot 10^{-43}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\\
\mathbf{elif}\;re \leq 6.477992946618128 \cdot 10^{+80}:\\
\;\;\;\;0.5 \cdot \frac{im \cdot \sqrt{2}}{\sqrt{re + im}}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \frac{im \cdot \sqrt{2}}{\sqrt{re + 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 -6.500985051720089e+126)
(* 0.5 (sqrt (* 2.0 (* re -2.0))))
(if (<= re -5.707305022353321e-43)
(* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re))))
(if (<= re 6.477992946618128e+80)
(* 0.5 (/ (* im (sqrt 2.0)) (sqrt (+ re im))))
(* 0.5 (/ (* im (sqrt 2.0)) (sqrt (+ 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 <= -6.500985051720089e+126) {
tmp = 0.5 * sqrt(2.0 * (re * -2.0));
} else if (re <= -5.707305022353321e-43) {
tmp = 0.5 * sqrt(2.0 * (sqrt((re * re) + (im * im)) - re));
} else if (re <= 6.477992946618128e+80) {
tmp = 0.5 * ((im * sqrt(2.0)) / sqrt(re + im));
} else {
tmp = 0.5 * ((im * sqrt(2.0)) / sqrt(re + re));
}
return tmp;
}



Bits error versus re



Bits error versus im
Results
if re < -6.5009850517200892e126Initial program 56.2
Taylor expanded around -inf 9.3
Simplified9.3
if -6.5009850517200892e126 < re < -5.707305022353321e-43Initial program 16.0
if -5.707305022353321e-43 < re < 6.47799294661812758e80Initial program 31.5
rmApplied flip--_binary64_39435.2
Applied associate-*r/_binary64_36135.2
Applied sqrt-div_binary64_43635.5
Simplified31.4
Simplified31.4
Taylor expanded around 0 26.0
Taylor expanded around 0 14.6
if 6.47799294661812758e80 < re Initial program 60.5
rmApplied flip--_binary64_39460.5
Applied associate-*r/_binary64_36160.5
Applied sqrt-div_binary64_43660.6
Simplified42.7
Simplified42.7
Taylor expanded around 0 38.9
Taylor expanded around inf 11.3
Final simplification13.4
herbie shell --seed 2021009
(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)))))