
(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 5 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 (if (<= (+ (- (exp x) 2.0) (exp (- x))) 2e-8) (fma x x (* 0.08333333333333333 (pow x 4.0))) (expm1 x)))
x = abs(x);
double code(double x) {
double tmp;
if (((exp(x) - 2.0) + exp(-x)) <= 2e-8) {
tmp = fma(x, x, (0.08333333333333333 * pow(x, 4.0)));
} else {
tmp = expm1(x);
}
return tmp;
}
x = abs(x) function code(x) tmp = 0.0 if (Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) <= 2e-8) tmp = fma(x, x, Float64(0.08333333333333333 * (x ^ 4.0))); else tmp = expm1(x); end return tmp end
NOTE: x should be positive before calling this function code[x_] := If[LessEqual[N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision], 2e-8], N[(x * x + N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 2 \cdot 10^{-8}:\\
\;\;\;\;\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 2e-8Initial program 51.5%
associate-+l-51.5%
sub-neg51.5%
sub-neg51.5%
+-commutative51.5%
distribute-neg-in51.5%
remove-double-neg51.5%
metadata-eval51.5%
Simplified51.5%
Taylor expanded in x around 0 100.0%
+-commutative100.0%
unpow2100.0%
fma-def100.0%
Simplified100.0%
if 2e-8 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 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 45.8%
Taylor expanded in x around inf 45.8%
expm1-def45.8%
Simplified45.8%
Final simplification74.4%
NOTE: x should be positive before calling this function (FPCore (x) :precision binary64 (if (<= (+ (- (exp x) 2.0) (exp (- x))) 2e-8) (* x x) (expm1 x)))
x = abs(x);
double code(double x) {
double tmp;
if (((exp(x) - 2.0) + exp(-x)) <= 2e-8) {
tmp = x * x;
} else {
tmp = expm1(x);
}
return tmp;
}
x = Math.abs(x);
public static double code(double x) {
double tmp;
if (((Math.exp(x) - 2.0) + Math.exp(-x)) <= 2e-8) {
tmp = x * x;
} else {
tmp = Math.expm1(x);
}
return tmp;
}
x = abs(x) def code(x): tmp = 0 if ((math.exp(x) - 2.0) + math.exp(-x)) <= 2e-8: tmp = x * x else: tmp = math.expm1(x) return tmp
x = abs(x) function code(x) tmp = 0.0 if (Float64(Float64(exp(x) - 2.0) + exp(Float64(-x))) <= 2e-8) tmp = Float64(x * x); else tmp = expm1(x); end return tmp end
NOTE: x should be positive before calling this function code[x_] := If[LessEqual[N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision], 2e-8], N[(x * x), $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 2 \cdot 10^{-8}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 2e-8Initial program 51.5%
associate-+l-51.5%
sub-neg51.5%
sub-neg51.5%
+-commutative51.5%
distribute-neg-in51.5%
remove-double-neg51.5%
metadata-eval51.5%
Simplified51.5%
Taylor expanded in x around 0 99.7%
unpow299.7%
Simplified99.7%
if 2e-8 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 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 45.8%
Taylor expanded in x around inf 45.8%
expm1-def45.8%
Simplified45.8%
Final simplification74.2%
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 74.4%
associate-+l-74.4%
sub-neg74.4%
sub-neg74.4%
+-commutative74.4%
distribute-neg-in74.4%
remove-double-neg74.4%
metadata-eval74.4%
Simplified74.4%
Taylor expanded in x around 0 78.7%
unpow278.7%
Simplified78.7%
Final simplification78.7%
NOTE: x should be positive before calling this function (FPCore (x) :precision binary64 2.0)
x = abs(x);
double code(double x) {
return 2.0;
}
NOTE: x should be positive before calling this function
real(8) function code(x)
real(8), intent (in) :: x
code = 2.0d0
end function
x = Math.abs(x);
public static double code(double x) {
return 2.0;
}
x = abs(x) def code(x): return 2.0
x = abs(x) function code(x) return 2.0 end
x = abs(x) function tmp = code(x) tmp = 2.0; end
NOTE: x should be positive before calling this function code[x_] := 2.0
\begin{array}{l}
x = |x|\\
\\
2
\end{array}
Initial program 74.4%
associate-+l-74.4%
sub-neg74.4%
sub-neg74.4%
+-commutative74.4%
distribute-neg-in74.4%
remove-double-neg74.4%
metadata-eval74.4%
Simplified74.4%
+-commutative74.4%
metadata-eval74.4%
sub-neg74.4%
associate--r-74.4%
add-sqr-sqrt39.8%
sqrt-unprod74.3%
sqr-neg74.3%
sqrt-unprod34.4%
add-sqr-sqrt48.7%
Applied egg-rr48.7%
associate--r-48.7%
sub-neg48.7%
metadata-eval48.7%
+-commutative48.7%
rem-square-sqrt21.5%
fabs-sqr21.5%
rem-square-sqrt24.5%
metadata-eval24.5%
sub-neg24.5%
fabs-sub24.5%
rem-square-sqrt3.0%
fabs-sqr3.0%
rem-square-sqrt3.0%
remove-double-neg3.0%
remove-double-neg3.0%
distribute-neg-out3.0%
+-commutative3.0%
neg-sub03.0%
associate--r-3.0%
metadata-eval3.0%
Simplified3.7%
Final simplification3.7%
NOTE: x should be positive before calling this function (FPCore (x) :precision binary64 x)
x = abs(x);
double code(double x) {
return x;
}
NOTE: x should be positive before calling this function
real(8) function code(x)
real(8), intent (in) :: x
code = x
end function
x = Math.abs(x);
public static double code(double x) {
return x;
}
x = abs(x) def code(x): return x
x = abs(x) function code(x) return x end
x = abs(x) function tmp = code(x) tmp = x; end
NOTE: x should be positive before calling this function code[x_] := x
\begin{array}{l}
x = |x|\\
\\
x
\end{array}
Initial program 74.4%
associate-+l-74.4%
sub-neg74.4%
sub-neg74.4%
+-commutative74.4%
distribute-neg-in74.4%
remove-double-neg74.4%
metadata-eval74.4%
Simplified74.4%
Taylor expanded in x around 0 48.7%
Taylor expanded in x around 0 4.4%
Final simplification4.4%
(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 2023271
(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))))