
(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 (let* ((t_0 (+ (- (exp x) 2.0) (exp (- x))))) (if (<= t_0 2e-13) (+ (* 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 <= 2e-13) {
tmp = (x * x) + (0.08333333333333333 * pow(x, 4.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 <= 2d-13) then
tmp = (x * x) + (0.08333333333333333d0 * (x ** 4.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 <= 2e-13) {
tmp = (x * x) + (0.08333333333333333 * Math.pow(x, 4.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 <= 2e-13: tmp = (x * x) + (0.08333333333333333 * math.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 <= 2e-13) tmp = Float64(Float64(x * x) + Float64(0.08333333333333333 * (x ^ 4.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 <= 2e-13) tmp = (x * x) + (0.08333333333333333 * (x ^ 4.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, 2e-13], N[(N[(x * x), $MachinePrecision] + 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 2 \cdot 10^{-13}:\\
\;\;\;\;x \cdot x + 0.08333333333333333 \cdot {x}^{4}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 2.0000000000000001e-13Initial program 54.5%
Taylor expanded in x around 0 100.0%
unpow2100.0%
Simplified100.0%
if 2.0000000000000001e-13 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= x 500000.0) (+ (* x x) (* 0.08333333333333333 (pow x 4.0))) (sqrt (* (pow x 8.0) 0.006944444444444444))))
double code(double x) {
double tmp;
if (x <= 500000.0) {
tmp = (x * x) + (0.08333333333333333 * pow(x, 4.0));
} else {
tmp = sqrt((pow(x, 8.0) * 0.006944444444444444));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 500000.0d0) then
tmp = (x * x) + (0.08333333333333333d0 * (x ** 4.0d0))
else
tmp = sqrt(((x ** 8.0d0) * 0.006944444444444444d0))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 500000.0) {
tmp = (x * x) + (0.08333333333333333 * Math.pow(x, 4.0));
} else {
tmp = Math.sqrt((Math.pow(x, 8.0) * 0.006944444444444444));
}
return tmp;
}
def code(x): tmp = 0 if x <= 500000.0: tmp = (x * x) + (0.08333333333333333 * math.pow(x, 4.0)) else: tmp = math.sqrt((math.pow(x, 8.0) * 0.006944444444444444)) return tmp
function code(x) tmp = 0.0 if (x <= 500000.0) tmp = Float64(Float64(x * x) + Float64(0.08333333333333333 * (x ^ 4.0))); else tmp = sqrt(Float64((x ^ 8.0) * 0.006944444444444444)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 500000.0) tmp = (x * x) + (0.08333333333333333 * (x ^ 4.0)); else tmp = sqrt(((x ^ 8.0) * 0.006944444444444444)); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 500000.0], N[(N[(x * x), $MachinePrecision] + N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Sqrt[N[(N[Power[x, 8.0], $MachinePrecision] * 0.006944444444444444), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 500000:\\
\;\;\;\;x \cdot x + 0.08333333333333333 \cdot {x}^{4}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{{x}^{8} \cdot 0.006944444444444444}\\
\end{array}
\end{array}
if x < 5e5Initial program 72.5%
Taylor expanded in x around 0 88.6%
unpow288.6%
Simplified88.6%
if 5e5 < x Initial program 100.0%
Taylor expanded in x around 0 83.8%
unpow283.8%
Simplified83.8%
Taylor expanded in x around inf 83.8%
add-sqr-sqrt83.8%
sqrt-unprod87.0%
*-commutative87.0%
*-commutative87.0%
swap-sqr87.0%
pow-prod-up87.0%
metadata-eval87.0%
metadata-eval87.0%
Applied egg-rr87.0%
Final simplification88.2%
(FPCore (x) :precision binary64 (+ (* x x) (* 0.08333333333333333 (pow x 4.0))))
double code(double x) {
return (x * x) + (0.08333333333333333 * pow(x, 4.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x * x) + (0.08333333333333333d0 * (x ** 4.0d0))
end function
public static double code(double x) {
return (x * x) + (0.08333333333333333 * Math.pow(x, 4.0));
}
def code(x): return (x * x) + (0.08333333333333333 * math.pow(x, 4.0))
function code(x) return Float64(Float64(x * x) + Float64(0.08333333333333333 * (x ^ 4.0))) end
function tmp = code(x) tmp = (x * x) + (0.08333333333333333 * (x ^ 4.0)); end
code[x_] := N[(N[(x * x), $MachinePrecision] + N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x + 0.08333333333333333 \cdot {x}^{4}
\end{array}
Initial program 78.8%
Taylor expanded in x around 0 87.5%
unpow287.5%
Simplified87.5%
Final simplification87.5%
(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 72.1%
Taylor expanded in x around 0 82.4%
unpow282.4%
Simplified82.4%
if 3.5 < x Initial program 100.0%
Taylor expanded in x around 0 80.1%
unpow280.1%
Simplified80.1%
Taylor expanded in x around inf 80.1%
Final simplification81.8%
(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 78.8%
Taylor expanded in x around 0 76.0%
unpow276.0%
Simplified76.0%
Final simplification76.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 78.8%
Applied egg-rr26.0%
Final simplification26.0%
(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 2023257
(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))))