
(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 6 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
(if (<= (+ (- (exp x) 2.0) (exp (- x))) 0.001)
(*
(pow x 2.0)
(+
1.0
(*
(* x x)
(+
0.08333333333333333
(* (* x x) (+ 0.002777777777777778 (* (* x x) 4.96031746031746e-5)))))))
(- (* 2.0 (cosh x)) 2.0)))
double code(double x) {
double tmp;
if (((exp(x) - 2.0) + exp(-x)) <= 0.001) {
tmp = pow(x, 2.0) * (1.0 + ((x * x) * (0.08333333333333333 + ((x * x) * (0.002777777777777778 + ((x * x) * 4.96031746031746e-5))))));
} else {
tmp = (2.0 * cosh(x)) - 2.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (((exp(x) - 2.0d0) + exp(-x)) <= 0.001d0) then
tmp = (x ** 2.0d0) * (1.0d0 + ((x * x) * (0.08333333333333333d0 + ((x * x) * (0.002777777777777778d0 + ((x * x) * 4.96031746031746d-5))))))
else
tmp = (2.0d0 * cosh(x)) - 2.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (((Math.exp(x) - 2.0) + Math.exp(-x)) <= 0.001) {
tmp = Math.pow(x, 2.0) * (1.0 + ((x * x) * (0.08333333333333333 + ((x * x) * (0.002777777777777778 + ((x * x) * 4.96031746031746e-5))))));
} else {
tmp = (2.0 * Math.cosh(x)) - 2.0;
}
return tmp;
}
def code(x): tmp = 0 if ((math.exp(x) - 2.0) + math.exp(-x)) <= 0.001: tmp = math.pow(x, 2.0) * (1.0 + ((x * x) * (0.08333333333333333 + ((x * x) * (0.002777777777777778 + ((x * x) * 4.96031746031746e-5)))))) else: tmp = (2.0 * math.cosh(x)) - 2.0 return tmp
function code(x) tmp = 0.0 if (Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) <= 0.001) tmp = Float64((x ^ 2.0) * Float64(1.0 + Float64(Float64(x * x) * Float64(0.08333333333333333 + Float64(Float64(x * x) * Float64(0.002777777777777778 + Float64(Float64(x * x) * 4.96031746031746e-5))))))); else tmp = Float64(Float64(2.0 * cosh(x)) - 2.0); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (((exp(x) - 2.0) + exp(-x)) <= 0.001) tmp = (x ^ 2.0) * (1.0 + ((x * x) * (0.08333333333333333 + ((x * x) * (0.002777777777777778 + ((x * x) * 4.96031746031746e-5)))))); else tmp = (2.0 * cosh(x)) - 2.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision], 0.001], N[(N[Power[x, 2.0], $MachinePrecision] * N[(1.0 + N[(N[(x * x), $MachinePrecision] * N[(0.08333333333333333 + N[(N[(x * x), $MachinePrecision] * N[(0.002777777777777778 + N[(N[(x * x), $MachinePrecision] * 4.96031746031746e-5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 * N[Cosh[x], $MachinePrecision]), $MachinePrecision] - 2.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 0.001:\\
\;\;\;\;{x}^{2} \cdot \left(1 + \left(x \cdot x\right) \cdot \left(0.08333333333333333 + \left(x \cdot x\right) \cdot \left(0.002777777777777778 + \left(x \cdot x\right) \cdot 4.96031746031746 \cdot 10^{-5}\right)\right)\right)\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \cosh x - 2\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) #s(literal 2 binary64)) (exp.f64 (neg.f64 x))) < 1e-3Initial program 49.7%
associate-+l-49.7%
sub-neg49.7%
sub-neg49.7%
distribute-neg-in49.7%
remove-double-neg49.7%
+-commutative49.7%
metadata-eval49.7%
Simplified49.7%
Taylor expanded in x around 0 100.0%
*-commutative100.0%
Simplified100.0%
unpow2100.0%
Applied egg-rr100.0%
unpow2100.0%
Applied egg-rr100.0%
unpow2100.0%
Applied egg-rr100.0%
if 1e-3 < (+.f64 (-.f64 (exp.f64 x) #s(literal 2 binary64)) (exp.f64 (neg.f64 x))) Initial program 98.4%
associate-+l-98.4%
sub-neg98.4%
sub-neg98.4%
distribute-neg-in98.4%
remove-double-neg98.4%
+-commutative98.4%
metadata-eval98.4%
Simplified98.4%
+-commutative98.4%
associate-+r+98.4%
metadata-eval98.4%
sub-neg98.4%
+-commutative98.4%
associate-+r-98.4%
+-commutative98.4%
cosh-undef98.4%
Applied egg-rr98.4%
(FPCore (x)
:precision binary64
(if (<= x 0.035)
(*
(pow x 2.0)
(+
1.0
(* (* x x) (+ 0.08333333333333333 (* (* x x) 0.002777777777777778)))))
(- (* 2.0 (cosh x)) 2.0)))
double code(double x) {
double tmp;
if (x <= 0.035) {
tmp = pow(x, 2.0) * (1.0 + ((x * x) * (0.08333333333333333 + ((x * x) * 0.002777777777777778))));
} else {
tmp = (2.0 * cosh(x)) - 2.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.035d0) then
tmp = (x ** 2.0d0) * (1.0d0 + ((x * x) * (0.08333333333333333d0 + ((x * x) * 0.002777777777777778d0))))
else
tmp = (2.0d0 * cosh(x)) - 2.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.035) {
tmp = Math.pow(x, 2.0) * (1.0 + ((x * x) * (0.08333333333333333 + ((x * x) * 0.002777777777777778))));
} else {
tmp = (2.0 * Math.cosh(x)) - 2.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.035: tmp = math.pow(x, 2.0) * (1.0 + ((x * x) * (0.08333333333333333 + ((x * x) * 0.002777777777777778)))) else: tmp = (2.0 * math.cosh(x)) - 2.0 return tmp
function code(x) tmp = 0.0 if (x <= 0.035) tmp = Float64((x ^ 2.0) * Float64(1.0 + Float64(Float64(x * x) * Float64(0.08333333333333333 + Float64(Float64(x * x) * 0.002777777777777778))))); else tmp = Float64(Float64(2.0 * cosh(x)) - 2.0); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.035) tmp = (x ^ 2.0) * (1.0 + ((x * x) * (0.08333333333333333 + ((x * x) * 0.002777777777777778)))); else tmp = (2.0 * cosh(x)) - 2.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.035], N[(N[Power[x, 2.0], $MachinePrecision] * N[(1.0 + N[(N[(x * x), $MachinePrecision] * N[(0.08333333333333333 + N[(N[(x * x), $MachinePrecision] * 0.002777777777777778), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 * N[Cosh[x], $MachinePrecision]), $MachinePrecision] - 2.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.035:\\
\;\;\;\;{x}^{2} \cdot \left(1 + \left(x \cdot x\right) \cdot \left(0.08333333333333333 + \left(x \cdot x\right) \cdot 0.002777777777777778\right)\right)\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \cosh x - 2\\
\end{array}
\end{array}
if x < 0.035000000000000003Initial program 50.3%
associate-+l-50.3%
sub-neg50.3%
sub-neg50.3%
distribute-neg-in50.3%
remove-double-neg50.3%
+-commutative50.3%
metadata-eval50.3%
Simplified50.3%
Taylor expanded in x around 0 99.0%
*-commutative99.0%
Simplified99.0%
unpow299.1%
Applied egg-rr99.0%
unpow299.1%
Applied egg-rr99.0%
if 0.035000000000000003 < x Initial program 98.2%
associate-+l-98.2%
sub-neg98.2%
sub-neg98.2%
distribute-neg-in98.2%
remove-double-neg98.2%
+-commutative98.2%
metadata-eval98.2%
Simplified98.2%
+-commutative98.2%
associate-+r+98.2%
metadata-eval98.2%
sub-neg98.2%
+-commutative98.2%
associate-+r-98.2%
+-commutative98.2%
cosh-undef98.2%
Applied egg-rr98.2%
(FPCore (x) :precision binary64 (if (<= x 0.0062) (+ (* x x) (* 0.08333333333333333 (pow x 4.0))) (- (* 2.0 (cosh x)) 2.0)))
double code(double x) {
double tmp;
if (x <= 0.0062) {
tmp = (x * x) + (0.08333333333333333 * pow(x, 4.0));
} else {
tmp = (2.0 * cosh(x)) - 2.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.0062d0) then
tmp = (x * x) + (0.08333333333333333d0 * (x ** 4.0d0))
else
tmp = (2.0d0 * cosh(x)) - 2.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.0062) {
tmp = (x * x) + (0.08333333333333333 * Math.pow(x, 4.0));
} else {
tmp = (2.0 * Math.cosh(x)) - 2.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.0062: tmp = (x * x) + (0.08333333333333333 * math.pow(x, 4.0)) else: tmp = (2.0 * math.cosh(x)) - 2.0 return tmp
function code(x) tmp = 0.0 if (x <= 0.0062) tmp = Float64(Float64(x * x) + Float64(0.08333333333333333 * (x ^ 4.0))); else tmp = Float64(Float64(2.0 * cosh(x)) - 2.0); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.0062) tmp = (x * x) + (0.08333333333333333 * (x ^ 4.0)); else tmp = (2.0 * cosh(x)) - 2.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.0062], N[(N[(x * x), $MachinePrecision] + N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 * N[Cosh[x], $MachinePrecision]), $MachinePrecision] - 2.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0062:\\
\;\;\;\;x \cdot x + 0.08333333333333333 \cdot {x}^{4}\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \cosh x - 2\\
\end{array}
\end{array}
if x < 0.00619999999999999978Initial program 50.3%
associate-+l-50.3%
sub-neg50.3%
sub-neg50.3%
distribute-neg-in50.3%
remove-double-neg50.3%
+-commutative50.3%
metadata-eval50.3%
Simplified50.3%
Taylor expanded in x around 0 98.8%
distribute-rgt-in98.8%
*-lft-identity98.8%
associate-*l*98.8%
pow-sqr98.8%
metadata-eval98.8%
Simplified98.8%
unpow299.1%
Applied egg-rr98.8%
if 0.00619999999999999978 < x Initial program 98.2%
associate-+l-98.2%
sub-neg98.2%
sub-neg98.2%
distribute-neg-in98.2%
remove-double-neg98.2%
+-commutative98.2%
metadata-eval98.2%
Simplified98.2%
+-commutative98.2%
associate-+r+98.2%
metadata-eval98.2%
sub-neg98.2%
+-commutative98.2%
associate-+r-98.2%
+-commutative98.2%
cosh-undef98.2%
Applied egg-rr98.2%
(FPCore (x) :precision binary64 (if (<= x 0.000145) (* x x) (- (* 2.0 (cosh x)) 2.0)))
double code(double x) {
double tmp;
if (x <= 0.000145) {
tmp = x * x;
} else {
tmp = (2.0 * cosh(x)) - 2.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.000145d0) then
tmp = x * x
else
tmp = (2.0d0 * cosh(x)) - 2.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.000145) {
tmp = x * x;
} else {
tmp = (2.0 * Math.cosh(x)) - 2.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.000145: tmp = x * x else: tmp = (2.0 * math.cosh(x)) - 2.0 return tmp
function code(x) tmp = 0.0 if (x <= 0.000145) tmp = Float64(x * x); else tmp = Float64(Float64(2.0 * cosh(x)) - 2.0); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.000145) tmp = x * x; else tmp = (2.0 * cosh(x)) - 2.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.000145], N[(x * x), $MachinePrecision], N[(N[(2.0 * N[Cosh[x], $MachinePrecision]), $MachinePrecision] - 2.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.000145:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \cosh x - 2\\
\end{array}
\end{array}
if x < 1.45e-4Initial program 50.3%
associate-+l-50.3%
sub-neg50.3%
sub-neg50.3%
distribute-neg-in50.3%
remove-double-neg50.3%
+-commutative50.3%
metadata-eval50.3%
Simplified50.3%
Taylor expanded in x around 0 97.8%
unpow299.1%
Applied egg-rr97.8%
if 1.45e-4 < x Initial program 98.2%
associate-+l-98.2%
sub-neg98.2%
sub-neg98.2%
distribute-neg-in98.2%
remove-double-neg98.2%
+-commutative98.2%
metadata-eval98.2%
Simplified98.2%
+-commutative98.2%
associate-+r+98.2%
metadata-eval98.2%
sub-neg98.2%
+-commutative98.2%
associate-+r-98.2%
+-commutative98.2%
cosh-undef98.2%
Applied egg-rr98.2%
(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 50.8%
associate-+l-50.8%
sub-neg50.8%
sub-neg50.8%
distribute-neg-in50.8%
remove-double-neg50.8%
+-commutative50.8%
metadata-eval50.8%
Simplified50.8%
Taylor expanded in x around 0 96.9%
unpow298.4%
Applied egg-rr96.9%
(FPCore (x) :precision binary64 x)
double code(double x) {
return x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = x
end function
public static double code(double x) {
return x;
}
def code(x): return x
function code(x) return x end
function tmp = code(x) tmp = x; end
code[x_] := x
\begin{array}{l}
\\
x
\end{array}
Initial program 50.8%
associate-+l-50.8%
sub-neg50.8%
sub-neg50.8%
distribute-neg-in50.8%
remove-double-neg50.8%
+-commutative50.8%
metadata-eval50.8%
Simplified50.8%
Taylor expanded in x around 0 47.3%
Taylor expanded in x around 0 5.8%
(FPCore (x) :precision binary64 (let* ((t_0 (sinh (/ x 2.0)))) (* 4.0 (* t_0 t_0))))
double code(double x) {
double t_0 = sinh((x / 2.0));
return 4.0 * (t_0 * t_0);
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
t_0 = sinh((x / 2.0d0))
code = 4.0d0 * (t_0 * t_0)
end function
public static double code(double x) {
double t_0 = Math.sinh((x / 2.0));
return 4.0 * (t_0 * t_0);
}
def code(x): t_0 = math.sinh((x / 2.0)) return 4.0 * (t_0 * t_0)
function code(x) t_0 = sinh(Float64(x / 2.0)) return Float64(4.0 * Float64(t_0 * t_0)) end
function tmp = code(x) t_0 = sinh((x / 2.0)); tmp = 4.0 * (t_0 * t_0); end
code[x_] := Block[{t$95$0 = N[Sinh[N[(x / 2.0), $MachinePrecision]], $MachinePrecision]}, N[(4.0 * N[(t$95$0 * t$95$0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sinh \left(\frac{x}{2}\right)\\
4 \cdot \left(t\_0 \cdot t\_0\right)
\end{array}
\end{array}
herbie shell --seed 2024144
(FPCore (x)
:name "exp2 (problem 3.3.7)"
:precision binary64
:pre (<= (fabs x) 710.0)
:alt
(! :herbie-platform default (* 4 (* (sinh (/ x 2)) (sinh (/ x 2)))))
(+ (- (exp x) 2.0) (exp (- x))))