
(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 3 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))) 1e-15) (pow x 2.0) (expm1 x)))
x = abs(x);
double code(double x) {
double tmp;
if (((exp(x) - 2.0) + exp(-x)) <= 1e-15) {
tmp = pow(x, 2.0);
} 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)) <= 1e-15) {
tmp = Math.pow(x, 2.0);
} else {
tmp = Math.expm1(x);
}
return tmp;
}
x = abs(x) def code(x): tmp = 0 if ((math.exp(x) - 2.0) + math.exp(-x)) <= 1e-15: tmp = math.pow(x, 2.0) 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))) <= 1e-15) tmp = x ^ 2.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], 1e-15], N[Power[x, 2.0], $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 10^{-15}:\\
\;\;\;\;{x}^{2}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 1.0000000000000001e-15Initial program 47.2%
+-commutative47.2%
associate-+r-47.1%
metadata-eval47.1%
associate--l-47.1%
associate-+r-47.2%
+-commutative47.2%
associate-+r-47.2%
sub-neg47.2%
associate-+r+47.2%
associate--l+47.2%
+-commutative47.2%
associate--l+47.2%
metadata-eval47.2%
metadata-eval47.2%
Simplified47.2%
Taylor expanded in x around 0 100.0%
if 1.0000000000000001e-15 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 100.0%
+-commutative100.0%
associate-+r-100.0%
metadata-eval100.0%
associate--l-100.0%
associate-+r-100.0%
+-commutative100.0%
associate-+r-100.0%
sub-neg100.0%
associate-+r+100.0%
associate--l+100.0%
+-commutative100.0%
associate--l+100.0%
metadata-eval100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 45.1%
Taylor expanded in x around inf 45.1%
expm1-def45.1%
Simplified45.1%
Final simplification71.7%
NOTE: x should be positive before calling this function (FPCore (x) :precision binary64 (expm1 x))
x = abs(x);
double code(double x) {
return expm1(x);
}
x = Math.abs(x);
public static double code(double x) {
return Math.expm1(x);
}
x = abs(x) def code(x): return math.expm1(x)
x = abs(x) function code(x) return expm1(x) end
NOTE: x should be positive before calling this function code[x_] := N[(Exp[x] - 1), $MachinePrecision]
\begin{array}{l}
x = |x|\\
\\
\mathsf{expm1}\left(x\right)
\end{array}
Initial program 74.4%
+-commutative74.4%
associate-+r-74.4%
metadata-eval74.4%
associate--l-74.4%
associate-+r-74.4%
+-commutative74.4%
associate-+r-74.4%
sub-neg74.4%
associate-+r+74.4%
associate--l+74.4%
+-commutative74.4%
associate--l+74.4%
metadata-eval74.4%
metadata-eval74.4%
Simplified74.4%
Taylor expanded in x around 0 46.1%
Taylor expanded in x around inf 46.1%
expm1-def26.1%
Simplified26.1%
Final simplification26.1%
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%
+-commutative74.4%
associate-+r-74.4%
metadata-eval74.4%
associate--l-74.4%
associate-+r-74.4%
+-commutative74.4%
associate-+r-74.4%
sub-neg74.4%
associate-+r+74.4%
associate--l+74.4%
+-commutative74.4%
associate--l+74.4%
metadata-eval74.4%
metadata-eval74.4%
Simplified74.4%
Taylor expanded in x around 0 46.1%
Taylor expanded in x around 0 4.2%
Final simplification4.2%
(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 2023305
(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))))