
(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 7 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.002)
(+
(* 4.96031746031746e-5 (pow x 8.0))
(+
(* 0.002777777777777778 (pow x 6.0))
(+ (* x x) (* (pow x 4.0) 0.08333333333333333))))
(- (* 2.0 (cosh x)) 2.0)))
double code(double x) {
double tmp;
if (((exp(x) - 2.0) + exp(-x)) <= 0.002) {
tmp = (4.96031746031746e-5 * pow(x, 8.0)) + ((0.002777777777777778 * pow(x, 6.0)) + ((x * x) + (pow(x, 4.0) * 0.08333333333333333)));
} 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.002d0) then
tmp = (4.96031746031746d-5 * (x ** 8.0d0)) + ((0.002777777777777778d0 * (x ** 6.0d0)) + ((x * x) + ((x ** 4.0d0) * 0.08333333333333333d0)))
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.002) {
tmp = (4.96031746031746e-5 * Math.pow(x, 8.0)) + ((0.002777777777777778 * Math.pow(x, 6.0)) + ((x * x) + (Math.pow(x, 4.0) * 0.08333333333333333)));
} 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.002: tmp = (4.96031746031746e-5 * math.pow(x, 8.0)) + ((0.002777777777777778 * math.pow(x, 6.0)) + ((x * x) + (math.pow(x, 4.0) * 0.08333333333333333))) 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.002) tmp = Float64(Float64(4.96031746031746e-5 * (x ^ 8.0)) + Float64(Float64(0.002777777777777778 * (x ^ 6.0)) + Float64(Float64(x * x) + Float64((x ^ 4.0) * 0.08333333333333333)))); 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.002) tmp = (4.96031746031746e-5 * (x ^ 8.0)) + ((0.002777777777777778 * (x ^ 6.0)) + ((x * x) + ((x ^ 4.0) * 0.08333333333333333))); 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.002], N[(N[(4.96031746031746e-5 * N[Power[x, 8.0], $MachinePrecision]), $MachinePrecision] + N[(N[(0.002777777777777778 * N[Power[x, 6.0], $MachinePrecision]), $MachinePrecision] + N[(N[(x * x), $MachinePrecision] + N[(N[Power[x, 4.0], $MachinePrecision] * 0.08333333333333333), $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.002:\\
\;\;\;\;4.96031746031746 \cdot 10^{-5} \cdot {x}^{8} + \left(0.002777777777777778 \cdot {x}^{6} + \left(x \cdot x + {x}^{4} \cdot 0.08333333333333333\right)\right)\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \cosh x - 2\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 2e-3Initial program 42.9%
+-commutative42.9%
associate-+r-42.8%
+-commutative42.8%
associate-+r-42.8%
+-commutative42.8%
associate-+l-42.9%
Simplified42.9%
Taylor expanded in x around 0 100.0%
*-commutative100.0%
pow2100.0%
fma-def100.0%
Applied egg-rr100.0%
fma-udef100.0%
Applied egg-rr100.0%
if 2e-3 < (+.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%
Taylor expanded in x around inf 100.0%
expm1-log1p-u100.0%
expm1-udef100.0%
cosh-undef100.0%
Applied egg-rr100.0%
expm1-def100.0%
expm1-log1p100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= (+ (- (exp x) 2.0) (exp (- x))) 1e-8) (fma x x (* (pow x 4.0) 0.08333333333333333)) (- (* 2.0 (cosh x)) 2.0)))
double code(double x) {
double tmp;
if (((exp(x) - 2.0) + exp(-x)) <= 1e-8) {
tmp = fma(x, x, (pow(x, 4.0) * 0.08333333333333333));
} 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))) <= 1e-8) tmp = fma(x, x, Float64((x ^ 4.0) * 0.08333333333333333)); 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], 1e-8], N[(x * x + N[(N[Power[x, 4.0], $MachinePrecision] * 0.08333333333333333), $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 10^{-8}:\\
\;\;\;\;\mathsf{fma}\left(x, x, {x}^{4} \cdot 0.08333333333333333\right)\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \cosh x - 2\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 1e-8Initial program 42.5%
+-commutative42.5%
associate-+r-42.4%
+-commutative42.4%
associate-+r-42.5%
+-commutative42.5%
associate-+l-42.5%
Simplified42.5%
Taylor expanded in x around 0 100.0%
+-commutative100.0%
unpow2100.0%
fma-def100.0%
Simplified100.0%
if 1e-8 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 99.9%
+-commutative99.9%
associate-+r-99.9%
+-commutative99.9%
associate-+r-99.9%
+-commutative99.9%
associate-+l-99.9%
Simplified99.9%
Taylor expanded in x around inf 99.9%
expm1-log1p-u99.8%
expm1-udef99.8%
cosh-undef99.8%
Applied egg-rr99.8%
expm1-def99.8%
expm1-log1p99.9%
Simplified99.9%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= (+ (- (exp x) 2.0) (exp (- x))) 1e-8) (+ (* x x) (* (pow x 4.0) 0.08333333333333333)) (- (* 2.0 (cosh x)) 2.0)))
double code(double x) {
double tmp;
if (((exp(x) - 2.0) + exp(-x)) <= 1e-8) {
tmp = (x * x) + (pow(x, 4.0) * 0.08333333333333333);
} 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-8) then
tmp = (x * x) + ((x ** 4.0d0) * 0.08333333333333333d0)
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-8) {
tmp = (x * x) + (Math.pow(x, 4.0) * 0.08333333333333333);
} 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-8: tmp = (x * x) + (math.pow(x, 4.0) * 0.08333333333333333) 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-8) tmp = Float64(Float64(x * x) + Float64((x ^ 4.0) * 0.08333333333333333)); 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-8) tmp = (x * x) + ((x ^ 4.0) * 0.08333333333333333); 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-8], N[(N[(x * x), $MachinePrecision] + N[(N[Power[x, 4.0], $MachinePrecision] * 0.08333333333333333), $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 10^{-8}:\\
\;\;\;\;x \cdot x + {x}^{4} \cdot 0.08333333333333333\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \cosh x - 2\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 1e-8Initial program 42.5%
+-commutative42.5%
associate-+r-42.4%
+-commutative42.4%
associate-+r-42.5%
+-commutative42.5%
associate-+l-42.5%
Simplified42.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 1e-8 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 99.9%
+-commutative99.9%
associate-+r-99.9%
+-commutative99.9%
associate-+r-99.9%
+-commutative99.9%
associate-+l-99.9%
Simplified99.9%
Taylor expanded in x around inf 99.9%
expm1-log1p-u99.8%
expm1-udef99.8%
cosh-undef99.8%
Applied egg-rr99.8%
expm1-def99.8%
expm1-log1p99.9%
Simplified99.9%
Final simplification99.9%
(FPCore (x) :precision binary64 (if (<= x 0.00016) (* x x) (- (* 2.0 (cosh x)) 2.0)))
double code(double x) {
double tmp;
if (x <= 0.00016) {
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.00016d0) 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.00016) {
tmp = x * x;
} else {
tmp = (2.0 * Math.cosh(x)) - 2.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.00016: tmp = x * x else: tmp = (2.0 * math.cosh(x)) - 2.0 return tmp
function code(x) tmp = 0.0 if (x <= 0.00016) 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.00016) tmp = x * x; else tmp = (2.0 * cosh(x)) - 2.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.00016], 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.00016:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \cosh x - 2\\
\end{array}
\end{array}
if x < 1.60000000000000013e-4Initial program 64.1%
+-commutative64.1%
associate-+r-64.1%
+-commutative64.1%
associate-+r-64.1%
+-commutative64.1%
associate-+l-64.1%
Simplified64.1%
Taylor expanded in x around 0 79.0%
unpow279.0%
Simplified79.0%
if 1.60000000000000013e-4 < x Initial program 99.8%
+-commutative99.8%
associate-+r-99.8%
+-commutative99.8%
associate-+r-99.7%
+-commutative99.7%
associate-+l-99.8%
Simplified99.8%
Taylor expanded in x around inf 99.8%
expm1-log1p-u99.7%
expm1-udef99.7%
cosh-undef99.7%
Applied egg-rr99.7%
expm1-def99.7%
expm1-log1p99.8%
Simplified99.8%
Final simplification83.4%
(FPCore (x) :precision binary64 (if (<= x 6.0) (* x x) (* 0.16666666666666666 (pow x 3.0))))
double code(double x) {
double tmp;
if (x <= 6.0) {
tmp = x * x;
} else {
tmp = 0.16666666666666666 * pow(x, 3.0);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 6.0d0) then
tmp = x * x
else
tmp = 0.16666666666666666d0 * (x ** 3.0d0)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 6.0) {
tmp = x * x;
} else {
tmp = 0.16666666666666666 * Math.pow(x, 3.0);
}
return tmp;
}
def code(x): tmp = 0 if x <= 6.0: tmp = x * x else: tmp = 0.16666666666666666 * math.pow(x, 3.0) return tmp
function code(x) tmp = 0.0 if (x <= 6.0) tmp = Float64(x * x); else tmp = Float64(0.16666666666666666 * (x ^ 3.0)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 6.0) tmp = x * x; else tmp = 0.16666666666666666 * (x ^ 3.0); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 6.0], N[(x * x), $MachinePrecision], N[(0.16666666666666666 * N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 6:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;0.16666666666666666 \cdot {x}^{3}\\
\end{array}
\end{array}
if x < 6Initial program 64.2%
+-commutative64.2%
associate-+r-64.2%
+-commutative64.2%
associate-+r-64.2%
+-commutative64.2%
associate-+l-64.2%
Simplified64.2%
Taylor expanded in x around 0 78.8%
unpow278.8%
Simplified78.8%
if 6 < 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%
Taylor expanded in x around 0 68.0%
+-commutative68.0%
neg-mul-168.0%
unsub-neg68.0%
cube-mult68.0%
unpow268.0%
associate-*r*68.0%
distribute-rgt-out68.0%
Simplified68.0%
Taylor expanded in x around inf 68.0%
Final simplification76.6%
(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 71.6%
+-commutative71.6%
associate-+r-71.6%
+-commutative71.6%
associate-+r-71.6%
+-commutative71.6%
associate-+l-71.6%
Simplified71.6%
Taylor expanded in x around 0 73.2%
unpow273.2%
Simplified73.2%
Final simplification73.2%
(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 71.6%
+-commutative71.6%
associate-+r-71.6%
+-commutative71.6%
associate-+r-71.6%
+-commutative71.6%
associate-+l-71.6%
Simplified71.6%
Applied egg-rr20.8%
Final simplification20.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 2023275
(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))))