
(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))) 4e-7)
(+
(* 0.002777777777777778 (pow x 6.0))
(* (* x x) (+ (* 0.08333333333333333 (* x x)) 1.0)))
(- (* 2.0 (cosh x)) 2.0)))
double code(double x) {
double tmp;
if (((exp(x) - 2.0) + exp(-x)) <= 4e-7) {
tmp = (0.002777777777777778 * pow(x, 6.0)) + ((x * x) * ((0.08333333333333333 * (x * x)) + 1.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 (((exp(x) - 2.0d0) + exp(-x)) <= 4d-7) then
tmp = (0.002777777777777778d0 * (x ** 6.0d0)) + ((x * x) * ((0.08333333333333333d0 * (x * x)) + 1.0d0))
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)) <= 4e-7) {
tmp = (0.002777777777777778 * Math.pow(x, 6.0)) + ((x * x) * ((0.08333333333333333 * (x * x)) + 1.0));
} 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)) <= 4e-7: tmp = (0.002777777777777778 * math.pow(x, 6.0)) + ((x * x) * ((0.08333333333333333 * (x * x)) + 1.0)) 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))) <= 4e-7) tmp = Float64(Float64(0.002777777777777778 * (x ^ 6.0)) + Float64(Float64(x * x) * Float64(Float64(0.08333333333333333 * Float64(x * x)) + 1.0))); 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)) <= 4e-7) tmp = (0.002777777777777778 * (x ^ 6.0)) + ((x * x) * ((0.08333333333333333 * (x * x)) + 1.0)); 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], 4e-7], N[(N[(0.002777777777777778 * N[Power[x, 6.0], $MachinePrecision]), $MachinePrecision] + N[(N[(x * x), $MachinePrecision] * N[(N[(0.08333333333333333 * N[(x * x), $MachinePrecision]), $MachinePrecision] + 1.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 4 \cdot 10^{-7}:\\
\;\;\;\;0.002777777777777778 \cdot {x}^{6} + \left(x \cdot x\right) \cdot \left(0.08333333333333333 \cdot \left(x \cdot x\right) + 1\right)\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \cosh x - 2\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 3.9999999999999998e-7Initial program 64.0%
+-commutative64.0%
sub-neg64.0%
+-commutative64.0%
associate-+l+64.0%
sub-neg64.0%
remove-double-neg64.0%
sub-neg64.0%
neg-sub064.0%
associate--r-64.0%
+-commutative64.0%
--rgt-identity64.0%
sub-neg64.0%
metadata-eval64.0%
Simplified64.0%
Taylor expanded in x around 0 100.0%
metadata-eval100.0%
pow-prod-up100.0%
associate-*r*100.0%
distribute-lft1-in100.0%
unpow2100.0%
unpow2100.0%
Applied egg-rr100.0%
if 3.9999999999999998e-7 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 100.0%
+-commutative100.0%
sub-neg100.0%
+-commutative100.0%
associate-+l+100.0%
sub-neg100.0%
remove-double-neg100.0%
sub-neg100.0%
neg-sub0100.0%
associate--r-100.0%
+-commutative100.0%
--rgt-identity100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
associate-+r+100.0%
metadata-eval100.0%
sub-neg100.0%
cosh-undef100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= x 0.006) (* (* x x) (- (* 0.08333333333333333 (* x x)) -1.0)) (- (* 2.0 (cosh x)) 2.0)))
double code(double x) {
double tmp;
if (x <= 0.006) {
tmp = (x * x) * ((0.08333333333333333 * (x * x)) - -1.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.006d0) then
tmp = (x * x) * ((0.08333333333333333d0 * (x * x)) - (-1.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.006) {
tmp = (x * x) * ((0.08333333333333333 * (x * x)) - -1.0);
} else {
tmp = (2.0 * Math.cosh(x)) - 2.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.006: tmp = (x * x) * ((0.08333333333333333 * (x * x)) - -1.0) else: tmp = (2.0 * math.cosh(x)) - 2.0 return tmp
function code(x) tmp = 0.0 if (x <= 0.006) tmp = Float64(Float64(x * x) * Float64(Float64(0.08333333333333333 * Float64(x * x)) - -1.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.006) tmp = (x * x) * ((0.08333333333333333 * (x * x)) - -1.0); else tmp = (2.0 * cosh(x)) - 2.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.006], N[(N[(x * x), $MachinePrecision] * N[(N[(0.08333333333333333 * N[(x * x), $MachinePrecision]), $MachinePrecision] - -1.0), $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.006:\\
\;\;\;\;\left(x \cdot x\right) \cdot \left(0.08333333333333333 \cdot \left(x \cdot x\right) - -1\right)\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \cosh x - 2\\
\end{array}
\end{array}
if x < 0.0060000000000000001Initial program 76.8%
+-commutative76.8%
sub-neg76.8%
+-commutative76.8%
associate-+l+76.8%
sub-neg76.8%
remove-double-neg76.8%
sub-neg76.8%
neg-sub076.8%
associate--r-76.8%
+-commutative76.8%
--rgt-identity76.8%
sub-neg76.8%
metadata-eval76.8%
Simplified76.8%
Taylor expanded in x around 0 92.2%
fma-def92.2%
unpow292.2%
Simplified92.2%
fma-udef92.2%
unpow292.2%
+-commutative92.2%
unpow292.2%
Applied egg-rr92.2%
metadata-eval92.2%
pow-sqr92.2%
pow292.2%
pow292.2%
associate-*r*92.2%
distribute-rgt1-in92.2%
fma-def92.2%
Applied egg-rr92.2%
metadata-eval92.2%
metadata-eval92.2%
fma-neg92.2%
metadata-eval92.2%
Applied egg-rr92.2%
if 0.0060000000000000001 < x Initial program 100.0%
+-commutative100.0%
sub-neg100.0%
+-commutative100.0%
associate-+l+100.0%
sub-neg100.0%
remove-double-neg100.0%
sub-neg100.0%
neg-sub0100.0%
associate--r-100.0%
+-commutative100.0%
--rgt-identity100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
associate-+r+100.0%
metadata-eval100.0%
sub-neg100.0%
cosh-undef100.0%
Applied egg-rr100.0%
Final simplification94.4%
(FPCore (x) :precision binary64 (if (<= x 2.6) (* (* x x) (- (* 0.08333333333333333 (* x x)) -1.0)) (expm1 x)))
double code(double x) {
double tmp;
if (x <= 2.6) {
tmp = (x * x) * ((0.08333333333333333 * (x * x)) - -1.0);
} else {
tmp = expm1(x);
}
return tmp;
}
public static double code(double x) {
double tmp;
if (x <= 2.6) {
tmp = (x * x) * ((0.08333333333333333 * (x * x)) - -1.0);
} else {
tmp = Math.expm1(x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 2.6: tmp = (x * x) * ((0.08333333333333333 * (x * x)) - -1.0) else: tmp = math.expm1(x) return tmp
function code(x) tmp = 0.0 if (x <= 2.6) tmp = Float64(Float64(x * x) * Float64(Float64(0.08333333333333333 * Float64(x * x)) - -1.0)); else tmp = expm1(x); end return tmp end
code[x_] := If[LessEqual[x, 2.6], N[(N[(x * x), $MachinePrecision] * N[(N[(0.08333333333333333 * N[(x * x), $MachinePrecision]), $MachinePrecision] - -1.0), $MachinePrecision]), $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.6:\\
\;\;\;\;\left(x \cdot x\right) \cdot \left(0.08333333333333333 \cdot \left(x \cdot x\right) - -1\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\
\end{array}
\end{array}
if x < 2.60000000000000009Initial program 76.8%
+-commutative76.8%
sub-neg76.8%
+-commutative76.8%
associate-+l+76.8%
sub-neg76.8%
remove-double-neg76.8%
sub-neg76.8%
neg-sub076.8%
associate--r-76.8%
+-commutative76.8%
--rgt-identity76.8%
sub-neg76.8%
metadata-eval76.8%
Simplified76.8%
Taylor expanded in x around 0 92.2%
fma-def92.2%
unpow292.2%
Simplified92.2%
fma-udef92.2%
unpow292.2%
+-commutative92.2%
unpow292.2%
Applied egg-rr92.2%
metadata-eval92.2%
pow-sqr92.2%
pow292.2%
pow292.2%
associate-*r*92.2%
distribute-rgt1-in92.2%
fma-def92.2%
Applied egg-rr92.2%
metadata-eval92.2%
metadata-eval92.2%
fma-neg92.2%
metadata-eval92.2%
Applied egg-rr92.2%
if 2.60000000000000009 < x Initial program 100.0%
+-commutative100.0%
sub-neg100.0%
+-commutative100.0%
associate-+l+100.0%
sub-neg100.0%
remove-double-neg100.0%
sub-neg100.0%
neg-sub0100.0%
associate--r-100.0%
+-commutative100.0%
--rgt-identity100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 99.1%
Taylor expanded in x around inf 99.1%
expm1-def99.1%
Simplified99.1%
Final simplification94.2%
(FPCore (x) :precision binary64 (if (<= x 3.5) (* x x) (* (* x x) (* 0.08333333333333333 (* x x)))))
double code(double x) {
double tmp;
if (x <= 3.5) {
tmp = x * x;
} else {
tmp = (x * x) * (0.08333333333333333 * (x * x));
}
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 = (x * x) * (0.08333333333333333d0 * (x * x))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 3.5) {
tmp = x * x;
} else {
tmp = (x * x) * (0.08333333333333333 * (x * x));
}
return tmp;
}
def code(x): tmp = 0 if x <= 3.5: tmp = x * x else: tmp = (x * x) * (0.08333333333333333 * (x * x)) return tmp
function code(x) tmp = 0.0 if (x <= 3.5) tmp = Float64(x * x); else tmp = Float64(Float64(x * x) * Float64(0.08333333333333333 * Float64(x * x))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 3.5) tmp = x * x; else tmp = (x * x) * (0.08333333333333333 * (x * x)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 3.5], N[(x * x), $MachinePrecision], N[(N[(x * x), $MachinePrecision] * N[(0.08333333333333333 * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.5:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;\left(x \cdot x\right) \cdot \left(0.08333333333333333 \cdot \left(x \cdot x\right)\right)\\
\end{array}
\end{array}
if x < 3.5Initial program 76.8%
+-commutative76.8%
sub-neg76.8%
+-commutative76.8%
associate-+l+76.8%
sub-neg76.8%
remove-double-neg76.8%
sub-neg76.8%
neg-sub076.8%
associate--r-76.8%
+-commutative76.8%
--rgt-identity76.8%
sub-neg76.8%
metadata-eval76.8%
Simplified76.8%
Taylor expanded in x around 0 81.8%
unpow281.8%
Simplified81.8%
if 3.5 < x Initial program 100.0%
+-commutative100.0%
sub-neg100.0%
+-commutative100.0%
associate-+l+100.0%
sub-neg100.0%
remove-double-neg100.0%
sub-neg100.0%
neg-sub0100.0%
associate--r-100.0%
+-commutative100.0%
--rgt-identity100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 67.6%
fma-def67.6%
unpow267.6%
Simplified67.6%
fma-udef67.6%
unpow267.6%
+-commutative67.6%
unpow267.6%
Applied egg-rr67.6%
metadata-eval67.6%
pow-sqr67.6%
pow267.6%
pow267.6%
associate-*r*67.6%
distribute-rgt1-in67.6%
fma-def67.6%
Applied egg-rr67.6%
Taylor expanded in x around inf 67.6%
unpow267.6%
Simplified67.6%
Final simplification77.8%
(FPCore (x) :precision binary64 (* (* x x) (- (* 0.08333333333333333 (* x x)) -1.0)))
double code(double x) {
return (x * x) * ((0.08333333333333333 * (x * x)) - -1.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x * x) * ((0.08333333333333333d0 * (x * x)) - (-1.0d0))
end function
public static double code(double x) {
return (x * x) * ((0.08333333333333333 * (x * x)) - -1.0);
}
def code(x): return (x * x) * ((0.08333333333333333 * (x * x)) - -1.0)
function code(x) return Float64(Float64(x * x) * Float64(Float64(0.08333333333333333 * Float64(x * x)) - -1.0)) end
function tmp = code(x) tmp = (x * x) * ((0.08333333333333333 * (x * x)) - -1.0); end
code[x_] := N[(N[(x * x), $MachinePrecision] * N[(N[(0.08333333333333333 * N[(x * x), $MachinePrecision]), $MachinePrecision] - -1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x \cdot x\right) \cdot \left(0.08333333333333333 \cdot \left(x \cdot x\right) - -1\right)
\end{array}
Initial program 83.4%
+-commutative83.4%
sub-neg83.4%
+-commutative83.4%
associate-+l+83.4%
sub-neg83.4%
remove-double-neg83.4%
sub-neg83.4%
neg-sub083.4%
associate--r-83.4%
+-commutative83.4%
--rgt-identity83.4%
sub-neg83.4%
metadata-eval83.4%
Simplified83.4%
Taylor expanded in x around 0 85.2%
fma-def85.2%
unpow285.2%
Simplified85.2%
fma-udef85.2%
unpow285.2%
+-commutative85.2%
unpow285.2%
Applied egg-rr85.2%
metadata-eval85.2%
pow-sqr85.2%
pow285.2%
pow285.2%
associate-*r*85.2%
distribute-rgt1-in85.2%
fma-def85.2%
Applied egg-rr85.2%
metadata-eval85.2%
metadata-eval85.2%
fma-neg85.2%
metadata-eval85.2%
Applied egg-rr85.2%
Final simplification85.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 83.4%
+-commutative83.4%
sub-neg83.4%
+-commutative83.4%
associate-+l+83.4%
sub-neg83.4%
remove-double-neg83.4%
sub-neg83.4%
neg-sub083.4%
associate--r-83.4%
+-commutative83.4%
--rgt-identity83.4%
sub-neg83.4%
metadata-eval83.4%
Simplified83.4%
Taylor expanded in x around 0 73.3%
unpow273.3%
Simplified73.3%
Final simplification73.3%
(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 83.4%
+-commutative83.4%
sub-neg83.4%
+-commutative83.4%
associate-+l+83.4%
sub-neg83.4%
remove-double-neg83.4%
sub-neg83.4%
neg-sub083.4%
associate--r-83.4%
+-commutative83.4%
--rgt-identity83.4%
sub-neg83.4%
metadata-eval83.4%
Simplified83.4%
Taylor expanded in x around 0 57.7%
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 2023297
(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))))