
(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-5)
(+
(* 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-5) {
tmp = (0.002777777777777778 * pow(x, 6.0)) + ((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-5) then
tmp = (0.002777777777777778d0 * (x ** 6.0d0)) + ((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-5) {
tmp = (0.002777777777777778 * Math.pow(x, 6.0)) + ((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-5: tmp = (0.002777777777777778 * math.pow(x, 6.0)) + ((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-5) tmp = Float64(Float64(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
function tmp_2 = code(x) t_0 = exp(-x); tmp = 0.0; if (((exp(x) - 2.0) + t_0) <= 5e-5) tmp = (0.002777777777777778 * (x ^ 6.0)) + ((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-5], N[(N[(0.002777777777777778 * N[Power[x, 6.0], $MachinePrecision]), $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^{-5}:\\
\;\;\;\;0.002777777777777778 \cdot {x}^{6} + \left(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))) < 5.00000000000000024e-5Initial program 47.8%
associate-+l-47.8%
sub-neg47.8%
sub-neg47.8%
+-commutative47.8%
distribute-neg-in47.8%
remove-double-neg47.8%
metadata-eval47.8%
Simplified47.8%
Taylor expanded in x around 0 100.0%
*-commutative100.0%
metadata-eval100.0%
pow-prod-up100.0%
associate-*l*100.0%
fma-def100.0%
unpow2100.0%
unpow2100.0%
unpow2100.0%
Applied egg-rr100.0%
fma-udef100.0%
associate-*r*100.0%
pow2100.0%
metadata-eval100.0%
pow2100.0%
metadata-eval100.0%
sqr-pow100.0%
*-commutative100.0%
+-commutative100.0%
Applied egg-rr100.0%
if 5.00000000000000024e-5 < (+.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-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) <= 5e-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) <= 5d-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) <= 5e-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) <= 5e-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) <= 5e-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) <= 5e-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], 5e-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 5 \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))) < 5.00000000000000018e-11Initial program 47.6%
associate-+l-47.5%
sub-neg47.5%
sub-neg47.5%
+-commutative47.5%
distribute-neg-in47.5%
remove-double-neg47.5%
metadata-eval47.5%
Simplified47.5%
Taylor expanded in x around 0 100.0%
+-commutative100.0%
unpow2100.0%
fma-def100.0%
Simplified100.0%
fma-udef100.0%
Applied egg-rr100.0%
if 5.00000000000000018e-11 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 99.8%
associate-+l-99.8%
sub-neg99.8%
sub-neg99.8%
+-commutative99.8%
distribute-neg-in99.8%
remove-double-neg99.8%
metadata-eval99.8%
Simplified99.8%
Final simplification99.9%
(FPCore (x) :precision binary64 (if (<= x 0.0062) (+ (* x x) (* 0.08333333333333333 (pow x 4.0))) (+ -2.0 (* 2.0 (cosh x)))))
double code(double x) {
double tmp;
if (x <= 0.0062) {
tmp = (x * x) + (0.08333333333333333 * pow(x, 4.0));
} else {
tmp = -2.0 + (2.0 * cosh(x));
}
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) + (2.0d0 * cosh(x))
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 + (2.0 * Math.cosh(x));
}
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 + (2.0 * math.cosh(x)) 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(-2.0 + Float64(2.0 * cosh(x))); 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 + (2.0 * cosh(x)); 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[(-2.0 + N[(2.0 * N[Cosh[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.0062:\\
\;\;\;\;x \cdot x + 0.08333333333333333 \cdot {x}^{4}\\
\mathbf{else}:\\
\;\;\;\;-2 + 2 \cdot \cosh x\\
\end{array}
\end{array}
if x < 0.00619999999999999978Initial program 64.7%
associate-+l-64.6%
sub-neg64.6%
sub-neg64.6%
+-commutative64.6%
distribute-neg-in64.6%
remove-double-neg64.6%
metadata-eval64.6%
Simplified64.6%
Taylor expanded in x around 0 94.3%
+-commutative94.3%
unpow294.3%
fma-def94.3%
Simplified94.3%
fma-udef94.3%
Applied egg-rr94.3%
if 0.00619999999999999978 < 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 simplification95.8%
(FPCore (x) :precision binary64 (if (<= x 0.000195) (* x x) (+ -2.0 (* 2.0 (cosh x)))))
double code(double x) {
double tmp;
if (x <= 0.000195) {
tmp = x * x;
} else {
tmp = -2.0 + (2.0 * cosh(x));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.000195d0) then
tmp = x * x
else
tmp = (-2.0d0) + (2.0d0 * cosh(x))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.000195) {
tmp = x * x;
} else {
tmp = -2.0 + (2.0 * Math.cosh(x));
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.000195: tmp = x * x else: tmp = -2.0 + (2.0 * math.cosh(x)) return tmp
function code(x) tmp = 0.0 if (x <= 0.000195) tmp = Float64(x * x); else tmp = Float64(-2.0 + Float64(2.0 * cosh(x))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.000195) tmp = x * x; else tmp = -2.0 + (2.0 * cosh(x)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.000195], N[(x * x), $MachinePrecision], N[(-2.0 + N[(2.0 * N[Cosh[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.000195:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;-2 + 2 \cdot \cosh x\\
\end{array}
\end{array}
if x < 1.94999999999999996e-4Initial program 64.7%
associate-+l-64.6%
sub-neg64.6%
sub-neg64.6%
+-commutative64.6%
distribute-neg-in64.6%
remove-double-neg64.6%
metadata-eval64.6%
Simplified64.6%
Taylor expanded in x around 0 82.3%
unpow282.3%
Simplified82.3%
if 1.94999999999999996e-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.2%
(FPCore (x) :precision binary64 (if (<= x 1.66) (* x x) (expm1 x)))
double code(double x) {
double tmp;
if (x <= 1.66) {
tmp = x * x;
} else {
tmp = expm1(x);
}
return tmp;
}
public static double code(double x) {
double tmp;
if (x <= 1.66) {
tmp = x * x;
} else {
tmp = Math.expm1(x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.66: tmp = x * x else: tmp = math.expm1(x) return tmp
function code(x) tmp = 0.0 if (x <= 1.66) tmp = Float64(x * x); else tmp = expm1(x); end return tmp end
code[x_] := If[LessEqual[x, 1.66], N[(x * x), $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.66:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\
\end{array}
\end{array}
if x < 1.65999999999999992Initial program 64.8%
associate-+l-64.8%
sub-neg64.8%
sub-neg64.8%
+-commutative64.8%
distribute-neg-in64.8%
remove-double-neg64.8%
metadata-eval64.8%
Simplified64.8%
Taylor expanded in x around 0 82.0%
unpow282.0%
Simplified82.0%
if 1.65999999999999992 < 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 100.0%
Taylor expanded in x around inf 100.0%
expm1-def100.0%
Simplified100.0%
Final simplification86.9%
(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 74.3%
associate-+l-74.3%
sub-neg74.3%
sub-neg74.3%
+-commutative74.3%
distribute-neg-in74.3%
remove-double-neg74.3%
metadata-eval74.3%
Simplified74.3%
Taylor expanded in x around 0 71.8%
unpow271.8%
Simplified71.8%
Final simplification71.8%
(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 74.3%
associate-+l-74.3%
sub-neg74.3%
sub-neg74.3%
+-commutative74.3%
distribute-neg-in74.3%
remove-double-neg74.3%
metadata-eval74.3%
Simplified74.3%
+-commutative74.3%
metadata-eval74.3%
sub-neg74.3%
associate--r-74.3%
add-sqr-sqrt34.8%
sqrt-unprod73.9%
sqr-neg73.9%
sqrt-unprod39.2%
add-sqr-sqrt50.3%
Applied egg-rr50.3%
associate--r-50.3%
sub-neg50.3%
metadata-eval50.3%
+-commutative50.3%
rem-square-sqrt27.0%
fabs-sqr27.0%
rem-square-sqrt29.9%
metadata-eval29.9%
sub-neg29.9%
fabs-sub29.9%
rem-square-sqrt2.9%
fabs-sqr2.9%
rem-square-sqrt2.9%
remove-double-neg2.9%
remove-double-neg2.9%
distribute-neg-out2.9%
+-commutative2.9%
neg-sub02.9%
associate--r-2.9%
metadata-eval2.9%
Simplified3.8%
Final simplification3.8%
(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 74.3%
associate-+l-74.3%
sub-neg74.3%
sub-neg74.3%
+-commutative74.3%
distribute-neg-in74.3%
remove-double-neg74.3%
metadata-eval74.3%
Simplified74.3%
Taylor expanded in x around 0 50.3%
Taylor expanded in x around 0 4.5%
Final simplification4.5%
(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 2023293
(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))))