0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\begin{array}{l}
\mathbf{if}\;re \leq -1.2016967219475596 \cdot 10^{+114}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re \cdot -2\right)}\\
\mathbf{elif}\;re \leq -5.097372440926994 \cdot 10^{-137}:\\
\;\;\;\;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 2.3022147910728526 \cdot 10^{+34}:\\
\;\;\;\;0.5 \cdot \left(\left(\sqrt[3]{\sqrt{2}} \cdot \sqrt[3]{\sqrt{2}}\right) \cdot \left(\sqrt[3]{\sqrt{2}} \cdot \sqrt{im - re}\right)\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 -1.2016967219475596e+114)
(* 0.5 (sqrt (* 2.0 (* re -2.0))))
(if (<= re -5.097372440926994e-137)
(*
0.5
(sqrt
(*
2.0
(-
(*
(sqrt (sqrt (+ (* re re) (* im im))))
(sqrt (sqrt (+ (* re re) (* im im)))))
re))))
(if (<= re 2.3022147910728526e+34)
(*
0.5
(*
(* (cbrt (sqrt 2.0)) (cbrt (sqrt 2.0)))
(* (cbrt (sqrt 2.0)) (sqrt (- 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 <= -1.2016967219475596e+114) {
tmp = 0.5 * sqrt(2.0 * (re * -2.0));
} else if (re <= -5.097372440926994e-137) {
tmp = 0.5 * sqrt(2.0 * ((sqrt(sqrt((re * re) + (im * im))) * sqrt(sqrt((re * re) + (im * im)))) - re));
} else if (re <= 2.3022147910728526e+34) {
tmp = 0.5 * ((cbrt(sqrt(2.0)) * cbrt(sqrt(2.0))) * (cbrt(sqrt(2.0)) * sqrt(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 < -1.2016967219475596e114Initial program 54.1
Taylor expanded around -inf 9.4
Simplified9.4
if -1.2016967219475596e114 < re < -5.09737244092699391e-137Initial program 14.9
rmApplied add-sqr-sqrt_binary6415.0
if -5.09737244092699391e-137 < re < 2.30221479107285261e34Initial program 33.3
Taylor expanded around 0 14.2
rmApplied sqrt-prod_binary6414.5
rmApplied add-cube-cbrt_binary6414.5
Applied associate-*l*_binary6414.5
Simplified14.5
if 2.30221479107285261e34 < re Initial program 58.6
rmApplied flip--_binary6458.6
Applied associate-*r/_binary6458.6
Applied sqrt-div_binary6458.6
Simplified39.7
Simplified39.7
Final simplification19.1
herbie shell --seed 2020232
(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)))))