| Alternative 1 | |
|---|---|
| Accuracy | 52.5% |
| Cost | 13908 |
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im)
:precision binary64
(if (<= re -2.9e+162)
(sqrt (* (* im (/ im re)) -0.25))
(if (or (<= re -5.3e+91) (not (<= re -2.2e+53)))
(* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))
(*
0.5
(*
(pow
(exp (* 0.25 (+ (log (/ -1.0 re)) (log (* 0.5 (pow im 2.0))))))
2.0)
(sqrt 2.0))))))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 <= -2.9e+162) {
tmp = sqrt(((im * (im / re)) * -0.25));
} else if ((re <= -5.3e+91) || !(re <= -2.2e+53)) {
tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
} else {
tmp = 0.5 * (pow(exp((0.25 * (log((-1.0 / re)) + log((0.5 * pow(im, 2.0)))))), 2.0) * sqrt(2.0));
}
return tmp;
}
public static double code(double re, double im) {
return 0.5 * Math.sqrt((2.0 * (Math.sqrt(((re * re) + (im * im))) + re)));
}
public static double code(double re, double im) {
double tmp;
if (re <= -2.9e+162) {
tmp = Math.sqrt(((im * (im / re)) * -0.25));
} else if ((re <= -5.3e+91) || !(re <= -2.2e+53)) {
tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
} else {
tmp = 0.5 * (Math.pow(Math.exp((0.25 * (Math.log((-1.0 / re)) + Math.log((0.5 * Math.pow(im, 2.0)))))), 2.0) * Math.sqrt(2.0));
}
return tmp;
}
def code(re, im): return 0.5 * math.sqrt((2.0 * (math.sqrt(((re * re) + (im * im))) + re)))
def code(re, im): tmp = 0 if re <= -2.9e+162: tmp = math.sqrt(((im * (im / re)) * -0.25)) elif (re <= -5.3e+91) or not (re <= -2.2e+53): tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im)))) else: tmp = 0.5 * (math.pow(math.exp((0.25 * (math.log((-1.0 / re)) + math.log((0.5 * math.pow(im, 2.0)))))), 2.0) * math.sqrt(2.0)) return tmp
function code(re, im) return Float64(0.5 * sqrt(Float64(2.0 * Float64(sqrt(Float64(Float64(re * re) + Float64(im * im))) + re)))) end
function code(re, im) tmp = 0.0 if (re <= -2.9e+162) tmp = sqrt(Float64(Float64(im * Float64(im / re)) * -0.25)); elseif ((re <= -5.3e+91) || !(re <= -2.2e+53)) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im))))); else tmp = Float64(0.5 * Float64((exp(Float64(0.25 * Float64(log(Float64(-1.0 / re)) + log(Float64(0.5 * (im ^ 2.0)))))) ^ 2.0) * sqrt(2.0))); end return tmp end
function tmp = code(re, im) tmp = 0.5 * sqrt((2.0 * (sqrt(((re * re) + (im * im))) + re))); end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= -2.9e+162) tmp = sqrt(((im * (im / re)) * -0.25)); elseif ((re <= -5.3e+91) || ~((re <= -2.2e+53))) tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im)))); else tmp = 0.5 * ((exp((0.25 * (log((-1.0 / re)) + log((0.5 * (im ^ 2.0)))))) ^ 2.0) * sqrt(2.0)); end tmp_2 = tmp; end
code[re_, im_] := N[(0.5 * N[Sqrt[N[(2.0 * N[(N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
code[re_, im_] := If[LessEqual[re, -2.9e+162], N[Sqrt[N[(N[(im * N[(im / re), $MachinePrecision]), $MachinePrecision] * -0.25), $MachinePrecision]], $MachinePrecision], If[Or[LessEqual[re, -5.3e+91], N[Not[LessEqual[re, -2.2e+53]], $MachinePrecision]], N[(0.5 * N[Sqrt[N[(2.0 * N[(re + N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(N[Power[N[Exp[N[(0.25 * N[(N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision] + N[Log[N[(0.5 * N[Power[im, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 2.0], $MachinePrecision] * N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\begin{array}{l}
\mathbf{if}\;re \leq -2.9 \cdot 10^{+162}:\\
\;\;\;\;\sqrt{\left(im \cdot \frac{im}{re}\right) \cdot -0.25}\\
\mathbf{elif}\;re \leq -5.3 \cdot 10^{+91} \lor \neg \left(re \leq -2.2 \cdot 10^{+53}\right):\\
\;\;\;\;0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left({\left(e^{0.25 \cdot \left(\log \left(\frac{-1}{re}\right) + \log \left(0.5 \cdot {im}^{2}\right)\right)}\right)}^{2} \cdot \sqrt{2}\right)\\
\end{array}
Results
| Original | 40.0% |
|---|---|
| Target | 47.8% |
| Herbie | 82.2% |
if re < -2.90000000000000006e162Initial program 0.0%
Simplified34.3%
[Start]0.0 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\] |
|---|---|
+-commutative [=>]0.0 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}}
\] |
hypot-def [=>]34.3 | \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)}
\] |
Taylor expanded in re around -inf 50.1%
Simplified50.1%
[Start]50.1 | \[ 0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}
\] |
|---|---|
*-commutative [=>]50.1 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\frac{{im}^{2}}{re} \cdot -0.5\right)}}
\] |
unpow2 [=>]50.1 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\frac{\color{blue}{im \cdot im}}{re} \cdot -0.5\right)}
\] |
Applied egg-rr50.0%
[Start]50.1 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}
\] |
|---|---|
add-sqr-sqrt [=>]49.9 | \[ \color{blue}{\sqrt{0.5 \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}} \cdot \sqrt{0.5 \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}}}
\] |
sqrt-unprod [=>]50.0 | \[ \color{blue}{\sqrt{\left(0.5 \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}\right) \cdot \left(0.5 \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}\right)}}
\] |
*-commutative [=>]50.0 | \[ \sqrt{\color{blue}{\left(\sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)} \cdot 0.5\right)} \cdot \left(0.5 \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}\right)}
\] |
*-commutative [=>]50.0 | \[ \sqrt{\left(\sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)} \cdot 0.5\right) \cdot \color{blue}{\left(\sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)} \cdot 0.5\right)}}
\] |
swap-sqr [=>]50.0 | \[ \sqrt{\color{blue}{\left(\sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)} \cdot \sqrt{2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)}\right) \cdot \left(0.5 \cdot 0.5\right)}}
\] |
add-sqr-sqrt [<=]50.0 | \[ \sqrt{\color{blue}{\left(2 \cdot \left(\frac{im \cdot im}{re} \cdot -0.5\right)\right)} \cdot \left(0.5 \cdot 0.5\right)}
\] |
*-commutative [=>]50.0 | \[ \sqrt{\left(2 \cdot \color{blue}{\left(-0.5 \cdot \frac{im \cdot im}{re}\right)}\right) \cdot \left(0.5 \cdot 0.5\right)}
\] |
associate-*r* [=>]50.0 | \[ \sqrt{\color{blue}{\left(\left(2 \cdot -0.5\right) \cdot \frac{im \cdot im}{re}\right)} \cdot \left(0.5 \cdot 0.5\right)}
\] |
associate-*r/ [=>]50.0 | \[ \sqrt{\color{blue}{\frac{\left(2 \cdot -0.5\right) \cdot \left(im \cdot im\right)}{re}} \cdot \left(0.5 \cdot 0.5\right)}
\] |
metadata-eval [=>]50.0 | \[ \sqrt{\frac{\color{blue}{-1} \cdot \left(im \cdot im\right)}{re} \cdot \left(0.5 \cdot 0.5\right)}
\] |
neg-mul-1 [<=]50.0 | \[ \sqrt{\frac{\color{blue}{-im \cdot im}}{re} \cdot \left(0.5 \cdot 0.5\right)}
\] |
distribute-rgt-neg-in [=>]50.0 | \[ \sqrt{\frac{\color{blue}{im \cdot \left(-im\right)}}{re} \cdot \left(0.5 \cdot 0.5\right)}
\] |
metadata-eval [=>]50.0 | \[ \sqrt{\frac{im \cdot \left(-im\right)}{re} \cdot \color{blue}{0.25}}
\] |
Simplified62.6%
[Start]50.0 | \[ \sqrt{\frac{im \cdot \left(-im\right)}{re} \cdot 0.25}
\] |
|---|---|
distribute-rgt-neg-out [=>]50.0 | \[ \sqrt{\frac{\color{blue}{-im \cdot im}}{re} \cdot 0.25}
\] |
distribute-frac-neg [=>]50.0 | \[ \sqrt{\color{blue}{\left(-\frac{im \cdot im}{re}\right)} \cdot 0.25}
\] |
associate-*r/ [<=]62.6 | \[ \sqrt{\left(-\color{blue}{im \cdot \frac{im}{re}}\right) \cdot 0.25}
\] |
mul-1-neg [<=]62.6 | \[ \sqrt{\color{blue}{\left(-1 \cdot \left(im \cdot \frac{im}{re}\right)\right)} \cdot 0.25}
\] |
*-commutative [=>]62.6 | \[ \sqrt{\color{blue}{\left(\left(im \cdot \frac{im}{re}\right) \cdot -1\right)} \cdot 0.25}
\] |
associate-*l* [=>]62.6 | \[ \sqrt{\color{blue}{\left(im \cdot \frac{im}{re}\right) \cdot \left(-1 \cdot 0.25\right)}}
\] |
metadata-eval [=>]62.6 | \[ \sqrt{\left(im \cdot \frac{im}{re}\right) \cdot \color{blue}{-0.25}}
\] |
if -2.90000000000000006e162 < re < -5.29999999999999997e91 or -2.19999999999999999e53 < re Initial program 46.2%
Simplified86.5%
[Start]46.2 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\] |
|---|---|
+-commutative [=>]46.2 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}}
\] |
hypot-def [=>]86.5 | \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)}
\] |
if -5.29999999999999997e91 < re < -2.19999999999999999e53Initial program 21.2%
Simplified47.7%
[Start]21.2 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\] |
|---|---|
+-commutative [=>]21.2 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}}
\] |
hypot-def [=>]47.7 | \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)}
\] |
Applied egg-rr47.4%
[Start]47.7 | \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}
\] |
|---|---|
sqrt-prod [=>]47.4 | \[ 0.5 \cdot \color{blue}{\left(\sqrt{2} \cdot \sqrt{re + \mathsf{hypot}\left(re, im\right)}\right)}
\] |
*-commutative [=>]47.4 | \[ 0.5 \cdot \color{blue}{\left(\sqrt{re + \mathsf{hypot}\left(re, im\right)} \cdot \sqrt{2}\right)}
\] |
Applied egg-rr47.3%
[Start]47.4 | \[ 0.5 \cdot \left(\sqrt{re + \mathsf{hypot}\left(re, im\right)} \cdot \sqrt{2}\right)
\] |
|---|---|
add-sqr-sqrt [=>]47.2 | \[ 0.5 \cdot \left(\color{blue}{\left(\sqrt{\sqrt{re + \mathsf{hypot}\left(re, im\right)}} \cdot \sqrt{\sqrt{re + \mathsf{hypot}\left(re, im\right)}}\right)} \cdot \sqrt{2}\right)
\] |
pow2 [=>]47.2 | \[ 0.5 \cdot \left(\color{blue}{{\left(\sqrt{\sqrt{re + \mathsf{hypot}\left(re, im\right)}}\right)}^{2}} \cdot \sqrt{2}\right)
\] |
pow1/2 [=>]47.2 | \[ 0.5 \cdot \left({\left(\sqrt{\color{blue}{{\left(re + \mathsf{hypot}\left(re, im\right)\right)}^{0.5}}}\right)}^{2} \cdot \sqrt{2}\right)
\] |
sqrt-pow1 [=>]47.3 | \[ 0.5 \cdot \left({\color{blue}{\left({\left(re + \mathsf{hypot}\left(re, im\right)\right)}^{\left(\frac{0.5}{2}\right)}\right)}}^{2} \cdot \sqrt{2}\right)
\] |
metadata-eval [=>]47.3 | \[ 0.5 \cdot \left({\left({\left(re + \mathsf{hypot}\left(re, im\right)\right)}^{\color{blue}{0.25}}\right)}^{2} \cdot \sqrt{2}\right)
\] |
Taylor expanded in re around -inf 40.9%
Final simplification82.2%
| Alternative 1 | |
|---|---|
| Accuracy | 52.5% |
| Cost | 13908 |
| Alternative 2 | |
|---|---|
| Accuracy | 52.5% |
| Cost | 13780 |
| Alternative 3 | |
|---|---|
| Accuracy | 82.4% |
| Cost | 13444 |
| Alternative 4 | |
|---|---|
| Accuracy | 52.3% |
| Cost | 7772 |
| Alternative 5 | |
|---|---|
| Accuracy | 52.5% |
| Cost | 7772 |
| Alternative 6 | |
|---|---|
| Accuracy | 51.9% |
| Cost | 7644 |
| Alternative 7 | |
|---|---|
| Accuracy | 59.5% |
| Cost | 6984 |
| Alternative 8 | |
|---|---|
| Accuracy | 43.5% |
| Cost | 6852 |
| Alternative 9 | |
|---|---|
| Accuracy | 27.7% |
| Cost | 6720 |
herbie shell --seed 2023131
(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)))))