
(FPCore (x eps) :precision binary64 (/ (- (* (+ 1.0 (/ 1.0 eps)) (exp (- (* (- 1.0 eps) x)))) (* (- (/ 1.0 eps) 1.0) (exp (- (* (+ 1.0 eps) x))))) 2.0))
double code(double x, double eps) {
return (((1.0 + (1.0 / eps)) * exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * exp(-((1.0 + eps) * x)))) / 2.0;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (((1.0d0 + (1.0d0 / eps)) * exp(-((1.0d0 - eps) * x))) - (((1.0d0 / eps) - 1.0d0) * exp(-((1.0d0 + eps) * x)))) / 2.0d0
end function
public static double code(double x, double eps) {
return (((1.0 + (1.0 / eps)) * Math.exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * Math.exp(-((1.0 + eps) * x)))) / 2.0;
}
def code(x, eps): return (((1.0 + (1.0 / eps)) * math.exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * math.exp(-((1.0 + eps) * x)))) / 2.0
function code(x, eps) return Float64(Float64(Float64(Float64(1.0 + Float64(1.0 / eps)) * exp(Float64(-Float64(Float64(1.0 - eps) * x)))) - Float64(Float64(Float64(1.0 / eps) - 1.0) * exp(Float64(-Float64(Float64(1.0 + eps) * x))))) / 2.0) end
function tmp = code(x, eps) tmp = (((1.0 + (1.0 / eps)) * exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * exp(-((1.0 + eps) * x)))) / 2.0; end
code[x_, eps_] := N[(N[(N[(N[(1.0 + N[(1.0 / eps), $MachinePrecision]), $MachinePrecision] * N[Exp[(-N[(N[(1.0 - eps), $MachinePrecision] * x), $MachinePrecision])], $MachinePrecision]), $MachinePrecision] - N[(N[(N[(1.0 / eps), $MachinePrecision] - 1.0), $MachinePrecision] * N[Exp[(-N[(N[(1.0 + eps), $MachinePrecision] * x), $MachinePrecision])], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-\left(1 + \varepsilon\right) \cdot x}}{2}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 14 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x eps) :precision binary64 (/ (- (* (+ 1.0 (/ 1.0 eps)) (exp (- (* (- 1.0 eps) x)))) (* (- (/ 1.0 eps) 1.0) (exp (- (* (+ 1.0 eps) x))))) 2.0))
double code(double x, double eps) {
return (((1.0 + (1.0 / eps)) * exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * exp(-((1.0 + eps) * x)))) / 2.0;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (((1.0d0 + (1.0d0 / eps)) * exp(-((1.0d0 - eps) * x))) - (((1.0d0 / eps) - 1.0d0) * exp(-((1.0d0 + eps) * x)))) / 2.0d0
end function
public static double code(double x, double eps) {
return (((1.0 + (1.0 / eps)) * Math.exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * Math.exp(-((1.0 + eps) * x)))) / 2.0;
}
def code(x, eps): return (((1.0 + (1.0 / eps)) * math.exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * math.exp(-((1.0 + eps) * x)))) / 2.0
function code(x, eps) return Float64(Float64(Float64(Float64(1.0 + Float64(1.0 / eps)) * exp(Float64(-Float64(Float64(1.0 - eps) * x)))) - Float64(Float64(Float64(1.0 / eps) - 1.0) * exp(Float64(-Float64(Float64(1.0 + eps) * x))))) / 2.0) end
function tmp = code(x, eps) tmp = (((1.0 + (1.0 / eps)) * exp(-((1.0 - eps) * x))) - (((1.0 / eps) - 1.0) * exp(-((1.0 + eps) * x)))) / 2.0; end
code[x_, eps_] := N[(N[(N[(N[(1.0 + N[(1.0 / eps), $MachinePrecision]), $MachinePrecision] * N[Exp[(-N[(N[(1.0 - eps), $MachinePrecision] * x), $MachinePrecision])], $MachinePrecision]), $MachinePrecision] - N[(N[(N[(1.0 / eps), $MachinePrecision] - 1.0), $MachinePrecision] * N[Exp[(-N[(N[(1.0 + eps), $MachinePrecision] * x), $MachinePrecision])], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(1 + \frac{1}{\varepsilon}\right) \cdot e^{-\left(1 - \varepsilon\right) \cdot x} - \left(\frac{1}{\varepsilon} - 1\right) \cdot e^{-\left(1 + \varepsilon\right) \cdot x}}{2}
\end{array}
(FPCore (x eps)
:precision binary64
(let* ((t_0 (exp (* (- eps 1.0) x))))
(if (<=
(-
(* (- 1.0 (/ -1.0 eps)) t_0)
(* (exp (* (- -1.0 eps) x)) (- (/ 1.0 eps) 1.0)))
2.000000000002)
(* (* (+ (+ 2.0 x) x) (exp (- x))) 0.5)
(* (+ (exp (* (- eps) x)) t_0) 0.5))))
double code(double x, double eps) {
double t_0 = exp(((eps - 1.0) * x));
double tmp;
if ((((1.0 - (-1.0 / eps)) * t_0) - (exp(((-1.0 - eps) * x)) * ((1.0 / eps) - 1.0))) <= 2.000000000002) {
tmp = (((2.0 + x) + x) * exp(-x)) * 0.5;
} else {
tmp = (exp((-eps * x)) + t_0) * 0.5;
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: t_0
real(8) :: tmp
t_0 = exp(((eps - 1.0d0) * x))
if ((((1.0d0 - ((-1.0d0) / eps)) * t_0) - (exp((((-1.0d0) - eps) * x)) * ((1.0d0 / eps) - 1.0d0))) <= 2.000000000002d0) then
tmp = (((2.0d0 + x) + x) * exp(-x)) * 0.5d0
else
tmp = (exp((-eps * x)) + t_0) * 0.5d0
end if
code = tmp
end function
public static double code(double x, double eps) {
double t_0 = Math.exp(((eps - 1.0) * x));
double tmp;
if ((((1.0 - (-1.0 / eps)) * t_0) - (Math.exp(((-1.0 - eps) * x)) * ((1.0 / eps) - 1.0))) <= 2.000000000002) {
tmp = (((2.0 + x) + x) * Math.exp(-x)) * 0.5;
} else {
tmp = (Math.exp((-eps * x)) + t_0) * 0.5;
}
return tmp;
}
def code(x, eps): t_0 = math.exp(((eps - 1.0) * x)) tmp = 0 if (((1.0 - (-1.0 / eps)) * t_0) - (math.exp(((-1.0 - eps) * x)) * ((1.0 / eps) - 1.0))) <= 2.000000000002: tmp = (((2.0 + x) + x) * math.exp(-x)) * 0.5 else: tmp = (math.exp((-eps * x)) + t_0) * 0.5 return tmp
function code(x, eps) t_0 = exp(Float64(Float64(eps - 1.0) * x)) tmp = 0.0 if (Float64(Float64(Float64(1.0 - Float64(-1.0 / eps)) * t_0) - Float64(exp(Float64(Float64(-1.0 - eps) * x)) * Float64(Float64(1.0 / eps) - 1.0))) <= 2.000000000002) tmp = Float64(Float64(Float64(Float64(2.0 + x) + x) * exp(Float64(-x))) * 0.5); else tmp = Float64(Float64(exp(Float64(Float64(-eps) * x)) + t_0) * 0.5); end return tmp end
function tmp_2 = code(x, eps) t_0 = exp(((eps - 1.0) * x)); tmp = 0.0; if ((((1.0 - (-1.0 / eps)) * t_0) - (exp(((-1.0 - eps) * x)) * ((1.0 / eps) - 1.0))) <= 2.000000000002) tmp = (((2.0 + x) + x) * exp(-x)) * 0.5; else tmp = (exp((-eps * x)) + t_0) * 0.5; end tmp_2 = tmp; end
code[x_, eps_] := Block[{t$95$0 = N[Exp[N[(N[(eps - 1.0), $MachinePrecision] * x), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[N[(N[(N[(1.0 - N[(-1.0 / eps), $MachinePrecision]), $MachinePrecision] * t$95$0), $MachinePrecision] - N[(N[Exp[N[(N[(-1.0 - eps), $MachinePrecision] * x), $MachinePrecision]], $MachinePrecision] * N[(N[(1.0 / eps), $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2.000000000002], N[(N[(N[(N[(2.0 + x), $MachinePrecision] + x), $MachinePrecision] * N[Exp[(-x)], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], N[(N[(N[Exp[N[((-eps) * x), $MachinePrecision]], $MachinePrecision] + t$95$0), $MachinePrecision] * 0.5), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{\left(\varepsilon - 1\right) \cdot x}\\
\mathbf{if}\;\left(1 - \frac{-1}{\varepsilon}\right) \cdot t\_0 - e^{\left(-1 - \varepsilon\right) \cdot x} \cdot \left(\frac{1}{\varepsilon} - 1\right) \leq 2.000000000002:\\
\;\;\;\;\left(\left(\left(2 + x\right) + x\right) \cdot e^{-x}\right) \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\left(e^{\left(-\varepsilon\right) \cdot x} + t\_0\right) \cdot 0.5\\
\end{array}
\end{array}
if (-.f64 (*.f64 (+.f64 #s(literal 1 binary64) (/.f64 #s(literal 1 binary64) eps)) (exp.f64 (neg.f64 (*.f64 (-.f64 #s(literal 1 binary64) eps) x)))) (*.f64 (-.f64 (/.f64 #s(literal 1 binary64) eps) #s(literal 1 binary64)) (exp.f64 (neg.f64 (*.f64 (+.f64 #s(literal 1 binary64) eps) x))))) < 2.0000000000020002Initial program 49.6%
Taylor expanded in eps around 0
*-commutativeN/A
lower-*.f64N/A
Applied rewrites99.8%
if 2.0000000000020002 < (-.f64 (*.f64 (+.f64 #s(literal 1 binary64) (/.f64 #s(literal 1 binary64) eps)) (exp.f64 (neg.f64 (*.f64 (-.f64 #s(literal 1 binary64) eps) x)))) (*.f64 (-.f64 (/.f64 #s(literal 1 binary64) eps) #s(literal 1 binary64)) (exp.f64 (neg.f64 (*.f64 (+.f64 #s(literal 1 binary64) eps) x))))) Initial program 99.9%
Taylor expanded in eps around inf
*-commutativeN/A
lower-*.f64N/A
Applied rewrites99.9%
Taylor expanded in eps around inf
Applied rewrites99.9%
Final simplification99.9%
(FPCore (x eps)
:precision binary64
(let* ((t_0 (- 1.0 (/ -1.0 eps)))
(t_1 (* (exp (* (- -1.0 eps) x)) (- (/ 1.0 eps) 1.0))))
(if (<= (- (* t_0 (exp (* (- eps 1.0) x))) t_1) 0.0)
(* (* (+ (+ 2.0 x) x) (exp (- x))) 0.5)
(/
(-
(* (fma (* (fma (* 0.5 x) (- eps 1.0) 1.0) (- eps 1.0)) x 1.0) t_0)
t_1)
2.0))))
double code(double x, double eps) {
double t_0 = 1.0 - (-1.0 / eps);
double t_1 = exp(((-1.0 - eps) * x)) * ((1.0 / eps) - 1.0);
double tmp;
if (((t_0 * exp(((eps - 1.0) * x))) - t_1) <= 0.0) {
tmp = (((2.0 + x) + x) * exp(-x)) * 0.5;
} else {
tmp = ((fma((fma((0.5 * x), (eps - 1.0), 1.0) * (eps - 1.0)), x, 1.0) * t_0) - t_1) / 2.0;
}
return tmp;
}
function code(x, eps) t_0 = Float64(1.0 - Float64(-1.0 / eps)) t_1 = Float64(exp(Float64(Float64(-1.0 - eps) * x)) * Float64(Float64(1.0 / eps) - 1.0)) tmp = 0.0 if (Float64(Float64(t_0 * exp(Float64(Float64(eps - 1.0) * x))) - t_1) <= 0.0) tmp = Float64(Float64(Float64(Float64(2.0 + x) + x) * exp(Float64(-x))) * 0.5); else tmp = Float64(Float64(Float64(fma(Float64(fma(Float64(0.5 * x), Float64(eps - 1.0), 1.0) * Float64(eps - 1.0)), x, 1.0) * t_0) - t_1) / 2.0); end return tmp end
code[x_, eps_] := Block[{t$95$0 = N[(1.0 - N[(-1.0 / eps), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Exp[N[(N[(-1.0 - eps), $MachinePrecision] * x), $MachinePrecision]], $MachinePrecision] * N[(N[(1.0 / eps), $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(N[(t$95$0 * N[Exp[N[(N[(eps - 1.0), $MachinePrecision] * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] - t$95$1), $MachinePrecision], 0.0], N[(N[(N[(N[(2.0 + x), $MachinePrecision] + x), $MachinePrecision] * N[Exp[(-x)], $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision], N[(N[(N[(N[(N[(N[(N[(0.5 * x), $MachinePrecision] * N[(eps - 1.0), $MachinePrecision] + 1.0), $MachinePrecision] * N[(eps - 1.0), $MachinePrecision]), $MachinePrecision] * x + 1.0), $MachinePrecision] * t$95$0), $MachinePrecision] - t$95$1), $MachinePrecision] / 2.0), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 1 - \frac{-1}{\varepsilon}\\
t_1 := e^{\left(-1 - \varepsilon\right) \cdot x} \cdot \left(\frac{1}{\varepsilon} - 1\right)\\
\mathbf{if}\;t\_0 \cdot e^{\left(\varepsilon - 1\right) \cdot x} - t\_1 \leq 0:\\
\;\;\;\;\left(\left(\left(2 + x\right) + x\right) \cdot e^{-x}\right) \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\mathsf{fma}\left(0.5 \cdot x, \varepsilon - 1, 1\right) \cdot \left(\varepsilon - 1\right), x, 1\right) \cdot t\_0 - t\_1}{2}\\
\end{array}
\end{array}
if (-.f64 (*.f64 (+.f64 #s(literal 1 binary64) (/.f64 #s(literal 1 binary64) eps)) (exp.f64 (neg.f64 (*.f64 (-.f64 #s(literal 1 binary64) eps) x)))) (*.f64 (-.f64 (/.f64 #s(literal 1 binary64) eps) #s(literal 1 binary64)) (exp.f64 (neg.f64 (*.f64 (+.f64 #s(literal 1 binary64) eps) x))))) < 0.0Initial program 36.9%
Taylor expanded in eps around 0
*-commutativeN/A
lower-*.f64N/A
Applied rewrites99.9%
if 0.0 < (-.f64 (*.f64 (+.f64 #s(literal 1 binary64) (/.f64 #s(literal 1 binary64) eps)) (exp.f64 (neg.f64 (*.f64 (-.f64 #s(literal 1 binary64) eps) x)))) (*.f64 (-.f64 (/.f64 #s(literal 1 binary64) eps) #s(literal 1 binary64)) (exp.f64 (neg.f64 (*.f64 (+.f64 #s(literal 1 binary64) eps) x))))) Initial program 99.8%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
associate--l+N/A
associate-*r*N/A
unpow2N/A
associate-*r*N/A
distribute-lft1-inN/A
lower-*.f64N/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower--.f64N/A
lower--.f6491.0
Applied rewrites91.0%
Final simplification94.7%
herbie shell --seed 2024230
(FPCore (x eps)
:name "NMSE Section 6.1 mentioned, A"
:precision binary64
(/ (- (* (+ 1.0 (/ 1.0 eps)) (exp (- (* (- 1.0 eps) x)))) (* (- (/ 1.0 eps) 1.0) (exp (- (* (+ 1.0 eps) x))))) 2.0))