
(FPCore (x) :precision binary64 (+ (- (exp x) 2.0) (exp (- x))))
double code(double x) {
return (exp(x) - 2.0) + exp(-x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (exp(x) - 2.0d0) + exp(-x)
end function
public static double code(double x) {
return (Math.exp(x) - 2.0) + Math.exp(-x);
}
def code(x): return (math.exp(x) - 2.0) + math.exp(-x)
function code(x) return Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) end
function tmp = code(x) tmp = (exp(x) - 2.0) + exp(-x); end
code[x_] := N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(e^{x} - 2\right) + e^{-x}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (+ (- (exp x) 2.0) (exp (- x))))
double code(double x) {
return (exp(x) - 2.0) + exp(-x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (exp(x) - 2.0d0) + exp(-x)
end function
public static double code(double x) {
return (Math.exp(x) - 2.0) + Math.exp(-x);
}
def code(x): return (math.exp(x) - 2.0) + math.exp(-x)
function code(x) return Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) end
function tmp = code(x) tmp = (exp(x) - 2.0) + exp(-x); end
code[x_] := N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(e^{x} - 2\right) + e^{-x}
\end{array}
(FPCore (x)
:precision binary64
(let* ((t_0 (exp (- x))))
(if (<= (+ (- (exp x) 2.0) t_0) 2e-11)
(+ (* x x) (* 0.08333333333333333 (pow x 4.0)))
(+ (exp x) (+ t_0 -2.0)))))
double code(double x) {
double t_0 = exp(-x);
double tmp;
if (((exp(x) - 2.0) + t_0) <= 2e-11) {
tmp = (x * x) + (0.08333333333333333 * pow(x, 4.0));
} else {
tmp = exp(x) + (t_0 + -2.0);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = exp(-x)
if (((exp(x) - 2.0d0) + t_0) <= 2d-11) then
tmp = (x * x) + (0.08333333333333333d0 * (x ** 4.0d0))
else
tmp = exp(x) + (t_0 + (-2.0d0))
end if
code = tmp
end function
public static double code(double x) {
double t_0 = Math.exp(-x);
double tmp;
if (((Math.exp(x) - 2.0) + t_0) <= 2e-11) {
tmp = (x * x) + (0.08333333333333333 * Math.pow(x, 4.0));
} else {
tmp = Math.exp(x) + (t_0 + -2.0);
}
return tmp;
}
def code(x): t_0 = math.exp(-x) tmp = 0 if ((math.exp(x) - 2.0) + t_0) <= 2e-11: tmp = (x * x) + (0.08333333333333333 * math.pow(x, 4.0)) else: tmp = math.exp(x) + (t_0 + -2.0) return tmp
function code(x) t_0 = exp(Float64(-x)) tmp = 0.0 if (Float64(Float64(exp(x) - 2.0) + t_0) <= 2e-11) tmp = Float64(Float64(x * x) + Float64(0.08333333333333333 * (x ^ 4.0))); else tmp = Float64(exp(x) + Float64(t_0 + -2.0)); end return tmp end
function tmp_2 = code(x) t_0 = exp(-x); tmp = 0.0; if (((exp(x) - 2.0) + t_0) <= 2e-11) tmp = (x * x) + (0.08333333333333333 * (x ^ 4.0)); else tmp = exp(x) + (t_0 + -2.0); end tmp_2 = tmp; end
code[x_] := Block[{t$95$0 = N[Exp[(-x)], $MachinePrecision]}, If[LessEqual[N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + t$95$0), $MachinePrecision], 2e-11], N[(N[(x * x), $MachinePrecision] + N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Exp[x], $MachinePrecision] + N[(t$95$0 + -2.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{-x}\\
\mathbf{if}\;\left(e^{x} - 2\right) + t_0 \leq 2 \cdot 10^{-11}:\\
\;\;\;\;x \cdot x + 0.08333333333333333 \cdot {x}^{4}\\
\mathbf{else}:\\
\;\;\;\;e^{x} + \left(t_0 + -2\right)\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 1.99999999999999988e-11Initial program 52.8%
associate-+l-53.0%
sub-neg53.0%
sub-neg53.0%
+-commutative53.0%
distribute-neg-in53.0%
remove-double-neg53.0%
metadata-eval53.0%
Simplified53.0%
Taylor expanded in x around 0 100.0%
unpow2100.0%
Simplified100.0%
if 1.99999999999999988e-11 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 99.9%
associate-+l-99.9%
sub-neg99.9%
sub-neg99.9%
+-commutative99.9%
distribute-neg-in99.9%
remove-double-neg99.9%
metadata-eval99.9%
Simplified99.9%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= x -5e+39) (sqrt (* 0.006944444444444444 (pow x 8.0))) (if (<= x 2.6) (+ (* x x) (* 0.08333333333333333 (pow x 4.0))) (expm1 x))))
double code(double x) {
double tmp;
if (x <= -5e+39) {
tmp = sqrt((0.006944444444444444 * pow(x, 8.0)));
} else if (x <= 2.6) {
tmp = (x * x) + (0.08333333333333333 * pow(x, 4.0));
} else {
tmp = expm1(x);
}
return tmp;
}
public static double code(double x) {
double tmp;
if (x <= -5e+39) {
tmp = Math.sqrt((0.006944444444444444 * Math.pow(x, 8.0)));
} else if (x <= 2.6) {
tmp = (x * x) + (0.08333333333333333 * Math.pow(x, 4.0));
} else {
tmp = Math.expm1(x);
}
return tmp;
}
def code(x): tmp = 0 if x <= -5e+39: tmp = math.sqrt((0.006944444444444444 * math.pow(x, 8.0))) elif x <= 2.6: tmp = (x * x) + (0.08333333333333333 * math.pow(x, 4.0)) else: tmp = math.expm1(x) return tmp
function code(x) tmp = 0.0 if (x <= -5e+39) tmp = sqrt(Float64(0.006944444444444444 * (x ^ 8.0))); elseif (x <= 2.6) tmp = Float64(Float64(x * x) + Float64(0.08333333333333333 * (x ^ 4.0))); else tmp = expm1(x); end return tmp end
code[x_] := If[LessEqual[x, -5e+39], N[Sqrt[N[(0.006944444444444444 * N[Power[x, 8.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[x, 2.6], N[(N[(x * x), $MachinePrecision] + N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{+39}:\\
\;\;\;\;\sqrt{0.006944444444444444 \cdot {x}^{8}}\\
\mathbf{elif}\;x \leq 2.6:\\
\;\;\;\;x \cdot x + 0.08333333333333333 \cdot {x}^{4}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\
\end{array}
\end{array}
if x < -5.00000000000000015e39Initial program 100.0%
associate-+l-100.0%
sub-neg100.0%
sub-neg100.0%
+-commutative100.0%
distribute-neg-in100.0%
remove-double-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 88.1%
unpow288.1%
Simplified88.1%
Taylor expanded in x around inf 88.1%
*-commutative88.1%
Simplified88.1%
add-sqr-sqrt88.1%
sqrt-unprod100.0%
pow1/2100.0%
*-commutative100.0%
*-commutative100.0%
swap-sqr100.0%
metadata-eval100.0%
pow-prod-up100.0%
metadata-eval100.0%
Applied egg-rr100.0%
unpow1/2100.0%
Simplified100.0%
if -5.00000000000000015e39 < x < 2.60000000000000009Initial program 55.9%
associate-+l-56.1%
sub-neg56.1%
sub-neg56.1%
+-commutative56.1%
distribute-neg-in56.1%
remove-double-neg56.1%
metadata-eval56.1%
Simplified56.1%
Taylor expanded in x around 0 94.0%
unpow294.0%
Simplified94.0%
if 2.60000000000000009 < x Initial program 100.0%
associate-+l-100.0%
sub-neg100.0%
sub-neg100.0%
+-commutative100.0%
distribute-neg-in100.0%
remove-double-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 98.8%
Taylor expanded in x around inf 98.8%
expm1-def98.8%
Simplified98.8%
Final simplification96.5%
(FPCore (x) :precision binary64 (if (<= x 2.6) (+ (* x x) (* 0.08333333333333333 (pow x 4.0))) (expm1 x)))
double code(double x) {
double tmp;
if (x <= 2.6) {
tmp = (x * x) + (0.08333333333333333 * pow(x, 4.0));
} else {
tmp = expm1(x);
}
return tmp;
}
public static double code(double x) {
double tmp;
if (x <= 2.6) {
tmp = (x * x) + (0.08333333333333333 * Math.pow(x, 4.0));
} else {
tmp = Math.expm1(x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 2.6: tmp = (x * x) + (0.08333333333333333 * math.pow(x, 4.0)) else: tmp = math.expm1(x) return tmp
function code(x) tmp = 0.0 if (x <= 2.6) tmp = Float64(Float64(x * x) + Float64(0.08333333333333333 * (x ^ 4.0))); else tmp = expm1(x); end return tmp end
code[x_] := If[LessEqual[x, 2.6], N[(N[(x * x), $MachinePrecision] + N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.6:\\
\;\;\;\;x \cdot x + 0.08333333333333333 \cdot {x}^{4}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\
\end{array}
\end{array}
if x < 2.60000000000000009Initial program 68.5%
associate-+l-68.7%
sub-neg68.7%
sub-neg68.7%
+-commutative68.7%
distribute-neg-in68.7%
remove-double-neg68.7%
metadata-eval68.7%
Simplified68.7%
Taylor expanded in x around 0 92.3%
unpow292.3%
Simplified92.3%
if 2.60000000000000009 < x Initial program 100.0%
associate-+l-100.0%
sub-neg100.0%
sub-neg100.0%
+-commutative100.0%
distribute-neg-in100.0%
remove-double-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 98.8%
Taylor expanded in x around inf 98.8%
expm1-def98.8%
Simplified98.8%
Final simplification93.9%
(FPCore (x) :precision binary64 (if (<= x -3.45) (* 0.08333333333333333 (pow x 4.0)) (if (<= x 1.65) (* x x) (expm1 x))))
double code(double x) {
double tmp;
if (x <= -3.45) {
tmp = 0.08333333333333333 * pow(x, 4.0);
} else if (x <= 1.65) {
tmp = x * x;
} else {
tmp = expm1(x);
}
return tmp;
}
public static double code(double x) {
double tmp;
if (x <= -3.45) {
tmp = 0.08333333333333333 * Math.pow(x, 4.0);
} else if (x <= 1.65) {
tmp = x * x;
} else {
tmp = Math.expm1(x);
}
return tmp;
}
def code(x): tmp = 0 if x <= -3.45: tmp = 0.08333333333333333 * math.pow(x, 4.0) elif x <= 1.65: tmp = x * x else: tmp = math.expm1(x) return tmp
function code(x) tmp = 0.0 if (x <= -3.45) tmp = Float64(0.08333333333333333 * (x ^ 4.0)); elseif (x <= 1.65) tmp = Float64(x * x); else tmp = expm1(x); end return tmp end
code[x_] := If[LessEqual[x, -3.45], N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 1.65], N[(x * x), $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.45:\\
\;\;\;\;0.08333333333333333 \cdot {x}^{4}\\
\mathbf{elif}\;x \leq 1.65:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\
\end{array}
\end{array}
if x < -3.4500000000000002Initial program 100.0%
associate-+l-100.0%
sub-neg100.0%
sub-neg100.0%
+-commutative100.0%
distribute-neg-in100.0%
remove-double-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 77.4%
unpow277.4%
Simplified77.4%
Taylor expanded in x around inf 77.4%
*-commutative77.4%
Simplified77.4%
if -3.4500000000000002 < x < 1.6499999999999999Initial program 53.2%
associate-+l-53.3%
sub-neg53.3%
sub-neg53.3%
+-commutative53.3%
distribute-neg-in53.3%
remove-double-neg53.3%
metadata-eval53.3%
Simplified53.3%
Taylor expanded in x around 0 99.2%
unpow299.2%
Simplified99.2%
if 1.6499999999999999 < x Initial program 100.0%
associate-+l-100.0%
sub-neg100.0%
sub-neg100.0%
+-commutative100.0%
distribute-neg-in100.0%
remove-double-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 98.8%
Taylor expanded in x around inf 98.8%
expm1-def98.8%
Simplified98.8%
Final simplification93.7%
(FPCore (x) :precision binary64 (if (<= x 1.65) (* x x) (expm1 x)))
double code(double x) {
double tmp;
if (x <= 1.65) {
tmp = x * x;
} else {
tmp = expm1(x);
}
return tmp;
}
public static double code(double x) {
double tmp;
if (x <= 1.65) {
tmp = x * x;
} else {
tmp = Math.expm1(x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.65: tmp = x * x else: tmp = math.expm1(x) return tmp
function code(x) tmp = 0.0 if (x <= 1.65) tmp = Float64(x * x); else tmp = expm1(x); end return tmp end
code[x_] := If[LessEqual[x, 1.65], N[(x * x), $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.65:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\
\end{array}
\end{array}
if x < 1.6499999999999999Initial program 68.5%
associate-+l-68.7%
sub-neg68.7%
sub-neg68.7%
+-commutative68.7%
distribute-neg-in68.7%
remove-double-neg68.7%
metadata-eval68.7%
Simplified68.7%
Taylor expanded in x around 0 86.1%
unpow286.1%
Simplified86.1%
if 1.6499999999999999 < x Initial program 100.0%
associate-+l-100.0%
sub-neg100.0%
sub-neg100.0%
+-commutative100.0%
distribute-neg-in100.0%
remove-double-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 98.8%
Taylor expanded in x around inf 98.8%
expm1-def98.8%
Simplified98.8%
Final simplification89.3%
(FPCore (x) :precision binary64 (* x x))
double code(double x) {
return x * x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = x * x
end function
public static double code(double x) {
return x * x;
}
def code(x): return x * x
function code(x) return Float64(x * x) end
function tmp = code(x) tmp = x * x; end
code[x_] := N[(x * x), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x
\end{array}
Initial program 76.4%
associate-+l-76.5%
sub-neg76.5%
sub-neg76.5%
+-commutative76.5%
distribute-neg-in76.5%
remove-double-neg76.5%
metadata-eval76.5%
Simplified76.5%
Taylor expanded in x around 0 77.1%
unpow277.1%
Simplified77.1%
Final simplification77.1%
(FPCore (x) :precision binary64 0.0)
double code(double x) {
return 0.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.0d0
end function
public static double code(double x) {
return 0.0;
}
def code(x): return 0.0
function code(x) return 0.0 end
function tmp = code(x) tmp = 0.0; end
code[x_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 76.4%
associate-+l-76.5%
sub-neg76.5%
sub-neg76.5%
+-commutative76.5%
distribute-neg-in76.5%
remove-double-neg76.5%
metadata-eval76.5%
Simplified76.5%
Applied egg-rr26.9%
Final simplification26.9%
(FPCore (x) :precision binary64 (* 4.0 (pow (sinh (/ x 2.0)) 2.0)))
double code(double x) {
return 4.0 * pow(sinh((x / 2.0)), 2.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 4.0d0 * (sinh((x / 2.0d0)) ** 2.0d0)
end function
public static double code(double x) {
return 4.0 * Math.pow(Math.sinh((x / 2.0)), 2.0);
}
def code(x): return 4.0 * math.pow(math.sinh((x / 2.0)), 2.0)
function code(x) return Float64(4.0 * (sinh(Float64(x / 2.0)) ^ 2.0)) end
function tmp = code(x) tmp = 4.0 * (sinh((x / 2.0)) ^ 2.0); end
code[x_] := N[(4.0 * N[Power[N[Sinh[N[(x / 2.0), $MachinePrecision]], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
4 \cdot {\sinh \left(\frac{x}{2}\right)}^{2}
\end{array}
herbie shell --seed 2023171
(FPCore (x)
:name "exp2 (problem 3.3.7)"
:precision binary64
:herbie-target
(* 4.0 (pow (sinh (/ x 2.0)) 2.0))
(+ (- (exp x) 2.0) (exp (- x))))