
(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 8 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.02) (expm1 (- (log 2.0) (log1p (exp (* -2.0 x))))) (expm1 (* x (+ 1.0 (* x -0.5))))))
double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -0.02) {
tmp = expm1((log(2.0) - log1p(exp((-2.0 * x)))));
} else {
tmp = expm1((x * (1.0 + (x * -0.5))));
}
return tmp;
}
public static double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -0.02) {
tmp = Math.expm1((Math.log(2.0) - Math.log1p(Math.exp((-2.0 * x)))));
} else {
tmp = Math.expm1((x * (1.0 + (x * -0.5))));
}
return tmp;
}
def code(x, y): tmp = 0 if (-2.0 * x) <= -0.02: tmp = math.expm1((math.log(2.0) - math.log1p(math.exp((-2.0 * x))))) else: tmp = math.expm1((x * (1.0 + (x * -0.5)))) return tmp
function code(x, y) tmp = 0.0 if (Float64(-2.0 * x) <= -0.02) tmp = expm1(Float64(log(2.0) - log1p(exp(Float64(-2.0 * x))))); else tmp = expm1(Float64(x * Float64(1.0 + Float64(x * -0.5)))); end return tmp end
code[x_, y_] := If[LessEqual[N[(-2.0 * x), $MachinePrecision], -0.02], N[(Exp[N[(N[Log[2.0], $MachinePrecision] - N[Log[1 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision], N[(Exp[N[(x * N[(1.0 + N[(x * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \leq -0.02:\\
\;\;\;\;\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(e^{-2 \cdot x}\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x \cdot \left(1 + x \cdot -0.5\right)\right)\\
\end{array}
\end{array}
if (*.f64 #s(literal -2 binary64) x) < -0.0200000000000000004Initial program 99.8%
add-exp-log99.8%
expm1-define99.8%
log-div99.9%
log1p-define99.9%
exp-prod99.9%
Applied egg-rr99.9%
Taylor expanded in x around inf 99.9%
if -0.0200000000000000004 < (*.f64 #s(literal -2 binary64) x) Initial program 45.0%
add-exp-log45.0%
expm1-define45.0%
log-div45.0%
log1p-define45.0%
exp-prod45.0%
Applied egg-rr45.0%
Taylor expanded in x around 0 99.4%
*-commutative99.4%
Simplified99.4%
(FPCore (x y) :precision binary64 (if (<= (* -2.0 x) -0.02) (+ (/ 2.0 (+ (exp (* -2.0 x)) 1.0)) -1.0) (expm1 (* x (+ 1.0 (* x -0.5))))))
double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -0.02) {
tmp = (2.0 / (exp((-2.0 * x)) + 1.0)) + -1.0;
} else {
tmp = expm1((x * (1.0 + (x * -0.5))));
}
return tmp;
}
public static double code(double x, double y) {
double tmp;
if ((-2.0 * x) <= -0.02) {
tmp = (2.0 / (Math.exp((-2.0 * x)) + 1.0)) + -1.0;
} else {
tmp = Math.expm1((x * (1.0 + (x * -0.5))));
}
return tmp;
}
def code(x, y): tmp = 0 if (-2.0 * x) <= -0.02: tmp = (2.0 / (math.exp((-2.0 * x)) + 1.0)) + -1.0 else: tmp = math.expm1((x * (1.0 + (x * -0.5)))) return tmp
function code(x, y) tmp = 0.0 if (Float64(-2.0 * x) <= -0.02) tmp = Float64(Float64(2.0 / Float64(exp(Float64(-2.0 * x)) + 1.0)) + -1.0); else tmp = expm1(Float64(x * Float64(1.0 + Float64(x * -0.5)))); end return tmp end
code[x_, y_] := If[LessEqual[N[(-2.0 * x), $MachinePrecision], -0.02], N[(N[(2.0 / N[(N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], N[(Exp[N[(x * N[(1.0 + N[(x * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \leq -0.02:\\
\;\;\;\;\frac{2}{e^{-2 \cdot x} + 1} + -1\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x \cdot \left(1 + x \cdot -0.5\right)\right)\\
\end{array}
\end{array}
if (*.f64 #s(literal -2 binary64) x) < -0.0200000000000000004Initial program 99.8%
if -0.0200000000000000004 < (*.f64 #s(literal -2 binary64) x) Initial program 45.0%
add-exp-log45.0%
expm1-define45.0%
log-div45.0%
log1p-define45.0%
exp-prod45.0%
Applied egg-rr45.0%
Taylor expanded in x around 0 99.4%
*-commutative99.4%
Simplified99.4%
Final simplification99.5%
(FPCore (x y) :precision binary64 (if (<= x 0.6) (expm1 (* x (+ 1.0 (* x -0.5)))) (* 2.0 (log1p (* x 0.5)))))
double code(double x, double y) {
double tmp;
if (x <= 0.6) {
tmp = expm1((x * (1.0 + (x * -0.5))));
} else {
tmp = 2.0 * log1p((x * 0.5));
}
return tmp;
}
public static double code(double x, double y) {
double tmp;
if (x <= 0.6) {
tmp = Math.expm1((x * (1.0 + (x * -0.5))));
} else {
tmp = 2.0 * Math.log1p((x * 0.5));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 0.6: tmp = math.expm1((x * (1.0 + (x * -0.5)))) else: tmp = 2.0 * math.log1p((x * 0.5)) return tmp
function code(x, y) tmp = 0.0 if (x <= 0.6) tmp = expm1(Float64(x * Float64(1.0 + Float64(x * -0.5)))); else tmp = Float64(2.0 * log1p(Float64(x * 0.5))); end return tmp end
code[x_, y_] := If[LessEqual[x, 0.6], N[(Exp[N[(x * N[(1.0 + N[(x * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision], N[(2.0 * N[Log[1 + N[(x * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.6:\\
\;\;\;\;\mathsf{expm1}\left(x \cdot \left(1 + x \cdot -0.5\right)\right)\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \mathsf{log1p}\left(x \cdot 0.5\right)\\
\end{array}
\end{array}
if x < 0.599999999999999978Initial program 45.5%
add-exp-log45.5%
expm1-define45.5%
log-div45.5%
log1p-define45.5%
exp-prod45.5%
Applied egg-rr45.5%
Taylor expanded in x around 0 98.9%
*-commutative98.9%
Simplified98.9%
if 0.599999999999999978 < x Initial program 100.0%
add-log-exp100.0%
add-sqr-sqrt100.0%
log-prod100.0%
sub-neg100.0%
exp-prod100.0%
metadata-eval100.0%
sub-neg100.0%
exp-prod100.0%
metadata-eval100.0%
Applied egg-rr100.0%
count-2100.0%
Simplified100.0%
Taylor expanded in x around 0 13.9%
*-commutative13.9%
Simplified13.9%
pow113.9%
log1p-define13.9%
Applied egg-rr13.9%
unpow113.9%
Simplified13.9%
(FPCore (x y)
:precision binary64
(if (<= x -2.1e-8)
(+
(/
2.0
(+ 1.0 (+ 1.0 (* x (- (* x (+ 2.0 (* x -1.3333333333333333))) 2.0)))))
-1.0)
(* 2.0 (log1p (* x 0.5)))))
double code(double x, double y) {
double tmp;
if (x <= -2.1e-8) {
tmp = (2.0 / (1.0 + (1.0 + (x * ((x * (2.0 + (x * -1.3333333333333333))) - 2.0))))) + -1.0;
} else {
tmp = 2.0 * log1p((x * 0.5));
}
return tmp;
}
public static double code(double x, double y) {
double tmp;
if (x <= -2.1e-8) {
tmp = (2.0 / (1.0 + (1.0 + (x * ((x * (2.0 + (x * -1.3333333333333333))) - 2.0))))) + -1.0;
} else {
tmp = 2.0 * Math.log1p((x * 0.5));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -2.1e-8: tmp = (2.0 / (1.0 + (1.0 + (x * ((x * (2.0 + (x * -1.3333333333333333))) - 2.0))))) + -1.0 else: tmp = 2.0 * math.log1p((x * 0.5)) return tmp
function code(x, y) tmp = 0.0 if (x <= -2.1e-8) tmp = Float64(Float64(2.0 / Float64(1.0 + Float64(1.0 + Float64(x * Float64(Float64(x * Float64(2.0 + Float64(x * -1.3333333333333333))) - 2.0))))) + -1.0); else tmp = Float64(2.0 * log1p(Float64(x * 0.5))); end return tmp end
code[x_, y_] := If[LessEqual[x, -2.1e-8], N[(N[(2.0 / N[(1.0 + N[(1.0 + N[(x * N[(N[(x * N[(2.0 + N[(x * -1.3333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], N[(2.0 * N[Log[1 + N[(x * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.1 \cdot 10^{-8}:\\
\;\;\;\;\frac{2}{1 + \left(1 + x \cdot \left(x \cdot \left(2 + x \cdot -1.3333333333333333\right) - 2\right)\right)} + -1\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \mathsf{log1p}\left(x \cdot 0.5\right)\\
\end{array}
\end{array}
if x < -2.09999999999999994e-8Initial program 99.6%
Taylor expanded in x around 0 98.4%
if -2.09999999999999994e-8 < x Initial program 38.8%
add-log-exp38.8%
add-sqr-sqrt38.8%
log-prod38.8%
sub-neg38.8%
exp-prod38.8%
metadata-eval38.8%
sub-neg38.8%
exp-prod38.8%
metadata-eval38.8%
Applied egg-rr38.8%
count-238.8%
Simplified38.8%
Taylor expanded in x around 0 9.0%
*-commutative9.0%
Simplified9.0%
pow19.0%
log1p-define69.9%
Applied egg-rr69.9%
unpow169.9%
Simplified69.9%
Final simplification78.9%
(FPCore (x y)
:precision binary64
(if (<= x -7e-6)
(+
(/
2.0
(+ 1.0 (+ 1.0 (* x (- (* x (+ 2.0 (* x -1.3333333333333333))) 2.0)))))
-1.0)
x))
double code(double x, double y) {
double tmp;
if (x <= -7e-6) {
tmp = (2.0 / (1.0 + (1.0 + (x * ((x * (2.0 + (x * -1.3333333333333333))) - 2.0))))) + -1.0;
} else {
tmp = x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-7d-6)) then
tmp = (2.0d0 / (1.0d0 + (1.0d0 + (x * ((x * (2.0d0 + (x * (-1.3333333333333333d0)))) - 2.0d0))))) + (-1.0d0)
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -7e-6) {
tmp = (2.0 / (1.0 + (1.0 + (x * ((x * (2.0 + (x * -1.3333333333333333))) - 2.0))))) + -1.0;
} else {
tmp = x;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -7e-6: tmp = (2.0 / (1.0 + (1.0 + (x * ((x * (2.0 + (x * -1.3333333333333333))) - 2.0))))) + -1.0 else: tmp = x return tmp
function code(x, y) tmp = 0.0 if (x <= -7e-6) tmp = Float64(Float64(2.0 / Float64(1.0 + Float64(1.0 + Float64(x * Float64(Float64(x * Float64(2.0 + Float64(x * -1.3333333333333333))) - 2.0))))) + -1.0); else tmp = x; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -7e-6) tmp = (2.0 / (1.0 + (1.0 + (x * ((x * (2.0 + (x * -1.3333333333333333))) - 2.0))))) + -1.0; else tmp = x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -7e-6], N[(N[(2.0 / N[(1.0 + N[(1.0 + N[(x * N[(N[(x * N[(2.0 + N[(x * -1.3333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], x]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -7 \cdot 10^{-6}:\\
\;\;\;\;\frac{2}{1 + \left(1 + x \cdot \left(x \cdot \left(2 + x \cdot -1.3333333333333333\right) - 2\right)\right)} + -1\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if x < -6.99999999999999989e-6Initial program 99.6%
Taylor expanded in x around 0 98.4%
if -6.99999999999999989e-6 < x Initial program 38.8%
Taylor expanded in x around 0 67.4%
Final simplification77.2%
(FPCore (x y)
:precision binary64
(if (<= x -6e-6)
(+
(/ 2.0 (+ 2.0 (* x (- (* x (+ 2.0 (* x -1.3333333333333333))) 2.0))))
-1.0)
x))
double code(double x, double y) {
double tmp;
if (x <= -6e-6) {
tmp = (2.0 / (2.0 + (x * ((x * (2.0 + (x * -1.3333333333333333))) - 2.0)))) + -1.0;
} else {
tmp = x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-6d-6)) then
tmp = (2.0d0 / (2.0d0 + (x * ((x * (2.0d0 + (x * (-1.3333333333333333d0)))) - 2.0d0)))) + (-1.0d0)
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -6e-6) {
tmp = (2.0 / (2.0 + (x * ((x * (2.0 + (x * -1.3333333333333333))) - 2.0)))) + -1.0;
} else {
tmp = x;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -6e-6: tmp = (2.0 / (2.0 + (x * ((x * (2.0 + (x * -1.3333333333333333))) - 2.0)))) + -1.0 else: tmp = x return tmp
function code(x, y) tmp = 0.0 if (x <= -6e-6) tmp = Float64(Float64(2.0 / Float64(2.0 + Float64(x * Float64(Float64(x * Float64(2.0 + Float64(x * -1.3333333333333333))) - 2.0)))) + -1.0); else tmp = x; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -6e-6) tmp = (2.0 / (2.0 + (x * ((x * (2.0 + (x * -1.3333333333333333))) - 2.0)))) + -1.0; else tmp = x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -6e-6], N[(N[(2.0 / N[(2.0 + N[(x * N[(N[(x * N[(2.0 + N[(x * -1.3333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], x]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -6 \cdot 10^{-6}:\\
\;\;\;\;\frac{2}{2 + x \cdot \left(x \cdot \left(2 + x \cdot -1.3333333333333333\right) - 2\right)} + -1\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if x < -6.0000000000000002e-6Initial program 99.6%
Taylor expanded in x around 0 98.4%
if -6.0000000000000002e-6 < x Initial program 38.8%
Taylor expanded in x around 0 67.4%
Final simplification77.2%
(FPCore (x y) :precision binary64 (if (<= x -1.0) -1.0 x))
double code(double x, double y) {
double tmp;
if (x <= -1.0) {
tmp = -1.0;
} else {
tmp = x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-1.0d0)) then
tmp = -1.0d0
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -1.0) {
tmp = -1.0;
} else {
tmp = x;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1.0: tmp = -1.0 else: tmp = x return tmp
function code(x, y) tmp = 0.0 if (x <= -1.0) tmp = -1.0; else tmp = x; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -1.0) tmp = -1.0; else tmp = x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1.0], -1.0, x]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if x < -1Initial program 100.0%
Taylor expanded in x around 0 98.6%
*-commutative98.6%
Simplified98.6%
Taylor expanded in x around inf 99.1%
if -1 < x Initial program 39.3%
Taylor expanded in x around 0 67.3%
(FPCore (x y) :precision binary64 -1.0)
double code(double x, double y) {
return -1.0;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = -1.0d0
end function
public static double code(double x, double y) {
return -1.0;
}
def code(x, y): return -1.0
function code(x, y) return -1.0 end
function tmp = code(x, y) tmp = -1.0; end
code[x_, y_] := -1.0
\begin{array}{l}
\\
-1
\end{array}
Initial program 58.0%
Taylor expanded in x around 0 34.0%
*-commutative34.0%
Simplified34.0%
Taylor expanded in x around inf 32.7%
herbie shell --seed 2024141
(FPCore (x y)
:name "Logistic function from Lakshay Garg"
:precision binary64
(- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))