
(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}
NOTE: x should be positive before calling this function
(FPCore (x)
:precision binary64
(let* ((t_0 (exp (- x))))
(if (<= (+ (- (exp x) 2.0) t_0) 5e-12)
(fma x x (* 0.08333333333333333 (* (* x x) (* x x))))
(+ (exp x) (+ t_0 -2.0)))))x = abs(x);
double code(double x) {
double t_0 = exp(-x);
double tmp;
if (((exp(x) - 2.0) + t_0) <= 5e-12) {
tmp = fma(x, x, (0.08333333333333333 * ((x * x) * (x * x))));
} else {
tmp = exp(x) + (t_0 + -2.0);
}
return tmp;
}
x = abs(x) function code(x) t_0 = exp(Float64(-x)) tmp = 0.0 if (Float64(Float64(exp(x) - 2.0) + t_0) <= 5e-12) tmp = fma(x, x, Float64(0.08333333333333333 * Float64(Float64(x * x) * Float64(x * x)))); else tmp = Float64(exp(x) + Float64(t_0 + -2.0)); end return tmp end
NOTE: x should be positive before calling this function
code[x_] := Block[{t$95$0 = N[Exp[(-x)], $MachinePrecision]}, If[LessEqual[N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + t$95$0), $MachinePrecision], 5e-12], N[(x * x + N[(0.08333333333333333 * N[(N[(x * x), $MachinePrecision] * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Exp[x], $MachinePrecision] + N[(t$95$0 + -2.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
t_0 := e^{-x}\\
\mathbf{if}\;\left(e^{x} - 2\right) + t_0 \leq 5 \cdot 10^{-12}:\\
\;\;\;\;\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot \left(\left(x \cdot x\right) \cdot \left(x \cdot x\right)\right)\right)\\
\mathbf{else}:\\
\;\;\;\;e^{x} + \left(t_0 + -2\right)\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 4.9999999999999997e-12Initial program 53.6%
associate-+l-53.6%
sub-neg53.6%
sub-neg53.6%
+-commutative53.6%
distribute-neg-in53.6%
remove-double-neg53.6%
metadata-eval53.6%
Simplified53.6%
Taylor expanded in x around 0 100.0%
unpow2100.0%
fma-def100.0%
Simplified100.0%
sqr-pow100.0%
metadata-eval100.0%
pow2100.0%
metadata-eval100.0%
pow2100.0%
Applied egg-rr100.0%
if 4.9999999999999997e-12 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 99.9%
associate-+l-99.9%
sub-neg99.9%
sub-neg99.9%
+-commutative99.9%
distribute-neg-in99.9%
remove-double-neg99.9%
metadata-eval99.9%
Simplified99.9%
Final simplification100.0%
NOTE: x should be positive before calling this function (FPCore (x) :precision binary64 (if (<= x 2.6) (fma x x (* 0.08333333333333333 (* (* x x) (* x x)))) (expm1 x)))
x = abs(x);
double code(double x) {
double tmp;
if (x <= 2.6) {
tmp = fma(x, x, (0.08333333333333333 * ((x * x) * (x * x))));
} else {
tmp = expm1(x);
}
return tmp;
}
x = abs(x) function code(x) tmp = 0.0 if (x <= 2.6) tmp = fma(x, x, Float64(0.08333333333333333 * Float64(Float64(x * x) * Float64(x * x)))); else tmp = expm1(x); end return tmp end
NOTE: x should be positive before calling this function code[x_] := If[LessEqual[x, 2.6], N[(x * x + N[(0.08333333333333333 * N[(N[(x * x), $MachinePrecision] * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.6:\\
\;\;\;\;\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot \left(\left(x \cdot x\right) \cdot \left(x \cdot x\right)\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\
\end{array}
\end{array}
if x < 2.60000000000000009Initial program 70.7%
associate-+l-70.7%
sub-neg70.7%
sub-neg70.7%
+-commutative70.7%
distribute-neg-in70.7%
remove-double-neg70.7%
metadata-eval70.7%
Simplified70.7%
Taylor expanded in x around 0 89.7%
unpow289.7%
fma-def89.7%
Simplified89.7%
sqr-pow89.7%
metadata-eval89.7%
pow289.7%
metadata-eval89.7%
pow289.7%
Applied egg-rr89.7%
if 2.60000000000000009 < x Initial program 100.0%
associate-+l-100.0%
sub-neg100.0%
sub-neg100.0%
+-commutative100.0%
distribute-neg-in100.0%
remove-double-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 98.8%
Taylor expanded in x around inf 98.8%
expm1-def98.8%
Simplified98.8%
Final simplification91.9%
NOTE: x should be positive before calling this function (FPCore (x) :precision binary64 (if (<= x 2.6) (* (* x x) (+ (* x (* x 0.08333333333333333)) 1.0)) (expm1 x)))
x = abs(x);
double code(double x) {
double tmp;
if (x <= 2.6) {
tmp = (x * x) * ((x * (x * 0.08333333333333333)) + 1.0);
} else {
tmp = expm1(x);
}
return tmp;
}
x = Math.abs(x);
public static double code(double x) {
double tmp;
if (x <= 2.6) {
tmp = (x * x) * ((x * (x * 0.08333333333333333)) + 1.0);
} else {
tmp = Math.expm1(x);
}
return tmp;
}
x = abs(x) def code(x): tmp = 0 if x <= 2.6: tmp = (x * x) * ((x * (x * 0.08333333333333333)) + 1.0) else: tmp = math.expm1(x) return tmp
x = abs(x) function code(x) tmp = 0.0 if (x <= 2.6) tmp = Float64(Float64(x * x) * Float64(Float64(x * Float64(x * 0.08333333333333333)) + 1.0)); else tmp = expm1(x); end return tmp end
NOTE: x should be positive before calling this function code[x_] := If[LessEqual[x, 2.6], N[(N[(x * x), $MachinePrecision] * N[(N[(x * N[(x * 0.08333333333333333), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.6:\\
\;\;\;\;\left(x \cdot x\right) \cdot \left(x \cdot \left(x \cdot 0.08333333333333333\right) + 1\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\
\end{array}
\end{array}
if x < 2.60000000000000009Initial program 70.7%
associate-+l-70.7%
sub-neg70.7%
sub-neg70.7%
+-commutative70.7%
distribute-neg-in70.7%
remove-double-neg70.7%
metadata-eval70.7%
Simplified70.7%
Taylor expanded in x around 0 89.7%
unpow289.7%
Simplified89.7%
sqr-pow89.7%
metadata-eval89.7%
pow289.7%
metadata-eval89.7%
pow289.7%
Applied egg-rr89.7%
associate-*r*89.7%
distribute-rgt1-in89.7%
*-commutative89.7%
Applied egg-rr89.7%
Taylor expanded in x around 0 89.7%
unpow289.7%
*-commutative89.7%
associate-*r*89.7%
*-commutative89.7%
Simplified89.7%
if 2.60000000000000009 < x Initial program 100.0%
associate-+l-100.0%
sub-neg100.0%
sub-neg100.0%
+-commutative100.0%
distribute-neg-in100.0%
remove-double-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 98.8%
Taylor expanded in x around inf 98.8%
expm1-def98.8%
Simplified98.8%
Final simplification91.9%
NOTE: x should be positive before calling this function (FPCore (x) :precision binary64 (* (* x x) (+ (* x (* x 0.08333333333333333)) 1.0)))
x = abs(x);
double code(double x) {
return (x * x) * ((x * (x * 0.08333333333333333)) + 1.0);
}
NOTE: x should be positive before calling this function
real(8) function code(x)
real(8), intent (in) :: x
code = (x * x) * ((x * (x * 0.08333333333333333d0)) + 1.0d0)
end function
x = Math.abs(x);
public static double code(double x) {
return (x * x) * ((x * (x * 0.08333333333333333)) + 1.0);
}
x = abs(x) def code(x): return (x * x) * ((x * (x * 0.08333333333333333)) + 1.0)
x = abs(x) function code(x) return Float64(Float64(x * x) * Float64(Float64(x * Float64(x * 0.08333333333333333)) + 1.0)) end
x = abs(x) function tmp = code(x) tmp = (x * x) * ((x * (x * 0.08333333333333333)) + 1.0); end
NOTE: x should be positive before calling this function code[x_] := N[(N[(x * x), $MachinePrecision] * N[(N[(x * N[(x * 0.08333333333333333), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x = |x|\\
\\
\left(x \cdot x\right) \cdot \left(x \cdot \left(x \cdot 0.08333333333333333\right) + 1\right)
\end{array}
Initial program 77.6%
associate-+l-77.7%
sub-neg77.7%
sub-neg77.7%
+-commutative77.7%
distribute-neg-in77.7%
remove-double-neg77.7%
metadata-eval77.7%
Simplified77.7%
Taylor expanded in x around 0 86.0%
unpow286.0%
Simplified86.0%
sqr-pow86.0%
metadata-eval86.0%
pow286.0%
metadata-eval86.0%
pow286.0%
Applied egg-rr86.0%
associate-*r*86.0%
distribute-rgt1-in86.0%
*-commutative86.0%
Applied egg-rr86.0%
Taylor expanded in x around 0 86.0%
unpow286.0%
*-commutative86.0%
associate-*r*86.0%
*-commutative86.0%
Simplified86.0%
Final simplification86.0%
NOTE: x should be positive before calling this function (FPCore (x) :precision binary64 (if (<= x 2.2e-103) 0.0 x))
x = abs(x);
double code(double x) {
double tmp;
if (x <= 2.2e-103) {
tmp = 0.0;
} else {
tmp = x;
}
return tmp;
}
NOTE: x should be positive before calling this function
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 2.2d-103) then
tmp = 0.0d0
else
tmp = x
end if
code = tmp
end function
x = Math.abs(x);
public static double code(double x) {
double tmp;
if (x <= 2.2e-103) {
tmp = 0.0;
} else {
tmp = x;
}
return tmp;
}
x = abs(x) def code(x): tmp = 0 if x <= 2.2e-103: tmp = 0.0 else: tmp = x return tmp
x = abs(x) function code(x) tmp = 0.0 if (x <= 2.2e-103) tmp = 0.0; else tmp = x; end return tmp end
x = abs(x) function tmp_2 = code(x) tmp = 0.0; if (x <= 2.2e-103) tmp = 0.0; else tmp = x; end tmp_2 = tmp; end
NOTE: x should be positive before calling this function code[x_] := If[LessEqual[x, 2.2e-103], 0.0, x]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.2 \cdot 10^{-103}:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if x < 2.1999999999999999e-103Initial program 79.3%
associate-+l-79.3%
sub-neg79.3%
sub-neg79.3%
+-commutative79.3%
distribute-neg-in79.3%
remove-double-neg79.3%
metadata-eval79.3%
Simplified79.3%
Applied egg-rr38.0%
if 2.1999999999999999e-103 < x Initial program 74.2%
associate-+l-74.2%
sub-neg74.2%
sub-neg74.2%
+-commutative74.2%
distribute-neg-in74.2%
remove-double-neg74.2%
metadata-eval74.2%
Simplified74.2%
Taylor expanded in x around 0 73.0%
Taylor expanded in x around 0 6.2%
Final simplification27.5%
NOTE: x should be positive before calling this function (FPCore (x) :precision binary64 (* x x))
x = abs(x);
double code(double x) {
return x * x;
}
NOTE: x should be positive before calling this function
real(8) function code(x)
real(8), intent (in) :: x
code = x * x
end function
x = Math.abs(x);
public static double code(double x) {
return x * x;
}
x = abs(x) def code(x): return x * x
x = abs(x) function code(x) return Float64(x * x) end
x = abs(x) function tmp = code(x) tmp = x * x; end
NOTE: x should be positive before calling this function code[x_] := N[(x * x), $MachinePrecision]
\begin{array}{l}
x = |x|\\
\\
x \cdot x
\end{array}
Initial program 77.6%
associate-+l-77.7%
sub-neg77.7%
sub-neg77.7%
+-commutative77.7%
distribute-neg-in77.7%
remove-double-neg77.7%
metadata-eval77.7%
Simplified77.7%
Taylor expanded in x around 0 75.8%
unpow275.8%
Simplified75.8%
Final simplification75.8%
NOTE: x should be positive before calling this function (FPCore (x) :precision binary64 0.0)
x = abs(x);
double code(double x) {
return 0.0;
}
NOTE: x should be positive before calling this function
real(8) function code(x)
real(8), intent (in) :: x
code = 0.0d0
end function
x = Math.abs(x);
public static double code(double x) {
return 0.0;
}
x = abs(x) def code(x): return 0.0
x = abs(x) function code(x) return 0.0 end
x = abs(x) function tmp = code(x) tmp = 0.0; end
NOTE: x should be positive before calling this function code[x_] := 0.0
\begin{array}{l}
x = |x|\\
\\
0
\end{array}
Initial program 77.6%
associate-+l-77.7%
sub-neg77.7%
sub-neg77.7%
+-commutative77.7%
distribute-neg-in77.7%
remove-double-neg77.7%
metadata-eval77.7%
Simplified77.7%
Applied egg-rr26.3%
Final simplification26.3%
(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 2023200
(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))))