0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\begin{array}{l}
\mathbf{if}\;re \leq -1.199222094282219 \cdot 10^{+126}:\\
\;\;\;\;0.5 \cdot \left(\left|im\right| \cdot \frac{\sqrt{2}}{\sqrt{re \cdot -2}}\right)\\
\mathbf{elif}\;re \leq -1.4106756593205677 \cdot 10^{-77}:\\
\;\;\;\;0.5 \cdot \left(\left|im\right| \cdot \left(\sqrt{\sqrt{2}} \cdot \frac{\sqrt{\sqrt{2}}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}\right)\right)\\
\mathbf{elif}\;re \leq 3.214172448806604 \cdot 10^{-141}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \left|\frac{re \cdot re}{im} \cdot -0.5 - im\right|\right)}\\
\mathbf{elif}\;re \leq 1.809200894956933 \cdot 10^{+46}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \sqrt{re \cdot re + im \cdot im}\right)}\\
\mathbf{elif}\;re \leq 9.542964692004831 \cdot 10^{+126}:\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \left|im\right|\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(2 \cdot \sqrt{re}\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 -1.199222094282219e+126)
(* 0.5 (* (fabs im) (/ (sqrt 2.0) (sqrt (* re -2.0)))))
(if (<= re -1.4106756593205677e-77)
(*
0.5
(*
(fabs im)
(*
(sqrt (sqrt 2.0))
(/ (sqrt (sqrt 2.0)) (sqrt (- (sqrt (+ (* re re) (* im im))) re))))))
(if (<= re 3.214172448806604e-141)
(* 0.5 (sqrt (* 2.0 (+ re (fabs (- (* (/ (* re re) im) -0.5) im))))))
(if (<= re 1.809200894956933e+46)
(* 0.5 (sqrt (* 2.0 (+ re (sqrt (+ (* re re) (* im im)))))))
(if (<= re 9.542964692004831e+126)
(* 0.5 (sqrt (* 2.0 (+ re (fabs im)))))
(* 0.5 (* 2.0 (sqrt 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 <= -1.199222094282219e+126) {
tmp = 0.5 * (fabs(im) * (sqrt(2.0) / sqrt(re * -2.0)));
} else if (re <= -1.4106756593205677e-77) {
tmp = 0.5 * (fabs(im) * (sqrt(sqrt(2.0)) * (sqrt(sqrt(2.0)) / sqrt(sqrt((re * re) + (im * im)) - re))));
} else if (re <= 3.214172448806604e-141) {
tmp = 0.5 * sqrt(2.0 * (re + fabs((((re * re) / im) * -0.5) - im)));
} else if (re <= 1.809200894956933e+46) {
tmp = 0.5 * sqrt(2.0 * (re + sqrt((re * re) + (im * im))));
} else if (re <= 9.542964692004831e+126) {
tmp = 0.5 * sqrt(2.0 * (re + fabs(im)));
} else {
tmp = 0.5 * (2.0 * sqrt(re));
}
return tmp;
}


















Bits error versus re


















Bits error versus im
Results
| Original | 38.5 |
|---|---|
| Target | 33.1 |
| Herbie | 13.1 |
| Alternative 1 | |
|---|---|
| Error | 13.1 |
| Cost | 20802 |
| Alternative 2 | |
|---|---|
| Error | 14.9 |
| Cost | 20097 |
| Alternative 3 | |
|---|---|
| Error | 17.1 |
| Cost | 14595 |
| Alternative 4 | |
|---|---|
| Error | 17.4 |
| Cost | 14595 |
| Alternative 5 | |
|---|---|
| Error | 19.0 |
| Cost | 14029 |
| Alternative 6 | |
|---|---|
| Error | 20.7 |
| Cost | 14029 |
| Alternative 7 | |
|---|---|
| Error | 25.2 |
| Cost | 8002 |
| Alternative 8 | |
|---|---|
| Error | 25.2 |
| Cost | 7490 |
| Alternative 9 | |
|---|---|
| Error | 25.5 |
| Cost | 7490 |
| Alternative 10 | |
|---|---|
| Error | 25.8 |
| Cost | 7362 |
| Alternative 11 | |
|---|---|
| Error | 35.9 |
| Cost | 7362 |
| Alternative 12 | |
|---|---|
| Error | 44.1 |
| Cost | 7362 |
| Alternative 13 | |
|---|---|
| Error | 58.2 |
| Cost | 385 |
| Alternative 14 | |
|---|---|
| Error | 60.0 |
| Cost | 64 |

if re < -1.19922209428221903e126Initial program 62.3
rmApplied flip-+_binary64_278062.3
Applied associate-*r/_binary64_274862.3
Applied sqrt-div_binary64_282362.3
Simplified44.6
rmApplied *-un-lft-identity_binary64_280644.6
Applied sqrt-prod_binary64_282244.6
Applied sqrt-prod_binary64_282244.6
Applied times-frac_binary64_281244.6
Simplified42.9
Taylor expanded around -inf 9.3
Simplified9.3
Simplified9.3
if -1.19922209428221903e126 < re < -1.41067565932056774e-77Initial program 47.2
rmApplied flip-+_binary64_278047.2
Applied associate-*r/_binary64_274847.2
Applied sqrt-div_binary64_282347.3
Simplified29.3
rmApplied *-un-lft-identity_binary64_280629.3
Applied sqrt-prod_binary64_282229.3
Applied sqrt-prod_binary64_282229.3
Applied times-frac_binary64_281229.3
Simplified16.9
rmApplied *-un-lft-identity_binary64_280616.9
Applied sqrt-prod_binary64_282216.9
Applied add-sqr-sqrt_binary64_282817.0
Applied times-frac_binary64_281216.9
Simplified16.9
Simplified16.9
if -1.41067565932056774e-77 < re < 3.2141724488066041e-141Initial program 29.5
rmApplied flip-+_binary64_278046.9
Simplified46.9
rmApplied add-sqr-sqrt_binary64_282846.9
Applied rem-sqrt-square_binary64_281946.9
Taylor expanded around -inf 9.6
Simplified9.6
Simplified9.6
if 3.2141724488066041e-141 < re < 1.8092008949569329e46Initial program 16.0
rmApplied +-commutative_binary64_273616.0
Simplified16.0
if 1.8092008949569329e46 < re < 9.54296469200483094e126Initial program 17.0
rmApplied flip-+_binary64_278049.5
Simplified49.5
rmApplied add-sqr-sqrt_binary64_282849.5
Applied rem-sqrt-square_binary64_281949.5
Taylor expanded around 0 32.2
Simplified32.2
if 9.54296469200483094e126 < re Initial program 57.2
Taylor expanded around 0 10.4
Simplified9.4
Simplified9.4
Final simplification13.1
herbie shell --seed 2021014
(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)))))