
(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 10 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 (<= (* x -2.0) -0.1) (expm1 (- (log 2.0) (log1p (exp (* x -2.0))))) (expm1 (* (fma -0.5 x 1.0) x))))
double code(double x, double y) {
double tmp;
if ((x * -2.0) <= -0.1) {
tmp = expm1((log(2.0) - log1p(exp((x * -2.0)))));
} else {
tmp = expm1((fma(-0.5, x, 1.0) * x));
}
return tmp;
}
function code(x, y) tmp = 0.0 if (Float64(x * -2.0) <= -0.1) tmp = expm1(Float64(log(2.0) - log1p(exp(Float64(x * -2.0))))); else tmp = expm1(Float64(fma(-0.5, x, 1.0) * x)); end return tmp end
code[x_, y_] := If[LessEqual[N[(x * -2.0), $MachinePrecision], -0.1], N[(Exp[N[(N[Log[2.0], $MachinePrecision] - N[Log[1 + N[Exp[N[(x * -2.0), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision], N[(Exp[N[(N[(-0.5 * x + 1.0), $MachinePrecision] * x), $MachinePrecision]] - 1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot -2 \leq -0.1:\\
\;\;\;\;\mathsf{expm1}\left(\log 2 - \mathsf{log1p}\left(e^{x \cdot -2}\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(\mathsf{fma}\left(-0.5, x, 1\right) \cdot x\right)\\
\end{array}
\end{array}
if (*.f64 #s(literal -2 binary64) x) < -0.10000000000000001Initial program 99.9%
lift--.f64N/A
lift-/.f64N/A
clear-numN/A
inv-powN/A
metadata-evalN/A
pow-to-expN/A
lower-expm1.f64N/A
lower-*.f64N/A
log-divN/A
lower--.f64N/A
lift-+.f64N/A
lower-log1p.f64N/A
lift-exp.f64N/A
lift-*.f64N/A
*-commutativeN/A
exp-prodN/A
lower-pow.f64N/A
lower-exp.f64N/A
lower-log.f64N/A
metadata-eval100.0
Applied rewrites100.0%
lift-pow.f64N/A
lift-exp.f64N/A
pow-expN/A
*-commutativeN/A
lower-exp.f64N/A
lower-*.f64100.0
Applied rewrites100.0%
if -0.10000000000000001 < (*.f64 #s(literal -2 binary64) x) Initial program 40.1%
lift--.f64N/A
lift-/.f64N/A
clear-numN/A
inv-powN/A
metadata-evalN/A
pow-to-expN/A
lower-expm1.f64N/A
lower-*.f64N/A
log-divN/A
lower--.f64N/A
lift-+.f64N/A
lower-log1p.f64N/A
lift-exp.f64N/A
lift-*.f64N/A
*-commutativeN/A
exp-prodN/A
lower-pow.f64N/A
lower-exp.f64N/A
lower-log.f64N/A
metadata-eval40.2
Applied rewrites40.2%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
lower-fma.f6499.5
Applied rewrites99.5%
Final simplification99.6%
(FPCore (x y) :precision binary64 (if (<= (* x -2.0) -0.1) (- (/ 2.0 (+ 1.0 (exp (* x -2.0)))) 1.0) (expm1 (* (fma -0.5 x 1.0) x))))
double code(double x, double y) {
double tmp;
if ((x * -2.0) <= -0.1) {
tmp = (2.0 / (1.0 + exp((x * -2.0)))) - 1.0;
} else {
tmp = expm1((fma(-0.5, x, 1.0) * x));
}
return tmp;
}
function code(x, y) tmp = 0.0 if (Float64(x * -2.0) <= -0.1) tmp = Float64(Float64(2.0 / Float64(1.0 + exp(Float64(x * -2.0)))) - 1.0); else tmp = expm1(Float64(fma(-0.5, x, 1.0) * x)); end return tmp end
code[x_, y_] := If[LessEqual[N[(x * -2.0), $MachinePrecision], -0.1], N[(N[(2.0 / N[(1.0 + N[Exp[N[(x * -2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision], N[(Exp[N[(N[(-0.5 * x + 1.0), $MachinePrecision] * x), $MachinePrecision]] - 1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot -2 \leq -0.1:\\
\;\;\;\;\frac{2}{1 + e^{x \cdot -2}} - 1\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(\mathsf{fma}\left(-0.5, x, 1\right) \cdot x\right)\\
\end{array}
\end{array}
if (*.f64 #s(literal -2 binary64) x) < -0.10000000000000001Initial program 99.9%
if -0.10000000000000001 < (*.f64 #s(literal -2 binary64) x) Initial program 40.1%
lift--.f64N/A
lift-/.f64N/A
clear-numN/A
inv-powN/A
metadata-evalN/A
pow-to-expN/A
lower-expm1.f64N/A
lower-*.f64N/A
log-divN/A
lower--.f64N/A
lift-+.f64N/A
lower-log1p.f64N/A
lift-exp.f64N/A
lift-*.f64N/A
*-commutativeN/A
exp-prodN/A
lower-pow.f64N/A
lower-exp.f64N/A
lower-log.f64N/A
metadata-eval40.2
Applied rewrites40.2%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
lower-fma.f6499.5
Applied rewrites99.5%
Final simplification99.6%
(FPCore (x y) :precision binary64 (expm1 (* (fma -0.5 x 1.0) x)))
double code(double x, double y) {
return expm1((fma(-0.5, x, 1.0) * x));
}
function code(x, y) return expm1(Float64(fma(-0.5, x, 1.0) * x)) end
code[x_, y_] := N[(Exp[N[(N[(-0.5 * x + 1.0), $MachinePrecision] * x), $MachinePrecision]] - 1), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{expm1}\left(\mathsf{fma}\left(-0.5, x, 1\right) \cdot x\right)
\end{array}
Initial program 55.8%
lift--.f64N/A
lift-/.f64N/A
clear-numN/A
inv-powN/A
metadata-evalN/A
pow-to-expN/A
lower-expm1.f64N/A
lower-*.f64N/A
log-divN/A
lower--.f64N/A
lift-+.f64N/A
lower-log1p.f64N/A
lift-exp.f64N/A
lift-*.f64N/A
*-commutativeN/A
exp-prodN/A
lower-pow.f64N/A
lower-exp.f64N/A
lower-log.f64N/A
metadata-eval55.8
Applied rewrites55.8%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
lower-fma.f6474.0
Applied rewrites74.0%
(FPCore (x y) :precision binary64 (expm1 (- (- x))))
double code(double x, double y) {
return expm1(-(-x));
}
public static double code(double x, double y) {
return Math.expm1(-(-x));
}
def code(x, y): return math.expm1(-(-x))
function code(x, y) return expm1(Float64(-Float64(-x))) end
code[x_, y_] := N[(Exp[(-(-x))] - 1), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{expm1}\left(-\left(-x\right)\right)
\end{array}
Initial program 55.8%
lift--.f64N/A
lift-/.f64N/A
clear-numN/A
inv-powN/A
metadata-evalN/A
pow-to-expN/A
lower-expm1.f64N/A
lower-*.f64N/A
log-divN/A
lower--.f64N/A
lift-+.f64N/A
lower-log1p.f64N/A
lift-exp.f64N/A
lift-*.f64N/A
*-commutativeN/A
exp-prodN/A
lower-pow.f64N/A
lower-exp.f64N/A
lower-log.f64N/A
metadata-eval55.8
Applied rewrites55.8%
Taylor expanded in x around 0
mul-1-negN/A
lower-neg.f6473.7
Applied rewrites73.7%
lift-*.f64N/A
*-commutativeN/A
mul-1-negN/A
lower-neg.f6473.7
Applied rewrites73.7%
(FPCore (x y) :precision binary64 (- (/ 2.0 (fma (fma (fma -1.3333333333333333 x 2.0) x -2.0) x 2.0)) 1.0))
double code(double x, double y) {
return (2.0 / fma(fma(fma(-1.3333333333333333, x, 2.0), x, -2.0), x, 2.0)) - 1.0;
}
function code(x, y) return Float64(Float64(2.0 / fma(fma(fma(-1.3333333333333333, x, 2.0), x, -2.0), x, 2.0)) - 1.0) end
code[x_, y_] := N[(N[(2.0 / N[(N[(N[(-1.3333333333333333 * x + 2.0), $MachinePrecision] * x + -2.0), $MachinePrecision] * x + 2.0), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{2}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-1.3333333333333333, x, 2\right), x, -2\right), x, 2\right)} - 1
\end{array}
Initial program 55.8%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f6429.7
Applied rewrites29.7%
(FPCore (x y) :precision binary64 (- (/ 2.0 (fma (fma 2.0 x -2.0) x 2.0)) 1.0))
double code(double x, double y) {
return (2.0 / fma(fma(2.0, x, -2.0), x, 2.0)) - 1.0;
}
function code(x, y) return Float64(Float64(2.0 / fma(fma(2.0, x, -2.0), x, 2.0)) - 1.0) end
code[x_, y_] := N[(N[(2.0 / N[(N[(2.0 * x + -2.0), $MachinePrecision] * x + 2.0), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{2}{\mathsf{fma}\left(\mathsf{fma}\left(2, x, -2\right), x, 2\right)} - 1
\end{array}
Initial program 55.8%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f6429.4
Applied rewrites29.4%
(FPCore (x y) :precision binary64 (if (<= x -1.9) (- (/ 2.0 (* x -2.0)) 1.0) (- (+ 1.0 x) 1.0)))
double code(double x, double y) {
double tmp;
if (x <= -1.9) {
tmp = (2.0 / (x * -2.0)) - 1.0;
} else {
tmp = (1.0 + x) - 1.0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-1.9d0)) then
tmp = (2.0d0 / (x * (-2.0d0))) - 1.0d0
else
tmp = (1.0d0 + x) - 1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -1.9) {
tmp = (2.0 / (x * -2.0)) - 1.0;
} else {
tmp = (1.0 + x) - 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1.9: tmp = (2.0 / (x * -2.0)) - 1.0 else: tmp = (1.0 + x) - 1.0 return tmp
function code(x, y) tmp = 0.0 if (x <= -1.9) tmp = Float64(Float64(2.0 / Float64(x * -2.0)) - 1.0); else tmp = Float64(Float64(1.0 + x) - 1.0); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -1.9) tmp = (2.0 / (x * -2.0)) - 1.0; else tmp = (1.0 + x) - 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1.9], N[(N[(2.0 / N[(x * -2.0), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision], N[(N[(1.0 + x), $MachinePrecision] - 1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.9:\\
\;\;\;\;\frac{2}{x \cdot -2} - 1\\
\mathbf{else}:\\
\;\;\;\;\left(1 + x\right) - 1\\
\end{array}
\end{array}
if x < -1.8999999999999999Initial program 100.0%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f6498.0
Applied rewrites98.0%
Taylor expanded in x around inf
Applied rewrites98.0%
if -1.8999999999999999 < x Initial program 40.7%
Taylor expanded in x around 0
lower-+.f647.4
Applied rewrites7.4%
Final simplification30.4%
(FPCore (x y) :precision binary64 (- (/ 2.0 (fma -2.0 x 2.0)) 1.0))
double code(double x, double y) {
return (2.0 / fma(-2.0, x, 2.0)) - 1.0;
}
function code(x, y) return Float64(Float64(2.0 / fma(-2.0, x, 2.0)) - 1.0) end
code[x_, y_] := N[(N[(2.0 / N[(-2.0 * x + 2.0), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{2}{\mathsf{fma}\left(-2, x, 2\right)} - 1
\end{array}
Initial program 55.8%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f6429.0
Applied rewrites29.0%
(FPCore (x y) :precision binary64 (- (+ 1.0 x) 1.0))
double code(double x, double y) {
return (1.0 + x) - 1.0;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (1.0d0 + x) - 1.0d0
end function
public static double code(double x, double y) {
return (1.0 + x) - 1.0;
}
def code(x, y): return (1.0 + x) - 1.0
function code(x, y) return Float64(Float64(1.0 + x) - 1.0) end
function tmp = code(x, y) tmp = (1.0 + x) - 1.0; end
code[x_, y_] := N[(N[(1.0 + x), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
\left(1 + x\right) - 1
\end{array}
Initial program 55.8%
Taylor expanded in x around 0
lower-+.f646.8
Applied rewrites6.8%
(FPCore (x y) :precision binary64 (- 1.0 1.0))
double code(double x, double y) {
return 1.0 - 1.0;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 1.0d0 - 1.0d0
end function
public static double code(double x, double y) {
return 1.0 - 1.0;
}
def code(x, y): return 1.0 - 1.0
function code(x, y) return Float64(1.0 - 1.0) end
function tmp = code(x, y) tmp = 1.0 - 1.0; end
code[x_, y_] := N[(1.0 - 1.0), $MachinePrecision]
\begin{array}{l}
\\
1 - 1
\end{array}
Initial program 55.8%
Taylor expanded in x around 0
Applied rewrites4.2%
herbie shell --seed 2024304
(FPCore (x y)
:name "Logistic function from Lakshay Garg"
:precision binary64
(- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))