
(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) 2.0) (exp (- x))))) (if (<= t_0 5e-9) (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-9) {
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-9) 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-9], 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^{-9}:\\
\;\;\;\;\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.0000000000000001e-9Initial program 47.8%
Taylor expanded in x around 0 100.0%
unpow2100.0%
fma-def100.0%
Simplified100.0%
if 5.0000000000000001e-9 < (+.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-9) (+ (* 0.08333333333333333 (pow x 4.0)) (* x x)) t_0)))
double code(double x) {
double t_0 = (exp(x) - 2.0) + exp(-x);
double tmp;
if (t_0 <= 5e-9) {
tmp = (0.08333333333333333 * pow(x, 4.0)) + (x * x);
} 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-9) then
tmp = (0.08333333333333333d0 * (x ** 4.0d0)) + (x * x)
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-9) {
tmp = (0.08333333333333333 * Math.pow(x, 4.0)) + (x * x);
} 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-9: tmp = (0.08333333333333333 * math.pow(x, 4.0)) + (x * x) 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-9) tmp = Float64(Float64(0.08333333333333333 * (x ^ 4.0)) + Float64(x * x)); 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-9) tmp = (0.08333333333333333 * (x ^ 4.0)) + (x * x); 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-9], N[(N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision] + N[(x * x), $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^{-9}:\\
\;\;\;\;0.08333333333333333 \cdot {x}^{4} + x \cdot x\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 5.0000000000000001e-9Initial program 47.8%
Taylor expanded in x around 0 100.0%
unpow2100.0%
Simplified100.0%
if 5.0000000000000001e-9 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= x 5000000000000.0) (+ (* 0.08333333333333333 (pow x 4.0)) (* x x)) (pow (* 3.3489797668038406e-7 (pow x 24.0)) 0.16666666666666666)))
double code(double x) {
double tmp;
if (x <= 5000000000000.0) {
tmp = (0.08333333333333333 * pow(x, 4.0)) + (x * x);
} else {
tmp = pow((3.3489797668038406e-7 * pow(x, 24.0)), 0.16666666666666666);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 5000000000000.0d0) then
tmp = (0.08333333333333333d0 * (x ** 4.0d0)) + (x * x)
else
tmp = (3.3489797668038406d-7 * (x ** 24.0d0)) ** 0.16666666666666666d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 5000000000000.0) {
tmp = (0.08333333333333333 * Math.pow(x, 4.0)) + (x * x);
} else {
tmp = Math.pow((3.3489797668038406e-7 * Math.pow(x, 24.0)), 0.16666666666666666);
}
return tmp;
}
def code(x): tmp = 0 if x <= 5000000000000.0: tmp = (0.08333333333333333 * math.pow(x, 4.0)) + (x * x) else: tmp = math.pow((3.3489797668038406e-7 * math.pow(x, 24.0)), 0.16666666666666666) return tmp
function code(x) tmp = 0.0 if (x <= 5000000000000.0) tmp = Float64(Float64(0.08333333333333333 * (x ^ 4.0)) + Float64(x * x)); else tmp = Float64(3.3489797668038406e-7 * (x ^ 24.0)) ^ 0.16666666666666666; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 5000000000000.0) tmp = (0.08333333333333333 * (x ^ 4.0)) + (x * x); else tmp = (3.3489797668038406e-7 * (x ^ 24.0)) ^ 0.16666666666666666; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 5000000000000.0], N[(N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision], N[Power[N[(3.3489797668038406e-7 * N[Power[x, 24.0], $MachinePrecision]), $MachinePrecision], 0.16666666666666666], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 5000000000000:\\
\;\;\;\;0.08333333333333333 \cdot {x}^{4} + x \cdot x\\
\mathbf{else}:\\
\;\;\;\;{\left(3.3489797668038406 \cdot 10^{-7} \cdot {x}^{24}\right)}^{0.16666666666666666}\\
\end{array}
\end{array}
if x < 5e12Initial program 66.0%
Taylor expanded in x around 0 91.7%
unpow291.7%
Simplified91.7%
if 5e12 < x Initial program 100.0%
Taylor expanded in x around 0 79.9%
unpow279.9%
Simplified79.9%
Taylor expanded in x around inf 79.9%
add-cbrt-cube95.3%
pow395.3%
*-commutative95.3%
unpow-prod-down95.3%
pow-pow95.3%
metadata-eval95.3%
metadata-eval95.3%
Applied egg-rr95.3%
pow1/395.3%
sqr-pow95.3%
pow-prod-down100.0%
*-commutative100.0%
*-commutative100.0%
swap-sqr100.0%
metadata-eval100.0%
pow-prod-up100.0%
metadata-eval100.0%
metadata-eval100.0%
Applied egg-rr100.0%
Final simplification93.6%
(FPCore (x) :precision binary64 (if (<= x 20000000000000.0) (+ (* 0.08333333333333333 (pow x 4.0)) (* x x)) (cbrt (* (pow x 12.0) 0.0005787037037037037))))
double code(double x) {
double tmp;
if (x <= 20000000000000.0) {
tmp = (0.08333333333333333 * pow(x, 4.0)) + (x * x);
} else {
tmp = cbrt((pow(x, 12.0) * 0.0005787037037037037));
}
return tmp;
}
public static double code(double x) {
double tmp;
if (x <= 20000000000000.0) {
tmp = (0.08333333333333333 * Math.pow(x, 4.0)) + (x * x);
} else {
tmp = Math.cbrt((Math.pow(x, 12.0) * 0.0005787037037037037));
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 20000000000000.0) tmp = Float64(Float64(0.08333333333333333 * (x ^ 4.0)) + Float64(x * x)); else tmp = cbrt(Float64((x ^ 12.0) * 0.0005787037037037037)); end return tmp end
code[x_] := If[LessEqual[x, 20000000000000.0], N[(N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision], N[Power[N[(N[Power[x, 12.0], $MachinePrecision] * 0.0005787037037037037), $MachinePrecision], 1/3], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 20000000000000:\\
\;\;\;\;0.08333333333333333 \cdot {x}^{4} + x \cdot x\\
\mathbf{else}:\\
\;\;\;\;\sqrt[3]{{x}^{12} \cdot 0.0005787037037037037}\\
\end{array}
\end{array}
if x < 2e13Initial program 66.0%
Taylor expanded in x around 0 91.7%
unpow291.7%
Simplified91.7%
if 2e13 < x Initial program 100.0%
Taylor expanded in x around 0 79.9%
unpow279.9%
Simplified79.9%
Taylor expanded in x around inf 79.9%
add-cbrt-cube95.3%
pow395.3%
*-commutative95.3%
unpow-prod-down95.3%
pow-pow95.3%
metadata-eval95.3%
metadata-eval95.3%
Applied egg-rr95.3%
Final simplification92.5%
(FPCore (x) :precision binary64 (+ (* 0.08333333333333333 (pow x 4.0)) (* x x)))
double code(double x) {
return (0.08333333333333333 * pow(x, 4.0)) + (x * x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.08333333333333333d0 * (x ** 4.0d0)) + (x * x)
end function
public static double code(double x) {
return (0.08333333333333333 * Math.pow(x, 4.0)) + (x * x);
}
def code(x): return (0.08333333333333333 * math.pow(x, 4.0)) + (x * x)
function code(x) return Float64(Float64(0.08333333333333333 * (x ^ 4.0)) + Float64(x * x)) end
function tmp = code(x) tmp = (0.08333333333333333 * (x ^ 4.0)) + (x * x); end
code[x_] := N[(N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.08333333333333333 \cdot {x}^{4} + x \cdot x
\end{array}
Initial program 74.1%
Taylor expanded in x around 0 88.9%
unpow288.9%
Simplified88.9%
Final simplification88.9%
(FPCore (x) :precision binary64 (if (<= x 3.5) (* x x) (* 0.08333333333333333 (pow x 4.0))))
double code(double x) {
double tmp;
if (x <= 3.5) {
tmp = x * x;
} else {
tmp = 0.08333333333333333 * pow(x, 4.0);
}
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 = 0.08333333333333333d0 * (x ** 4.0d0)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 3.5) {
tmp = x * x;
} else {
tmp = 0.08333333333333333 * Math.pow(x, 4.0);
}
return tmp;
}
def code(x): tmp = 0 if x <= 3.5: tmp = x * x else: tmp = 0.08333333333333333 * math.pow(x, 4.0) return tmp
function code(x) tmp = 0.0 if (x <= 3.5) tmp = Float64(x * x); else tmp = Float64(0.08333333333333333 * (x ^ 4.0)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 3.5) tmp = x * x; else tmp = 0.08333333333333333 * (x ^ 4.0); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 3.5], N[(x * x), $MachinePrecision], N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.5:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;0.08333333333333333 \cdot {x}^{4}\\
\end{array}
\end{array}
if x < 3.5Initial program 65.6%
Taylor expanded in x around 0 82.6%
unpow282.6%
Simplified82.6%
if 3.5 < x Initial program 100.0%
Taylor expanded in x around 0 77.5%
unpow277.5%
Simplified77.5%
Taylor expanded in x around inf 77.5%
Final simplification81.3%
(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.1%
Taylor expanded in x around 0 74.7%
unpow274.7%
Simplified74.7%
Final simplification74.7%
(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 74.1%
Applied egg-rr24.1%
Final simplification24.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 2023201
(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))))