| Alternative 1 | |
|---|---|
| Accuracy | 99.6% |
| Cost | 26372 |

(FPCore (x y) :precision binary64 (- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))
(FPCore (x y)
:precision binary64
(let* ((t_0 (pow (exp -2.0) x)))
(if (<= (* -2.0 x) -0.1)
(log (exp (+ (/ 2.0 (+ t_0 1.0)) -1.0)))
(if (<= (* -2.0 x) 2e-12)
(+ x (* -0.3333333333333333 (pow x 3.0)))
(expm1 (- (log 2.0) (log1p t_0)))))))double code(double x, double y) {
return (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0;
}
double code(double x, double y) {
double t_0 = pow(exp(-2.0), x);
double tmp;
if ((-2.0 * x) <= -0.1) {
tmp = log(exp(((2.0 / (t_0 + 1.0)) + -1.0)));
} else if ((-2.0 * x) <= 2e-12) {
tmp = x + (-0.3333333333333333 * pow(x, 3.0));
} else {
tmp = expm1((log(2.0) - log1p(t_0)));
}
return tmp;
}
public static double code(double x, double y) {
return (2.0 / (1.0 + Math.exp((-2.0 * x)))) - 1.0;
}
public static double code(double x, double y) {
double t_0 = Math.pow(Math.exp(-2.0), x);
double tmp;
if ((-2.0 * x) <= -0.1) {
tmp = Math.log(Math.exp(((2.0 / (t_0 + 1.0)) + -1.0)));
} else if ((-2.0 * x) <= 2e-12) {
tmp = x + (-0.3333333333333333 * Math.pow(x, 3.0));
} else {
tmp = Math.expm1((Math.log(2.0) - Math.log1p(t_0)));
}
return tmp;
}
def code(x, y): return (2.0 / (1.0 + math.exp((-2.0 * x)))) - 1.0
def code(x, y): t_0 = math.pow(math.exp(-2.0), x) tmp = 0 if (-2.0 * x) <= -0.1: tmp = math.log(math.exp(((2.0 / (t_0 + 1.0)) + -1.0))) elif (-2.0 * x) <= 2e-12: tmp = x + (-0.3333333333333333 * math.pow(x, 3.0)) else: tmp = math.expm1((math.log(2.0) - math.log1p(t_0))) return tmp
function code(x, y) return Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) - 1.0) end
function code(x, y) t_0 = exp(-2.0) ^ x tmp = 0.0 if (Float64(-2.0 * x) <= -0.1) tmp = log(exp(Float64(Float64(2.0 / Float64(t_0 + 1.0)) + -1.0))); elseif (Float64(-2.0 * x) <= 2e-12) tmp = Float64(x + Float64(-0.3333333333333333 * (x ^ 3.0))); else tmp = expm1(Float64(log(2.0) - log1p(t_0))); end return tmp end
code[x_, y_] := N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
code[x_, y_] := Block[{t$95$0 = N[Power[N[Exp[-2.0], $MachinePrecision], x], $MachinePrecision]}, If[LessEqual[N[(-2.0 * x), $MachinePrecision], -0.1], N[Log[N[Exp[N[(N[(2.0 / N[(t$95$0 + 1.0), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]], $MachinePrecision]], $MachinePrecision], If[LessEqual[N[(-2.0 * x), $MachinePrecision], 2e-12], N[(x + N[(-0.3333333333333333 * N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(Exp[N[(N[Log[2.0], $MachinePrecision] - N[Log[1 + t$95$0], $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]]]]
\frac{2}{1 + e^{-2 \cdot x}} - 1
\begin{array}{l}
t_0 := {\left(e^{-2}\right)}^{x}\\
\mathbf{if}\;-2 \cdot x \leq -0.1:\\
\;\;\;\;\log \left(e^{\frac{2}{t_0 + 1} + -1}\right)\\
\mathbf{elif}\;-2 \cdot x \leq 2 \cdot 10^{-12}:\\
\;\;\;\;x + -0.3333333333333333 \cdot {x}^{3}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(t_0\right)\right)\\
\end{array}
Herbie found 10 alternatives:
| Alternative | Accuracy | Speedup |
|---|
Results
if (*.f64 -2 x) < -0.10000000000000001Initial program 99.9%
Applied egg-rr99.9%
[Start]99.9% | \[ \frac{2}{1 + e^{-2 \cdot x}} - 1
\] |
|---|---|
add-log-exp [=>]99.9% | \[ \color{blue}{\log \left(e^{\frac{2}{1 + e^{-2 \cdot x}} - 1}\right)}
\] |
*-un-lft-identity [=>]99.9% | \[ \log \color{blue}{\left(1 \cdot e^{\frac{2}{1 + e^{-2 \cdot x}} - 1}\right)}
\] |
log-prod [=>]99.9% | \[ \color{blue}{\log 1 + \log \left(e^{\frac{2}{1 + e^{-2 \cdot x}} - 1}\right)}
\] |
metadata-eval [=>]99.9% | \[ \color{blue}{0} + \log \left(e^{\frac{2}{1 + e^{-2 \cdot x}} - 1}\right)
\] |
add-log-exp [<=]99.9% | \[ 0 + \color{blue}{\left(\frac{2}{1 + e^{-2 \cdot x}} - 1\right)}
\] |
add-exp-log [=>]99.9% | \[ 0 + \left(\color{blue}{e^{\log \left(\frac{2}{1 + e^{-2 \cdot x}}\right)}} - 1\right)
\] |
expm1-def [=>]99.9% | \[ 0 + \color{blue}{\mathsf{expm1}\left(\log \left(\frac{2}{1 + e^{-2 \cdot x}}\right)\right)}
\] |
log-div [=>]99.9% | \[ 0 + \mathsf{expm1}\left(\color{blue}{\log 2 - \log \left(1 + e^{-2 \cdot x}\right)}\right)
\] |
log1p-udef [<=]99.9% | \[ 0 + \mathsf{expm1}\left(\log 2 - \color{blue}{\mathsf{log1p}\left(e^{-2 \cdot x}\right)}\right)
\] |
exp-prod [=>]99.9% | \[ 0 + \mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(\color{blue}{{\left(e^{-2}\right)}^{x}}\right)\right)
\] |
Simplified99.9%
[Start]99.9% | \[ 0 + \mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)\right)
\] |
|---|---|
+-lft-identity [=>]99.9% | \[ \color{blue}{\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)\right)}
\] |
Applied egg-rr99.9%
[Start]99.9% | \[ \mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)\right)
\] |
|---|---|
add-log-exp [=>]99.9% | \[ \color{blue}{\log \left(e^{\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)\right)}\right)}
\] |
expm1-udef [=>]99.9% | \[ \log \left(e^{\color{blue}{e^{\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)} - 1}}\right)
\] |
sub-neg [=>]99.9% | \[ \log \left(e^{\color{blue}{e^{\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)} + \left(-1\right)}}\right)
\] |
exp-diff [=>]99.9% | \[ \log \left(e^{\color{blue}{\frac{e^{\log 2}}{e^{\mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)}}} + \left(-1\right)}\right)
\] |
add-exp-log [<=]99.9% | \[ \log \left(e^{\frac{\color{blue}{2}}{e^{\mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)}} + \left(-1\right)}\right)
\] |
log1p-udef [=>]99.9% | \[ \log \left(e^{\frac{2}{e^{\color{blue}{\log \left(1 + {\left(e^{-2}\right)}^{x}\right)}}} + \left(-1\right)}\right)
\] |
add-exp-log [<=]99.9% | \[ \log \left(e^{\frac{2}{\color{blue}{1 + {\left(e^{-2}\right)}^{x}}} + \left(-1\right)}\right)
\] |
+-commutative [=>]99.9% | \[ \log \left(e^{\frac{2}{\color{blue}{{\left(e^{-2}\right)}^{x} + 1}} + \left(-1\right)}\right)
\] |
metadata-eval [=>]99.9% | \[ \log \left(e^{\frac{2}{{\left(e^{-2}\right)}^{x} + 1} + \color{blue}{-1}}\right)
\] |
if -0.10000000000000001 < (*.f64 -2 x) < 1.99999999999999996e-12Initial program 8.0%
Taylor expanded in x around 0 100.0%
if 1.99999999999999996e-12 < (*.f64 -2 x) Initial program 99.9%
Applied egg-rr100.0%
[Start]99.9% | \[ \frac{2}{1 + e^{-2 \cdot x}} - 1
\] |
|---|---|
add-log-exp [=>]99.9% | \[ \color{blue}{\log \left(e^{\frac{2}{1 + e^{-2 \cdot x}} - 1}\right)}
\] |
*-un-lft-identity [=>]99.9% | \[ \log \color{blue}{\left(1 \cdot e^{\frac{2}{1 + e^{-2 \cdot x}} - 1}\right)}
\] |
log-prod [=>]99.9% | \[ \color{blue}{\log 1 + \log \left(e^{\frac{2}{1 + e^{-2 \cdot x}} - 1}\right)}
\] |
metadata-eval [=>]99.9% | \[ \color{blue}{0} + \log \left(e^{\frac{2}{1 + e^{-2 \cdot x}} - 1}\right)
\] |
add-log-exp [<=]99.9% | \[ 0 + \color{blue}{\left(\frac{2}{1 + e^{-2 \cdot x}} - 1\right)}
\] |
add-exp-log [=>]99.9% | \[ 0 + \left(\color{blue}{e^{\log \left(\frac{2}{1 + e^{-2 \cdot x}}\right)}} - 1\right)
\] |
expm1-def [=>]99.9% | \[ 0 + \color{blue}{\mathsf{expm1}\left(\log \left(\frac{2}{1 + e^{-2 \cdot x}}\right)\right)}
\] |
log-div [=>]99.9% | \[ 0 + \mathsf{expm1}\left(\color{blue}{\log 2 - \log \left(1 + e^{-2 \cdot x}\right)}\right)
\] |
log1p-udef [<=]100.0% | \[ 0 + \mathsf{expm1}\left(\log 2 - \color{blue}{\mathsf{log1p}\left(e^{-2 \cdot x}\right)}\right)
\] |
exp-prod [=>]100.0% | \[ 0 + \mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(\color{blue}{{\left(e^{-2}\right)}^{x}}\right)\right)
\] |
Simplified100.0%
[Start]100.0% | \[ 0 + \mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)\right)
\] |
|---|---|
+-lft-identity [=>]100.0% | \[ \color{blue}{\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left({\left(e^{-2}\right)}^{x}\right)\right)}
\] |
Final simplification100.0%
| Alternative 1 | |
|---|---|
| Accuracy | 99.6% |
| Cost | 26372 |
| Alternative 2 | |
|---|---|
| Accuracy | 99.6% |
| Cost | 26308 |
| Alternative 3 | |
|---|---|
| Accuracy | 99.6% |
| Cost | 13832 |
| Alternative 4 | |
|---|---|
| Accuracy | 99.6% |
| Cost | 7497 |
| Alternative 5 | |
|---|---|
| Accuracy | 78.6% |
| Cost | 708 |
| Alternative 6 | |
|---|---|
| Accuracy | 79.1% |
| Cost | 584 |
| Alternative 7 | |
|---|---|
| Accuracy | 79.1% |
| Cost | 328 |
| Alternative 8 | |
|---|---|
| Accuracy | 32.4% |
| Cost | 196 |
| Alternative 9 | |
|---|---|
| Accuracy | 27.3% |
| Cost | 64 |
herbie shell --seed 2023164
(FPCore (x y)
:name "Logistic function from Lakshay Garg"
:precision binary64
(- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))