0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\begin{array}{l}
\mathbf{if}\;re \le 1.3940086170973382 \cdot 10^{-278}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \frac{{im}^{1}}{\frac{\mathsf{hypot}\left(re, im\right) - re}{im}}}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\mathsf{hypot}\left(im, re\right) + re}\right)\\
\end{array}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 VAR;
if ((re <= 1.3940086170973382e-278)) {
VAR = (0.5 * sqrt((2.0 * (pow(im, 1.0) / ((hypot(re, im) - re) / im)))));
} else {
VAR = (0.5 * (sqrt(2.0) * sqrt((hypot(im, re) + re))));
}
return VAR;
}




Bits error versus re




Bits error versus im
Results
| Original | 38.7 |
|---|---|
| Target | 33.7 |
| Herbie | 5.6 |
if re < 1.3940086170973382e-278Initial program 44.8
rmApplied flip-+44.7
Simplified35.0
Simplified30.3
rmApplied sqr-pow30.3
Applied associate-/l*10.3
Simplified10.3
if 1.3940086170973382e-278 < re Initial program 32.2
rmApplied +-commutative32.2
Applied hypot-def0.1
rmApplied sqrt-prod0.5
Final simplification5.6
herbie shell --seed 2020078 +o rules:numerics
(FPCore (re im)
:name "math.sqrt on complex, real part"
:precision binary64
:herbie-target
(if (< re 0.0) (* 0.5 (* (sqrt 2) (sqrt (/ (* im im) (- (sqrt (+ (* re re) (* im im))) re))))) (* 0.5 (sqrt (* 2 (+ (sqrt (+ (* re re) (* im im))) re)))))
(* 0.5 (sqrt (* 2 (+ (sqrt (+ (* re re) (* im im))) re)))))