
(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 9 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) 2.0) (exp (- x)))))
(if (<= t_0 5e-6)
(fma
x
x
(+
(* 0.08333333333333333 (pow x 4.0))
(* 0.002777777777777778 (pow x 6.0))))
t_0)))
double code(double x) {
double t_0 = (exp(x) - 2.0) + exp(-x);
double tmp;
if (t_0 <= 5e-6) {
tmp = fma(x, x, ((0.08333333333333333 * pow(x, 4.0)) + (0.002777777777777778 * pow(x, 6.0))));
} else {
tmp = t_0;
}
return tmp;
}
function code(x) t_0 = Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) tmp = 0.0 if (t_0 <= 5e-6) tmp = fma(x, x, Float64(Float64(0.08333333333333333 * (x ^ 4.0)) + Float64(0.002777777777777778 * (x ^ 6.0)))); else tmp = t_0; end return tmp end
code[x_] := Block[{t$95$0 = N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 5e-6], N[(x * x + N[(N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision] + N[(0.002777777777777778 * N[Power[x, 6.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(e^{x} - 2\right) + e^{-x}\\
\mathbf{if}\;t_0 \leq 5 \cdot 10^{-6}:\\
\;\;\;\;\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4} + 0.002777777777777778 \cdot {x}^{6}\right)\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 5.00000000000000041e-6Initial program 55.1%
Taylor expanded in x around 0 100.0%
associate-+r+100.0%
+-commutative100.0%
unpow2100.0%
fma-def100.0%
+-commutative100.0%
fma-def100.0%
Applied egg-rr100.0%
fma-udef100.0%
Applied egg-rr100.0%
if 5.00000000000000041e-6 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 100.0%
Final simplification100.0%
(FPCore (x)
:precision binary64
(let* ((t_0 (+ (- (exp x) 2.0) (exp (- x)))))
(if (<= t_0 5e-6)
(+
(* 0.002777777777777778 (pow x 6.0))
(fma x x (* 0.08333333333333333 (pow x 4.0))))
t_0)))
double code(double x) {
double t_0 = (exp(x) - 2.0) + exp(-x);
double tmp;
if (t_0 <= 5e-6) {
tmp = (0.002777777777777778 * pow(x, 6.0)) + fma(x, x, (0.08333333333333333 * pow(x, 4.0)));
} else {
tmp = t_0;
}
return tmp;
}
function code(x) t_0 = Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) tmp = 0.0 if (t_0 <= 5e-6) tmp = Float64(Float64(0.002777777777777778 * (x ^ 6.0)) + fma(x, x, Float64(0.08333333333333333 * (x ^ 4.0)))); else tmp = t_0; end return tmp end
code[x_] := Block[{t$95$0 = N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 5e-6], 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], t$95$0]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(e^{x} - 2\right) + e^{-x}\\
\mathbf{if}\;t_0 \leq 5 \cdot 10^{-6}:\\
\;\;\;\;0.002777777777777778 \cdot {x}^{6} + \mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 5.00000000000000041e-6Initial program 55.1%
Taylor expanded in x around 0 100.0%
+-commutative99.9%
unpow299.9%
fma-def99.9%
Applied egg-rr100.0%
if 5.00000000000000041e-6 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (let* ((t_0 (+ (- (exp x) 2.0) (exp (- x))))) (if (<= t_0 5e-6) (+ (* 0.08333333333333333 (pow x 4.0)) (pow x 2.0)) t_0)))
double code(double x) {
double t_0 = (exp(x) - 2.0) + exp(-x);
double tmp;
if (t_0 <= 5e-6) {
tmp = (0.08333333333333333 * pow(x, 4.0)) + pow(x, 2.0);
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = (exp(x) - 2.0d0) + exp(-x)
if (t_0 <= 5d-6) then
tmp = (0.08333333333333333d0 * (x ** 4.0d0)) + (x ** 2.0d0)
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x) {
double t_0 = (Math.exp(x) - 2.0) + Math.exp(-x);
double tmp;
if (t_0 <= 5e-6) {
tmp = (0.08333333333333333 * Math.pow(x, 4.0)) + Math.pow(x, 2.0);
} else {
tmp = t_0;
}
return tmp;
}
def code(x): t_0 = (math.exp(x) - 2.0) + math.exp(-x) tmp = 0 if t_0 <= 5e-6: tmp = (0.08333333333333333 * math.pow(x, 4.0)) + math.pow(x, 2.0) else: tmp = t_0 return tmp
function code(x) t_0 = Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) tmp = 0.0 if (t_0 <= 5e-6) tmp = Float64(Float64(0.08333333333333333 * (x ^ 4.0)) + (x ^ 2.0)); else tmp = t_0; end return tmp end
function tmp_2 = code(x) t_0 = (exp(x) - 2.0) + exp(-x); tmp = 0.0; if (t_0 <= 5e-6) tmp = (0.08333333333333333 * (x ^ 4.0)) + (x ^ 2.0); else tmp = t_0; end tmp_2 = tmp; end
code[x_] := Block[{t$95$0 = N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 5e-6], N[(N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision] + N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision], t$95$0]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(e^{x} - 2\right) + e^{-x}\\
\mathbf{if}\;t_0 \leq 5 \cdot 10^{-6}:\\
\;\;\;\;0.08333333333333333 \cdot {x}^{4} + {x}^{2}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 5.00000000000000041e-6Initial program 55.1%
Taylor expanded in x around 0 99.9%
if 5.00000000000000041e-6 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 100.0%
Final simplification99.9%
(FPCore (x) :precision binary64 (let* ((t_0 (+ (- (exp x) 2.0) (exp (- x))))) (if (<= t_0 4e-16) (pow x 2.0) t_0)))
double code(double x) {
double t_0 = (exp(x) - 2.0) + exp(-x);
double tmp;
if (t_0 <= 4e-16) {
tmp = pow(x, 2.0);
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = (exp(x) - 2.0d0) + exp(-x)
if (t_0 <= 4d-16) then
tmp = x ** 2.0d0
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x) {
double t_0 = (Math.exp(x) - 2.0) + Math.exp(-x);
double tmp;
if (t_0 <= 4e-16) {
tmp = Math.pow(x, 2.0);
} else {
tmp = t_0;
}
return tmp;
}
def code(x): t_0 = (math.exp(x) - 2.0) + math.exp(-x) tmp = 0 if t_0 <= 4e-16: tmp = math.pow(x, 2.0) else: tmp = t_0 return tmp
function code(x) t_0 = Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) tmp = 0.0 if (t_0 <= 4e-16) tmp = x ^ 2.0; else tmp = t_0; end return tmp end
function tmp_2 = code(x) t_0 = (exp(x) - 2.0) + exp(-x); tmp = 0.0; if (t_0 <= 4e-16) tmp = x ^ 2.0; else tmp = t_0; end tmp_2 = tmp; end
code[x_] := Block[{t$95$0 = N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 4e-16], N[Power[x, 2.0], $MachinePrecision], t$95$0]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(e^{x} - 2\right) + e^{-x}\\
\mathbf{if}\;t_0 \leq 4 \cdot 10^{-16}:\\
\;\;\;\;{x}^{2}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 3.9999999999999999e-16Initial program 54.7%
Taylor expanded in x around 0 100.0%
if 3.9999999999999999e-16 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 99.4%
Final simplification99.7%
(FPCore (x) :precision binary64 (let* ((t_0 (+ (- (exp x) 2.0) (exp (- x))))) (if (<= t_0 5e-6) (fma x x (* 0.08333333333333333 (pow x 4.0))) t_0)))
double code(double x) {
double t_0 = (exp(x) - 2.0) + exp(-x);
double tmp;
if (t_0 <= 5e-6) {
tmp = fma(x, x, (0.08333333333333333 * pow(x, 4.0)));
} else {
tmp = t_0;
}
return tmp;
}
function code(x) t_0 = Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) tmp = 0.0 if (t_0 <= 5e-6) tmp = fma(x, x, Float64(0.08333333333333333 * (x ^ 4.0))); else tmp = t_0; end return tmp end
code[x_] := Block[{t$95$0 = N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 5e-6], N[(x * x + N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(e^{x} - 2\right) + e^{-x}\\
\mathbf{if}\;t_0 \leq 5 \cdot 10^{-6}:\\
\;\;\;\;\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 5.00000000000000041e-6Initial program 55.1%
Taylor expanded in x around 0 99.9%
+-commutative99.9%
unpow299.9%
fma-def99.9%
Applied egg-rr99.9%
if 5.00000000000000041e-6 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 100.0%
Final simplification99.9%
(FPCore (x) :precision binary64 (if (<= x 4.5) (pow x 2.0) (cbrt (* (pow x 18.0) 2.1433470507544582e-8))))
double code(double x) {
double tmp;
if (x <= 4.5) {
tmp = pow(x, 2.0);
} else {
tmp = cbrt((pow(x, 18.0) * 2.1433470507544582e-8));
}
return tmp;
}
public static double code(double x) {
double tmp;
if (x <= 4.5) {
tmp = Math.pow(x, 2.0);
} else {
tmp = Math.cbrt((Math.pow(x, 18.0) * 2.1433470507544582e-8));
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 4.5) tmp = x ^ 2.0; else tmp = cbrt(Float64((x ^ 18.0) * 2.1433470507544582e-8)); end return tmp end
code[x_] := If[LessEqual[x, 4.5], N[Power[x, 2.0], $MachinePrecision], N[Power[N[(N[Power[x, 18.0], $MachinePrecision] * 2.1433470507544582e-8), $MachinePrecision], 1/3], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.5:\\
\;\;\;\;{x}^{2}\\
\mathbf{else}:\\
\;\;\;\;\sqrt[3]{{x}^{18} \cdot 2.1433470507544582 \cdot 10^{-8}}\\
\end{array}
\end{array}
if x < 4.5Initial program 69.5%
Taylor expanded in x around 0 83.2%
if 4.5 < x Initial program 100.0%
Taylor expanded in x around 0 83.4%
Taylor expanded in x around inf 83.4%
add-sqr-sqrt83.4%
sqrt-unprod92.4%
*-commutative92.4%
*-commutative92.4%
swap-sqr92.4%
pow-prod-up92.4%
metadata-eval92.4%
metadata-eval92.4%
Applied egg-rr92.4%
add-cbrt-cube95.4%
pow1/395.4%
pow395.4%
*-commutative95.4%
sqrt-prod95.4%
metadata-eval95.4%
sqrt-pow195.4%
metadata-eval95.4%
*-commutative95.4%
unpow-prod-down95.4%
pow395.4%
pow-prod-up95.4%
metadata-eval95.4%
pow-prod-up95.4%
metadata-eval95.4%
metadata-eval95.4%
Applied egg-rr95.4%
unpow1/395.4%
Simplified95.4%
Final simplification86.2%
(FPCore (x) :precision binary64 (if (<= x 4.5) (pow x 2.0) (* 0.002777777777777778 (pow x 6.0))))
double code(double x) {
double tmp;
if (x <= 4.5) {
tmp = pow(x, 2.0);
} else {
tmp = 0.002777777777777778 * pow(x, 6.0);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 4.5d0) then
tmp = x ** 2.0d0
else
tmp = 0.002777777777777778d0 * (x ** 6.0d0)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 4.5) {
tmp = Math.pow(x, 2.0);
} else {
tmp = 0.002777777777777778 * Math.pow(x, 6.0);
}
return tmp;
}
def code(x): tmp = 0 if x <= 4.5: tmp = math.pow(x, 2.0) else: tmp = 0.002777777777777778 * math.pow(x, 6.0) return tmp
function code(x) tmp = 0.0 if (x <= 4.5) tmp = x ^ 2.0; else tmp = Float64(0.002777777777777778 * (x ^ 6.0)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 4.5) tmp = x ^ 2.0; else tmp = 0.002777777777777778 * (x ^ 6.0); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 4.5], N[Power[x, 2.0], $MachinePrecision], N[(0.002777777777777778 * N[Power[x, 6.0], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.5:\\
\;\;\;\;{x}^{2}\\
\mathbf{else}:\\
\;\;\;\;0.002777777777777778 \cdot {x}^{6}\\
\end{array}
\end{array}
if x < 4.5Initial program 69.5%
Taylor expanded in x around 0 83.2%
if 4.5 < x Initial program 100.0%
Taylor expanded in x around 0 83.4%
Taylor expanded in x around inf 83.4%
Final simplification83.2%
(FPCore (x) :precision binary64 (pow x 2.0))
double code(double x) {
return pow(x, 2.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = x ** 2.0d0
end function
public static double code(double x) {
return Math.pow(x, 2.0);
}
def code(x): return math.pow(x, 2.0)
function code(x) return x ^ 2.0 end
function tmp = code(x) tmp = x ^ 2.0; end
code[x_] := N[Power[x, 2.0], $MachinePrecision]
\begin{array}{l}
\\
{x}^{2}
\end{array}
Initial program 77.0%
Taylor expanded in x around 0 78.0%
Final simplification78.0%
(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 77.0%
Applied egg-rr28.1%
Final simplification28.1%
(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 2023322
(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))))