0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\begin{array}{l}
\mathbf{if}\;re \leq -0.00017542879764735815:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \frac{im}{\frac{re}{im}}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\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 -0.00017542879764735815) (* 0.5 (sqrt (* 2.0 (* -0.5 (/ im (/ re im)))))) (* 0.5 (sqrt (* 2.0 (+ re (hypot re 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 <= -0.00017542879764735815) {
tmp = 0.5 * sqrt(2.0 * (-0.5 * (im / (re / im))));
} else {
tmp = 0.5 * sqrt(2.0 * (re + hypot(re, im)));
}
return tmp;
}




Bits error versus re




Bits error versus im
Results
| Original | 38.4 |
|---|---|
| Target | 33.3 |
| Herbie | 11.1 |
if re < -1.7542879764735815e-4Initial program 57.2
Simplified38.2
Taylor expanded in re around -inf 34.8
Applied unpow2_binary6434.8
Applied associate-/l*_binary6430.8
if -1.7542879764735815e-4 < re Initial program 32.1
Simplified4.4
Final simplification11.1
herbie shell --seed 2021340
(FPCore (re im)
:name "math.sqrt on complex, real part"
:precision binary64
:herbie-target
(if (< re 0.0) (* 0.5 (* (sqrt 2.0) (sqrt (/ (* im im) (- (sqrt (+ (* re re) (* im im))) re))))) (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))