
(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))) 2e-13) (fma x x (* 0.08333333333333333 (pow x 4.0))) (+ (* 2.0 (cosh x)) -2.0)))
double code(double x) {
double tmp;
if (((exp(x) - 2.0) + exp(-x)) <= 2e-13) {
tmp = fma(x, x, (0.08333333333333333 * pow(x, 4.0)));
} else {
tmp = (2.0 * cosh(x)) + -2.0;
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) <= 2e-13) tmp = fma(x, x, Float64(0.08333333333333333 * (x ^ 4.0))); else tmp = Float64(Float64(2.0 * cosh(x)) + -2.0); end return tmp end
code[x_] := If[LessEqual[N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision], 2e-13], N[(x * x + 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}\;\left(e^{x} - 2\right) + e^{-x} \leq 2 \cdot 10^{-13}:\\
\;\;\;\;\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \cosh x + -2\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 2.0000000000000001e-13Initial program 49.0%
associate-+l-49.0%
sub-neg49.0%
sub-neg49.0%
+-commutative49.0%
distribute-neg-in49.0%
remove-double-neg49.0%
metadata-eval49.0%
Simplified49.0%
Taylor expanded in x around 0 100.0%
unpow2100.0%
fma-def100.0%
Simplified100.0%
if 2.0000000000000001e-13 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 100.0%
+-commutative100.0%
associate-+r-100.0%
+-commutative100.0%
associate-+r-100.0%
+-commutative100.0%
associate-+l-100.0%
Simplified100.0%
associate--r-100.0%
sub-neg100.0%
metadata-eval100.0%
+-commutative100.0%
associate-+r+100.0%
cosh-undef100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= (+ (- (exp x) 2.0) (exp (- x))) 2e-13) (+ (* 0.08333333333333333 (pow x 4.0)) (* x x)) (+ (* 2.0 (cosh x)) -2.0)))
double code(double x) {
double tmp;
if (((exp(x) - 2.0) + exp(-x)) <= 2e-13) {
tmp = (0.08333333333333333 * pow(x, 4.0)) + (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 (((exp(x) - 2.0d0) + exp(-x)) <= 2d-13) then
tmp = (0.08333333333333333d0 * (x ** 4.0d0)) + (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 (((Math.exp(x) - 2.0) + Math.exp(-x)) <= 2e-13) {
tmp = (0.08333333333333333 * Math.pow(x, 4.0)) + (x * x);
} 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)) <= 2e-13: tmp = (0.08333333333333333 * math.pow(x, 4.0)) + (x * x) 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))) <= 2e-13) tmp = Float64(Float64(0.08333333333333333 * (x ^ 4.0)) + 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 (((exp(x) - 2.0) + exp(-x)) <= 2e-13) tmp = (0.08333333333333333 * (x ^ 4.0)) + (x * x); 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], 2e-13], N[(N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision] + N[(x * x), $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 2 \cdot 10^{-13}:\\
\;\;\;\;0.08333333333333333 \cdot {x}^{4} + x \cdot x\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \cosh x + -2\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 2.0000000000000001e-13Initial program 49.0%
associate-+l-49.0%
sub-neg49.0%
sub-neg49.0%
+-commutative49.0%
distribute-neg-in49.0%
remove-double-neg49.0%
metadata-eval49.0%
Simplified49.0%
Taylor expanded in x around 0 100.0%
unpow2100.0%
Simplified100.0%
if 2.0000000000000001e-13 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 100.0%
+-commutative100.0%
associate-+r-100.0%
+-commutative100.0%
associate-+r-100.0%
+-commutative100.0%
associate-+l-100.0%
Simplified100.0%
associate--r-100.0%
sub-neg100.0%
metadata-eval100.0%
+-commutative100.0%
associate-+r+100.0%
cosh-undef100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= x 0.00017) (* x x) (+ (* 2.0 (cosh x)) -2.0)))
double code(double x) {
double tmp;
if (x <= 0.00017) {
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.00017d0) 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.00017) {
tmp = x * x;
} else {
tmp = (2.0 * Math.cosh(x)) + -2.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.00017: tmp = x * x else: tmp = (2.0 * math.cosh(x)) + -2.0 return tmp
function code(x) tmp = 0.0 if (x <= 0.00017) 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.00017) tmp = x * x; else tmp = (2.0 * cosh(x)) + -2.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.00017], 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.00017:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \cosh x + -2\\
\end{array}
\end{array}
if x < 1.7e-4Initial program 66.8%
associate-+l-66.8%
sub-neg66.8%
sub-neg66.8%
+-commutative66.8%
distribute-neg-in66.8%
remove-double-neg66.8%
metadata-eval66.8%
Simplified66.8%
Taylor expanded in x around 0 83.0%
unpow283.0%
Simplified83.0%
if 1.7e-4 < x Initial program 100.0%
+-commutative100.0%
associate-+r-100.0%
+-commutative100.0%
associate-+r-100.0%
+-commutative100.0%
associate-+l-100.0%
Simplified100.0%
associate--r-100.0%
sub-neg100.0%
metadata-eval100.0%
+-commutative100.0%
associate-+r+100.0%
cosh-undef100.0%
Applied egg-rr100.0%
Final simplification87.4%
(FPCore (x) :precision binary64 (if (<= x 3.5) (* x x) (* 0.08333333333333333 (pow x 4.0))))
double code(double x) {
double tmp;
if (x <= 3.5) {
tmp = x * x;
} else {
tmp = 0.08333333333333333 * pow(x, 4.0);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 3.5d0) then
tmp = x * x
else
tmp = 0.08333333333333333d0 * (x ** 4.0d0)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 3.5) {
tmp = x * x;
} else {
tmp = 0.08333333333333333 * Math.pow(x, 4.0);
}
return tmp;
}
def code(x): tmp = 0 if x <= 3.5: tmp = x * x else: tmp = 0.08333333333333333 * math.pow(x, 4.0) return tmp
function code(x) tmp = 0.0 if (x <= 3.5) tmp = Float64(x * x); else tmp = Float64(0.08333333333333333 * (x ^ 4.0)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 3.5) tmp = x * x; else tmp = 0.08333333333333333 * (x ^ 4.0); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 3.5], N[(x * x), $MachinePrecision], N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.5:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;0.08333333333333333 \cdot {x}^{4}\\
\end{array}
\end{array}
if x < 3.5Initial program 67.2%
associate-+l-67.1%
sub-neg67.1%
sub-neg67.1%
+-commutative67.1%
distribute-neg-in67.1%
remove-double-neg67.1%
metadata-eval67.1%
Simplified67.1%
Taylor expanded in x around 0 82.4%
unpow282.4%
Simplified82.4%
if 3.5 < 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 68.0%
unpow268.0%
Simplified68.0%
Taylor expanded in x around inf 68.0%
Final simplification78.7%
(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 75.5%
associate-+l-75.5%
sub-neg75.5%
sub-neg75.5%
+-commutative75.5%
distribute-neg-in75.5%
remove-double-neg75.5%
metadata-eval75.5%
Simplified75.5%
Taylor expanded in x around 0 73.9%
unpow273.9%
Simplified73.9%
Final simplification73.9%
(FPCore (x) :precision binary64 2.0)
double code(double x) {
return 2.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 2.0d0
end function
public static double code(double x) {
return 2.0;
}
def code(x): return 2.0
function code(x) return 2.0 end
function tmp = code(x) tmp = 2.0; end
code[x_] := 2.0
\begin{array}{l}
\\
2
\end{array}
Initial program 75.5%
associate-+l-75.5%
sub-neg75.5%
sub-neg75.5%
+-commutative75.5%
distribute-neg-in75.5%
remove-double-neg75.5%
metadata-eval75.5%
Simplified75.5%
+-commutative75.5%
metadata-eval75.5%
sub-neg75.5%
associate--r-75.5%
add-sqr-sqrt40.2%
sqrt-unprod74.6%
sqr-neg74.6%
sqrt-unprod34.4%
add-sqr-sqrt48.7%
Applied egg-rr48.7%
sub-neg48.7%
neg-sub048.7%
associate--r-48.7%
metadata-eval48.7%
+-commutative48.7%
rem-square-sqrt25.1%
fabs-sqr25.1%
rem-square-sqrt28.1%
+-commutative28.1%
metadata-eval28.1%
associate--r-28.1%
neg-sub028.1%
fabs-neg28.1%
rem-square-sqrt2.9%
fabs-sqr2.9%
rem-square-sqrt3.0%
sub-neg3.0%
+-commutative3.0%
associate-+r+3.0%
sub-neg3.0%
+-inverses3.8%
metadata-eval3.8%
Simplified3.8%
Final simplification3.8%
(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 2023274
(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))))