| Alternative 1 | |
|---|---|
| Accuracy | 84.9% |
| Cost | 27401 |
(FPCore (re im) :precision binary64 (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))
(FPCore (re im)
:precision binary64
(if (<= re -1.02e+186)
(* 0.5 (sqrt (* im (/ (- im) re))))
(if (or (<= re -0.05) (not (<= re -1.6e-73)))
(* 0.5 (sqrt (* 2.0 (+ re (hypot re im)))))
(*
0.5
(pow (pow (exp 0.25) (+ (* 2.0 (log im)) (log (/ -1.0 re)))) 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 <= -1.02e+186) {
tmp = 0.5 * sqrt((im * (-im / re)));
} else if ((re <= -0.05) || !(re <= -1.6e-73)) {
tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im))));
} else {
tmp = 0.5 * pow(pow(exp(0.25), ((2.0 * log(im)) + log((-1.0 / re)))), 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 <= -1.02e+186) {
tmp = 0.5 * Math.sqrt((im * (-im / re)));
} else if ((re <= -0.05) || !(re <= -1.6e-73)) {
tmp = 0.5 * Math.sqrt((2.0 * (re + Math.hypot(re, im))));
} else {
tmp = 0.5 * Math.pow(Math.pow(Math.exp(0.25), ((2.0 * Math.log(im)) + Math.log((-1.0 / re)))), 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 <= -1.02e+186: tmp = 0.5 * math.sqrt((im * (-im / re))) elif (re <= -0.05) or not (re <= -1.6e-73): tmp = 0.5 * math.sqrt((2.0 * (re + math.hypot(re, im)))) else: tmp = 0.5 * math.pow(math.pow(math.exp(0.25), ((2.0 * math.log(im)) + math.log((-1.0 / re)))), 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 <= -1.02e+186) tmp = Float64(0.5 * sqrt(Float64(im * Float64(Float64(-im) / re)))); elseif ((re <= -0.05) || !(re <= -1.6e-73)) tmp = Float64(0.5 * sqrt(Float64(2.0 * Float64(re + hypot(re, im))))); else tmp = Float64(0.5 * ((exp(0.25) ^ Float64(Float64(2.0 * log(im)) + log(Float64(-1.0 / re)))) ^ 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 <= -1.02e+186) tmp = 0.5 * sqrt((im * (-im / re))); elseif ((re <= -0.05) || ~((re <= -1.6e-73))) tmp = 0.5 * sqrt((2.0 * (re + hypot(re, im)))); else tmp = 0.5 * ((exp(0.25) ^ ((2.0 * log(im)) + log((-1.0 / re)))) ^ 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, -1.02e+186], N[(0.5 * N[Sqrt[N[(im * N[((-im) / re), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[re, -0.05], N[Not[LessEqual[re, -1.6e-73]], $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[Power[N[Power[N[Exp[0.25], $MachinePrecision], N[(N[(2.0 * N[Log[im], $MachinePrecision]), $MachinePrecision] + N[Log[N[(-1.0 / re), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 2.0], $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 -1.02 \cdot 10^{+186}:\\
\;\;\;\;0.5 \cdot \sqrt{im \cdot \frac{-im}{re}}\\
\mathbf{elif}\;re \leq -0.05 \lor \neg \left(re \leq -1.6 \cdot 10^{-73}\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}\right)}^{\left(2 \cdot \log im + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}\\
\end{array}
Results
| Original | 38.7% |
|---|---|
| Target | 47.0% |
| Herbie | 80.0% |
if re < -1.01999999999999999e186Initial program 0.0%
Simplified35.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 [=>]35.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.9%
Simplified65.7%
[Start]50.9 | \[ 0.5 \cdot \sqrt{2 \cdot \left(-0.5 \cdot \frac{{im}^{2}}{re}\right)}
\] |
|---|---|
*-commutative [=>]50.9 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(\frac{{im}^{2}}{re} \cdot -0.5\right)}}
\] |
unpow2 [=>]50.9 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\frac{\color{blue}{im \cdot im}}{re} \cdot -0.5\right)}
\] |
associate-/l* [=>]65.7 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\color{blue}{\frac{im}{\frac{re}{im}}} \cdot -0.5\right)}
\] |
Taylor expanded in im around 0 50.9%
Simplified65.8%
[Start]50.9 | \[ 0.5 \cdot \sqrt{-1 \cdot \frac{{im}^{2}}{re}}
\] |
|---|---|
mul-1-neg [=>]50.9 | \[ 0.5 \cdot \sqrt{\color{blue}{-\frac{{im}^{2}}{re}}}
\] |
unpow2 [=>]50.9 | \[ 0.5 \cdot \sqrt{-\frac{\color{blue}{im \cdot im}}{re}}
\] |
associate-*r/ [<=]65.8 | \[ 0.5 \cdot \sqrt{-\color{blue}{im \cdot \frac{im}{re}}}
\] |
distribute-rgt-neg-in [=>]65.8 | \[ 0.5 \cdot \sqrt{\color{blue}{im \cdot \left(-\frac{im}{re}\right)}}
\] |
distribute-frac-neg [<=]65.8 | \[ 0.5 \cdot \sqrt{im \cdot \color{blue}{\frac{-im}{re}}}
\] |
if -1.01999999999999999e186 < re < -0.050000000000000003 or -1.59999999999999993e-73 < re Initial program 43.7%
Simplified85.9%
[Start]43.7 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\] |
|---|---|
+-commutative [=>]43.7 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}}
\] |
hypot-def [=>]85.9 | \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)}
\] |
if -0.050000000000000003 < re < -1.59999999999999993e-73Initial program 34.3%
Simplified58.5%
[Start]34.3 | \[ 0.5 \cdot \sqrt{2 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
\] |
|---|---|
+-commutative [=>]34.3 | \[ 0.5 \cdot \sqrt{2 \cdot \color{blue}{\left(re + \sqrt{re \cdot re + im \cdot im}\right)}}
\] |
hypot-def [=>]58.5 | \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \color{blue}{\mathsf{hypot}\left(re, im\right)}\right)}
\] |
Applied egg-rr58.1%
[Start]58.5 | \[ 0.5 \cdot \sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}
\] |
|---|---|
add-sqr-sqrt [=>]58.1 | \[ 0.5 \cdot \color{blue}{\left(\sqrt{\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}} \cdot \sqrt{\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}}\right)}
\] |
pow2 [=>]58.1 | \[ 0.5 \cdot \color{blue}{{\left(\sqrt{\sqrt{2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)}}\right)}^{2}}
\] |
pow1/2 [=>]58.1 | \[ 0.5 \cdot {\left(\sqrt{\color{blue}{{\left(2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)\right)}^{0.5}}}\right)}^{2}
\] |
sqrt-pow1 [=>]58.1 | \[ 0.5 \cdot {\color{blue}{\left({\left(2 \cdot \left(re + \mathsf{hypot}\left(re, im\right)\right)\right)}^{\left(\frac{0.5}{2}\right)}\right)}}^{2}
\] |
*-commutative [=>]58.1 | \[ 0.5 \cdot {\left({\color{blue}{\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right)}}^{\left(\frac{0.5}{2}\right)}\right)}^{2}
\] |
metadata-eval [=>]58.1 | \[ 0.5 \cdot {\left({\left(\left(re + \mathsf{hypot}\left(re, im\right)\right) \cdot 2\right)}^{\color{blue}{0.25}}\right)}^{2}
\] |
Taylor expanded in re around -inf 23.5%
Simplified18.1%
[Start]23.5 | \[ 0.5 \cdot {\left(e^{0.25 \cdot \left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}
\] |
|---|---|
exp-prod [=>]22.6 | \[ 0.5 \cdot {\color{blue}{\left({\left(e^{0.25}\right)}^{\left(\log \left({im}^{2}\right) + \log \left(\frac{-1}{re}\right)\right)}\right)}}^{2}
\] |
log-pow [=>]18.1 | \[ 0.5 \cdot {\left({\left(e^{0.25}\right)}^{\left(\color{blue}{2 \cdot \log im} + \log \left(\frac{-1}{re}\right)\right)}\right)}^{2}
\] |
Final simplification80.0%
| Alternative 1 | |
|---|---|
| Accuracy | 84.9% |
| Cost | 27401 |
| Alternative 2 | |
|---|---|
| Accuracy | 56.7% |
| Cost | 7640 |
| Alternative 3 | |
|---|---|
| Accuracy | 56.6% |
| Cost | 7377 |
| Alternative 4 | |
|---|---|
| Accuracy | 56.9% |
| Cost | 7377 |
| Alternative 5 | |
|---|---|
| Accuracy | 56.7% |
| Cost | 7249 |
| Alternative 6 | |
|---|---|
| Accuracy | 51.9% |
| Cost | 6852 |
| Alternative 7 | |
|---|---|
| Accuracy | 25.7% |
| Cost | 6720 |
herbie shell --seed 2023135
(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)))))