
(FPCore (x y) :precision binary64 (- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))
double code(double x, double y) {
return (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (2.0d0 / (1.0d0 + exp(((-2.0d0) * x)))) - 1.0d0
end function
public static double code(double x, double y) {
return (2.0 / (1.0 + Math.exp((-2.0 * x)))) - 1.0;
}
def code(x, y): return (2.0 / (1.0 + math.exp((-2.0 * x)))) - 1.0
function code(x, y) return Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) - 1.0) end
function tmp = code(x, y) tmp = (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0; end
code[x_, y_] := N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{2}{1 + e^{-2 \cdot x}} - 1
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))
double code(double x, double y) {
return (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (2.0d0 / (1.0d0 + exp(((-2.0d0) * x)))) - 1.0d0
end function
public static double code(double x, double y) {
return (2.0 / (1.0 + Math.exp((-2.0 * x)))) - 1.0;
}
def code(x, y): return (2.0 / (1.0 + math.exp((-2.0 * x)))) - 1.0
function code(x, y) return Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) - 1.0) end
function tmp = code(x, y) tmp = (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0; end
code[x_, y_] := N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{2}{1 + e^{-2 \cdot x}} - 1
\end{array}
(FPCore (x y) :precision binary64 (if (<= (* -2.0 x) -0.5) (expm1 (- (log 2.0) (log (+ 1.0 (exp (* -2.0 x)))))) (expm1 x)))
double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -0.5) {
tmp = expm1((log(2.0) - log((1.0 + exp((-2.0 * x))))));
} else {
tmp = expm1(x);
}
return tmp;
}
public static double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -0.5) {
tmp = Math.expm1((Math.log(2.0) - Math.log((1.0 + Math.exp((-2.0 * x))))));
} else {
tmp = Math.expm1(x);
}
return tmp;
}
def code(x, y): tmp = 0 if (-2.0 * x) <= -0.5: tmp = math.expm1((math.log(2.0) - math.log((1.0 + math.exp((-2.0 * x)))))) else: tmp = math.expm1(x) return tmp
function code(x, y) tmp = 0.0 if (Float64(-2.0 * x) <= -0.5) tmp = expm1(Float64(log(2.0) - log(Float64(1.0 + exp(Float64(-2.0 * x)))))); else tmp = expm1(x); end return tmp end
code[x_, y_] := If[LessEqual[N[(-2.0 * x), $MachinePrecision], -0.5], N[(Exp[N[(N[Log[2.0], $MachinePrecision] - N[Log[N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \leq -0.5:\\
\;\;\;\;\mathsf{expm1}\left(\log 2 - \log \left(1 + e^{-2 \cdot x}\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\
\end{array}
\end{array}
if (*.f64 -2 x) < -0.5Initial program 100.0%
add-log-exp100.0%
*-un-lft-identity100.0%
log-prod100.0%
metadata-eval100.0%
add-log-exp100.0%
add-exp-log100.0%
expm1-def100.0%
log-div100.0%
log1p-udef100.0%
exp-prod100.0%
Applied egg-rr100.0%
+-lft-identity100.0%
Simplified100.0%
Taylor expanded in x around inf 100.0%
if -0.5 < (*.f64 -2 x) Initial program 37.6%
add-log-exp37.6%
*-un-lft-identity37.6%
log-prod37.6%
metadata-eval37.6%
add-log-exp37.6%
add-exp-log37.6%
expm1-def37.6%
log-div37.6%
log1p-udef37.7%
exp-prod37.7%
Applied egg-rr37.7%
+-lft-identity37.7%
Simplified37.7%
Taylor expanded in x around 0 99.7%
Final simplification99.8%
(FPCore (x y) :precision binary64 (if (<= (* -2.0 x) -0.5) (+ (cbrt (* 8.0 (/ 1.0 (pow (+ 1.0 (exp (* -2.0 x))) 3.0)))) -1.0) (expm1 x)))
double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -0.5) {
tmp = cbrt((8.0 * (1.0 / pow((1.0 + exp((-2.0 * x))), 3.0)))) + -1.0;
} else {
tmp = expm1(x);
}
return tmp;
}
public static double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -0.5) {
tmp = Math.cbrt((8.0 * (1.0 / Math.pow((1.0 + Math.exp((-2.0 * x))), 3.0)))) + -1.0;
} else {
tmp = Math.expm1(x);
}
return tmp;
}
function code(x, y) tmp = 0.0 if (Float64(-2.0 * x) <= -0.5) tmp = Float64(cbrt(Float64(8.0 * Float64(1.0 / (Float64(1.0 + exp(Float64(-2.0 * x))) ^ 3.0)))) + -1.0); else tmp = expm1(x); end return tmp end
code[x_, y_] := If[LessEqual[N[(-2.0 * x), $MachinePrecision], -0.5], N[(N[Power[N[(8.0 * N[(1.0 / N[Power[N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + -1.0), $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \leq -0.5:\\
\;\;\;\;\sqrt[3]{8 \cdot \frac{1}{{\left(1 + e^{-2 \cdot x}\right)}^{3}}} + -1\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\
\end{array}
\end{array}
if (*.f64 -2 x) < -0.5Initial program 100.0%
add-cbrt-cube100.0%
pow1/3100.0%
pow3100.0%
div-inv100.0%
unpow-prod-down100.0%
metadata-eval100.0%
inv-pow100.0%
metadata-eval100.0%
pow-pow100.0%
exp-prod100.0%
metadata-eval100.0%
metadata-eval100.0%
Applied egg-rr100.0%
unpow1/3100.0%
Simplified100.0%
Taylor expanded in x around inf 100.0%
if -0.5 < (*.f64 -2 x) Initial program 37.6%
add-log-exp37.6%
*-un-lft-identity37.6%
log-prod37.6%
metadata-eval37.6%
add-log-exp37.6%
add-exp-log37.6%
expm1-def37.6%
log-div37.6%
log1p-udef37.7%
exp-prod37.7%
Applied egg-rr37.7%
+-lft-identity37.7%
Simplified37.7%
Taylor expanded in x around 0 99.7%
Final simplification99.8%
(FPCore (x y) :precision binary64 (if (<= (* -2.0 x) -0.5) (+ (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) -1.0) (expm1 x)))
double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -0.5) {
tmp = (2.0 / (1.0 + exp((-2.0 * x)))) + -1.0;
} else {
tmp = expm1(x);
}
return tmp;
}
public static double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -0.5) {
tmp = (2.0 / (1.0 + Math.exp((-2.0 * x)))) + -1.0;
} else {
tmp = Math.expm1(x);
}
return tmp;
}
def code(x, y): tmp = 0 if (-2.0 * x) <= -0.5: tmp = (2.0 / (1.0 + math.exp((-2.0 * x)))) + -1.0 else: tmp = math.expm1(x) return tmp
function code(x, y) tmp = 0.0 if (Float64(-2.0 * x) <= -0.5) tmp = Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) + -1.0); else tmp = expm1(x); end return tmp end
code[x_, y_] := If[LessEqual[N[(-2.0 * x), $MachinePrecision], -0.5], N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \leq -0.5:\\
\;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} + -1\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\
\end{array}
\end{array}
if (*.f64 -2 x) < -0.5Initial program 100.0%
if -0.5 < (*.f64 -2 x) Initial program 37.6%
add-log-exp37.6%
*-un-lft-identity37.6%
log-prod37.6%
metadata-eval37.6%
add-log-exp37.6%
add-exp-log37.6%
expm1-def37.6%
log-div37.6%
log1p-udef37.7%
exp-prod37.7%
Applied egg-rr37.7%
+-lft-identity37.7%
Simplified37.7%
Taylor expanded in x around 0 99.7%
Final simplification99.8%
(FPCore (x y) :precision binary64 (if (<= x 2e-45) (expm1 x) (/ 1.0 (* (/ -0.5 x) (- (- x) 2.0)))))
double code(double x, double y) {
double tmp;
if (x <= 2e-45) {
tmp = expm1(x);
} else {
tmp = 1.0 / ((-0.5 / x) * (-x - 2.0));
}
return tmp;
}
public static double code(double x, double y) {
double tmp;
if (x <= 2e-45) {
tmp = Math.expm1(x);
} else {
tmp = 1.0 / ((-0.5 / x) * (-x - 2.0));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 2e-45: tmp = math.expm1(x) else: tmp = 1.0 / ((-0.5 / x) * (-x - 2.0)) return tmp
function code(x, y) tmp = 0.0 if (x <= 2e-45) tmp = expm1(x); else tmp = Float64(1.0 / Float64(Float64(-0.5 / x) * Float64(Float64(-x) - 2.0))); end return tmp end
code[x_, y_] := If[LessEqual[x, 2e-45], N[(Exp[x] - 1), $MachinePrecision], N[(1.0 / N[(N[(-0.5 / x), $MachinePrecision] * N[((-x) - 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2 \cdot 10^{-45}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{-0.5}{x} \cdot \left(\left(-x\right) - 2\right)}\\
\end{array}
\end{array}
if x < 1.99999999999999997e-45Initial program 38.6%
add-log-exp38.6%
*-un-lft-identity38.6%
log-prod38.6%
metadata-eval38.6%
add-log-exp38.6%
add-exp-log38.6%
expm1-def38.6%
log-div38.6%
log1p-udef38.7%
exp-prod38.7%
Applied egg-rr38.7%
+-lft-identity38.7%
Simplified38.7%
Taylor expanded in x around 0 99.7%
if 1.99999999999999997e-45 < x Initial program 93.8%
Taylor expanded in x around 0 5.1%
+-commutative5.1%
Simplified5.1%
flip--4.8%
clear-num4.8%
associate-+l+4.8%
metadata-eval4.8%
metadata-eval4.8%
difference-of-sqr-14.8%
associate-+l+4.8%
metadata-eval4.8%
associate--l+10.9%
metadata-eval10.9%
+-rgt-identity10.9%
Applied egg-rr10.9%
clear-num10.9%
frac-2neg10.9%
associate-/r/10.9%
*-commutative10.9%
distribute-rgt-neg-in10.9%
Applied egg-rr10.9%
Taylor expanded in x around 0 23.9%
Final simplification76.3%
(FPCore (x y) :precision binary64 (/ 1.0 (* (/ -0.5 x) (- (- x) 2.0))))
double code(double x, double y) {
return 1.0 / ((-0.5 / x) * (-x - 2.0));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 1.0d0 / (((-0.5d0) / x) * (-x - 2.0d0))
end function
public static double code(double x, double y) {
return 1.0 / ((-0.5 / x) * (-x - 2.0));
}
def code(x, y): return 1.0 / ((-0.5 / x) * (-x - 2.0))
function code(x, y) return Float64(1.0 / Float64(Float64(-0.5 / x) * Float64(Float64(-x) - 2.0))) end
function tmp = code(x, y) tmp = 1.0 / ((-0.5 / x) * (-x - 2.0)); end
code[x_, y_] := N[(1.0 / N[(N[(-0.5 / x), $MachinePrecision] * N[((-x) - 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\frac{-0.5}{x} \cdot \left(\left(-x\right) - 2\right)}
\end{array}
Initial program 55.6%
Taylor expanded in x around 0 5.7%
+-commutative5.7%
Simplified5.7%
flip--5.5%
clear-num5.5%
associate-+l+5.5%
metadata-eval5.5%
metadata-eval5.5%
difference-of-sqr-15.5%
associate-+l+5.5%
metadata-eval5.5%
associate--l+49.6%
metadata-eval49.6%
+-rgt-identity49.6%
Applied egg-rr49.6%
clear-num49.6%
frac-2neg49.6%
associate-/r/49.6%
*-commutative49.6%
distribute-rgt-neg-in49.6%
Applied egg-rr49.6%
Taylor expanded in x around 0 52.7%
Final simplification52.7%
(FPCore (x y) :precision binary64 x)
double code(double x, double y) {
return x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x
end function
public static double code(double x, double y) {
return x;
}
def code(x, y): return x
function code(x, y) return x end
function tmp = code(x, y) tmp = x; end
code[x_, y_] := x
\begin{array}{l}
\\
x
\end{array}
Initial program 55.6%
Taylor expanded in x around 0 50.0%
Final simplification50.0%
herbie shell --seed 2023318
(FPCore (x y)
:name "Logistic function from Lakshay Garg"
:precision binary64
(- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))