
(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))) 1e-9) (* x x) (- (* 2.0 (cosh x)) 2.0)))
double code(double x) {
double tmp;
if (((exp(x) - 2.0) + exp(-x)) <= 1e-9) {
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 (((exp(x) - 2.0d0) + exp(-x)) <= 1d-9) 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 (((Math.exp(x) - 2.0) + Math.exp(-x)) <= 1e-9) {
tmp = 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)) <= 1e-9: tmp = 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))) <= 1e-9) 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 (((exp(x) - 2.0) + exp(-x)) <= 1e-9) tmp = 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], 1e-9], N[(x * x), $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 10^{-9}:\\
\;\;\;\;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))) < 1.00000000000000006e-9Initial program 54.1%
associate-+l-54.1%
sub-neg54.1%
sub-neg54.1%
distribute-neg-in54.1%
remove-double-neg54.1%
+-commutative54.1%
metadata-eval54.1%
Simplified54.1%
+-commutative54.1%
associate-+r+54.1%
metadata-eval54.1%
sub-neg54.1%
add-exp-log54.0%
+-commutative54.0%
sub-neg54.0%
metadata-eval54.0%
associate-+r+54.1%
+-commutative54.1%
+-commutative54.1%
cosh-undef54.1%
Applied egg-rr54.1%
Taylor expanded in x around 0 49.5%
*-commutative49.5%
exp-to-pow99.9%
pow299.9%
Applied egg-rr99.9%
if 1.00000000000000006e-9 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 88.4%
associate-+l-88.0%
sub-neg88.0%
sub-neg88.0%
distribute-neg-in88.0%
remove-double-neg88.0%
+-commutative88.0%
metadata-eval88.0%
Simplified88.0%
+-commutative88.0%
associate-+r+88.4%
metadata-eval88.4%
sub-neg88.4%
+-commutative88.4%
associate-+r-88.2%
+-commutative88.2%
cosh-undef88.2%
Applied egg-rr88.2%
Final simplification99.5%
(FPCore (x) :precision binary64 (+ (* 4.96031746031746e-5 (pow x 8.0)) (+ (* 0.002777777777777778 (pow x 6.0)) (fma x x (* 0.08333333333333333 (pow x 4.0))))))
double code(double x) {
return (4.96031746031746e-5 * pow(x, 8.0)) + ((0.002777777777777778 * pow(x, 6.0)) + fma(x, x, (0.08333333333333333 * pow(x, 4.0))));
}
function code(x) return Float64(Float64(4.96031746031746e-5 * (x ^ 8.0)) + Float64(Float64(0.002777777777777778 * (x ^ 6.0)) + fma(x, x, Float64(0.08333333333333333 * (x ^ 4.0))))) end
code[x_] := N[(N[(4.96031746031746e-5 * N[Power[x, 8.0], $MachinePrecision]), $MachinePrecision] + N[(N[(0.002777777777777778 * N[Power[x, 6.0], $MachinePrecision]), $MachinePrecision] + N[(x * x + N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
4.96031746031746 \cdot 10^{-5} \cdot {x}^{8} + \left(0.002777777777777778 \cdot {x}^{6} + \mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)\right)
\end{array}
Initial program 55.3%
associate-+l-55.3%
sub-neg55.3%
sub-neg55.3%
distribute-neg-in55.3%
remove-double-neg55.3%
+-commutative55.3%
metadata-eval55.3%
Simplified55.3%
Taylor expanded in x around 0 98.7%
+-commutative98.7%
unpow298.7%
fma-define98.7%
Applied egg-rr98.7%
Final simplification98.7%
(FPCore (x) :precision binary64 (+ (* 0.002777777777777778 (pow x 6.0)) (fma x x (* 0.08333333333333333 (pow x 4.0)))))
double code(double x) {
return (0.002777777777777778 * pow(x, 6.0)) + fma(x, x, (0.08333333333333333 * pow(x, 4.0)));
}
function code(x) return Float64(Float64(0.002777777777777778 * (x ^ 6.0)) + fma(x, x, Float64(0.08333333333333333 * (x ^ 4.0)))) end
code[x_] := N[(N[(0.002777777777777778 * N[Power[x, 6.0], $MachinePrecision]), $MachinePrecision] + N[(x * x + N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.002777777777777778 \cdot {x}^{6} + \mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)
\end{array}
Initial program 55.3%
associate-+l-55.3%
sub-neg55.3%
sub-neg55.3%
distribute-neg-in55.3%
remove-double-neg55.3%
+-commutative55.3%
metadata-eval55.3%
Simplified55.3%
Taylor expanded in x around 0 98.6%
+-commutative98.7%
unpow298.7%
fma-define98.7%
Applied egg-rr98.6%
Final simplification98.6%
(FPCore (x) :precision binary64 (fma x x (* 0.08333333333333333 (pow x 4.0))))
double code(double x) {
return fma(x, x, (0.08333333333333333 * pow(x, 4.0)));
}
function code(x) return fma(x, x, Float64(0.08333333333333333 * (x ^ 4.0))) end
code[x_] := N[(x * x + N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)
\end{array}
Initial program 55.3%
associate-+l-55.3%
sub-neg55.3%
sub-neg55.3%
distribute-neg-in55.3%
remove-double-neg55.3%
+-commutative55.3%
metadata-eval55.3%
Simplified55.3%
Taylor expanded in x around 0 98.2%
+-commutative98.7%
unpow298.7%
fma-define98.7%
Applied egg-rr98.2%
Final simplification98.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 55.3%
associate-+l-55.3%
sub-neg55.3%
sub-neg55.3%
distribute-neg-in55.3%
remove-double-neg55.3%
+-commutative55.3%
metadata-eval55.3%
Simplified55.3%
+-commutative55.3%
associate-+r+55.3%
metadata-eval55.3%
sub-neg55.3%
add-exp-log55.2%
+-commutative55.2%
sub-neg55.2%
metadata-eval55.2%
associate-+r+55.2%
+-commutative55.2%
+-commutative55.2%
cosh-undef55.2%
Applied egg-rr55.2%
Taylor expanded in x around 0 47.9%
*-commutative47.9%
exp-to-pow97.5%
pow297.5%
Applied egg-rr97.5%
Final simplification97.5%
(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 55.3%
associate-+l-55.3%
sub-neg55.3%
sub-neg55.3%
distribute-neg-in55.3%
remove-double-neg55.3%
+-commutative55.3%
metadata-eval55.3%
Simplified55.3%
Taylor expanded in x around 0 52.5%
Taylor expanded in x around 0 6.1%
Final simplification6.1%
(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 2024046
(FPCore (x)
:name "exp2 (problem 3.3.7)"
:precision binary64
:pre (<= (fabs x) 710.0)
:herbie-target
(* 4.0 (* (sinh (/ x 2.0)) (sinh (/ x 2.0))))
(+ (- (exp x) 2.0) (exp (- x))))