
(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 8 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) 5e-7)
(fma
0.002777777777777778
(pow x 6.0)
(+ (* 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) <= 5e-7) {
tmp = fma(0.002777777777777778, pow(x, 6.0), ((x * x) + (0.08333333333333333 * pow(x, 4.0))));
} else {
tmp = 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) <= 5e-7) tmp = fma(0.002777777777777778, (x ^ 6.0), Float64(Float64(x * x) + Float64(0.08333333333333333 * (x ^ 4.0)))); else tmp = Float64(exp(x) + Float64(t_0 + -2.0)); end return 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], 5e-7], N[(0.002777777777777778 * N[Power[x, 6.0], $MachinePrecision] + N[(N[(x * x), $MachinePrecision] + N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $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 5 \cdot 10^{-7}:\\
\;\;\;\;\mathsf{fma}\left(0.002777777777777778, {x}^{6}, x \cdot x + 0.08333333333333333 \cdot {x}^{4}\right)\\
\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))) < 4.99999999999999977e-7Initial program 51.6%
associate-+l-51.7%
sub-neg51.7%
sub-neg51.7%
+-commutative51.7%
distribute-neg-in51.7%
remove-double-neg51.7%
metadata-eval51.7%
Simplified51.7%
Taylor expanded in x around 0 100.0%
fma-def100.0%
unpow2100.0%
Simplified100.0%
if 4.99999999999999977e-7 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 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%
Final simplification100.0%
(FPCore (x)
:precision binary64
(let* ((t_0 (exp (- x))))
(if (<= (+ (- (exp x) 2.0) t_0) 5e-7)
(+ (* 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) <= 5e-7) {
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) <= 5d-7) 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) <= 5e-7) {
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) <= 5e-7: 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) <= 5e-7) 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) <= 5e-7) 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], 5e-7], 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 5 \cdot 10^{-7}:\\
\;\;\;\;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))) < 4.99999999999999977e-7Initial program 51.6%
associate-+l-51.7%
sub-neg51.7%
sub-neg51.7%
+-commutative51.7%
distribute-neg-in51.7%
remove-double-neg51.7%
metadata-eval51.7%
Simplified51.7%
Taylor expanded in x around 0 100.0%
unpow2100.0%
Simplified100.0%
if 4.99999999999999977e-7 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 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%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= x -6.2) (sqrt (* (pow x 12.0) 7.71604938271605e-6)) (if (<= x 2.6) (+ (* x x) (* 0.08333333333333333 (pow x 4.0))) (expm1 x))))
double code(double x) {
double tmp;
if (x <= -6.2) {
tmp = sqrt((pow(x, 12.0) * 7.71604938271605e-6));
} 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 <= -6.2) {
tmp = Math.sqrt((Math.pow(x, 12.0) * 7.71604938271605e-6));
} 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 <= -6.2: tmp = math.sqrt((math.pow(x, 12.0) * 7.71604938271605e-6)) 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 <= -6.2) tmp = sqrt(Float64((x ^ 12.0) * 7.71604938271605e-6)); 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, -6.2], N[Sqrt[N[(N[Power[x, 12.0], $MachinePrecision] * 7.71604938271605e-6), $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 -6.2:\\
\;\;\;\;\sqrt{{x}^{12} \cdot 7.71604938271605 \cdot 10^{-6}}\\
\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 < -6.20000000000000018Initial 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 85.6%
fma-def85.6%
unpow285.6%
Simplified85.6%
Taylor expanded in x around inf 85.6%
add-sqr-sqrt85.6%
sqrt-unprod93.3%
*-commutative93.3%
*-commutative93.3%
swap-sqr93.3%
pow-prod-up93.3%
metadata-eval93.3%
metadata-eval93.3%
Applied egg-rr93.3%
if -6.20000000000000018 < x < 2.60000000000000009Initial program 51.6%
associate-+l-51.7%
sub-neg51.7%
sub-neg51.7%
+-commutative51.7%
distribute-neg-in51.7%
remove-double-neg51.7%
metadata-eval51.7%
Simplified51.7%
Taylor expanded in x around 0 100.0%
unpow2100.0%
Simplified100.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 99.1%
Taylor expanded in x around inf 99.1%
expm1-def99.1%
Simplified99.1%
Final simplification97.9%
(FPCore (x) :precision binary64 (if (<= x -6.2) (* 0.002777777777777778 (pow x 6.0)) (if (<= x 2.6) (+ (* x x) (* 0.08333333333333333 (pow x 4.0))) (expm1 x))))
double code(double x) {
double tmp;
if (x <= -6.2) {
tmp = 0.002777777777777778 * pow(x, 6.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 <= -6.2) {
tmp = 0.002777777777777778 * Math.pow(x, 6.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 <= -6.2: tmp = 0.002777777777777778 * math.pow(x, 6.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 <= -6.2) tmp = Float64(0.002777777777777778 * (x ^ 6.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, -6.2], N[(0.002777777777777778 * N[Power[x, 6.0], $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 -6.2:\\
\;\;\;\;0.002777777777777778 \cdot {x}^{6}\\
\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 < -6.20000000000000018Initial 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 85.6%
fma-def85.6%
unpow285.6%
Simplified85.6%
Taylor expanded in x around inf 85.6%
if -6.20000000000000018 < x < 2.60000000000000009Initial program 51.6%
associate-+l-51.7%
sub-neg51.7%
sub-neg51.7%
+-commutative51.7%
distribute-neg-in51.7%
remove-double-neg51.7%
metadata-eval51.7%
Simplified51.7%
Taylor expanded in x around 0 100.0%
unpow2100.0%
Simplified100.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 99.1%
Taylor expanded in x around inf 99.1%
expm1-def99.1%
Simplified99.1%
Final simplification95.7%
(FPCore (x) :precision binary64 (if (<= x -4.4) (* 0.002777777777777778 (pow x 6.0)) (if (<= x 1.65) (* x x) (expm1 x))))
double code(double x) {
double tmp;
if (x <= -4.4) {
tmp = 0.002777777777777778 * pow(x, 6.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 <= -4.4) {
tmp = 0.002777777777777778 * Math.pow(x, 6.0);
} else if (x <= 1.65) {
tmp = x * x;
} else {
tmp = Math.expm1(x);
}
return tmp;
}
def code(x): tmp = 0 if x <= -4.4: tmp = 0.002777777777777778 * math.pow(x, 6.0) elif x <= 1.65: tmp = x * x else: tmp = math.expm1(x) return tmp
function code(x) tmp = 0.0 if (x <= -4.4) tmp = Float64(0.002777777777777778 * (x ^ 6.0)); elseif (x <= 1.65) tmp = Float64(x * x); else tmp = expm1(x); end return tmp end
code[x_] := If[LessEqual[x, -4.4], N[(0.002777777777777778 * N[Power[x, 6.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 -4.4:\\
\;\;\;\;0.002777777777777778 \cdot {x}^{6}\\
\mathbf{elif}\;x \leq 1.65:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\
\end{array}
\end{array}
if x < -4.4000000000000004Initial 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 85.6%
fma-def85.6%
unpow285.6%
Simplified85.6%
Taylor expanded in x around inf 85.6%
if -4.4000000000000004 < x < 1.6499999999999999Initial program 51.6%
associate-+l-51.7%
sub-neg51.7%
sub-neg51.7%
+-commutative51.7%
distribute-neg-in51.7%
remove-double-neg51.7%
metadata-eval51.7%
Simplified51.7%
Taylor expanded in x around 0 99.5%
unpow299.5%
Simplified99.5%
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 99.1%
Taylor expanded in x around inf 99.1%
expm1-def99.1%
Simplified99.1%
Final simplification95.5%
(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 69.9%
associate-+l-69.9%
sub-neg69.9%
sub-neg69.9%
+-commutative69.9%
distribute-neg-in69.9%
remove-double-neg69.9%
metadata-eval69.9%
Simplified69.9%
Taylor expanded in x around 0 80.4%
unpow280.4%
Simplified80.4%
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 99.1%
Taylor expanded in x around inf 99.1%
expm1-def99.1%
Simplified99.1%
Final simplification85.1%
(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 77.5%
associate-+l-77.5%
sub-neg77.5%
sub-neg77.5%
+-commutative77.5%
distribute-neg-in77.5%
remove-double-neg77.5%
metadata-eval77.5%
Simplified77.5%
Taylor expanded in x around 0 71.4%
unpow271.4%
Simplified71.4%
Final simplification71.4%
(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 77.5%
associate-+l-77.5%
sub-neg77.5%
sub-neg77.5%
+-commutative77.5%
distribute-neg-in77.5%
remove-double-neg77.5%
metadata-eval77.5%
Simplified77.5%
Applied egg-rr24.4%
Final simplification24.4%
(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 2023195
(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))))