
(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}
(FPCore (x) :precision binary64 (let* ((t_0 (exp (- x)))) (if (<= (+ (- (exp x) 2.0) t_0) 0.0) (pow x 2.0) (- (+ (exp x) t_0) 2.0))))
double code(double x) {
double t_0 = exp(-x);
double tmp;
if (((exp(x) - 2.0) + t_0) <= 0.0) {
tmp = pow(x, 2.0);
} else {
tmp = (exp(x) + t_0) - 2.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = exp(-x)
if (((exp(x) - 2.0d0) + t_0) <= 0.0d0) then
tmp = x ** 2.0d0
else
tmp = (exp(x) + t_0) - 2.0d0
end if
code = tmp
end function
public static double code(double x) {
double t_0 = Math.exp(-x);
double tmp;
if (((Math.exp(x) - 2.0) + t_0) <= 0.0) {
tmp = Math.pow(x, 2.0);
} else {
tmp = (Math.exp(x) + t_0) - 2.0;
}
return tmp;
}
def code(x): t_0 = math.exp(-x) tmp = 0 if ((math.exp(x) - 2.0) + t_0) <= 0.0: tmp = math.pow(x, 2.0) else: tmp = (math.exp(x) + t_0) - 2.0 return tmp
function code(x) t_0 = exp(Float64(-x)) tmp = 0.0 if (Float64(Float64(exp(x) - 2.0) + t_0) <= 0.0) tmp = x ^ 2.0; else tmp = Float64(Float64(exp(x) + t_0) - 2.0); end return tmp end
function tmp_2 = code(x) t_0 = exp(-x); tmp = 0.0; if (((exp(x) - 2.0) + t_0) <= 0.0) tmp = x ^ 2.0; else tmp = (exp(x) + t_0) - 2.0; end tmp_2 = tmp; end
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], 0.0], N[Power[x, 2.0], $MachinePrecision], N[(N[(N[Exp[x], $MachinePrecision] + t$95$0), $MachinePrecision] - 2.0), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{-x}\\
\mathbf{if}\;\left(e^{x} - 2\right) + t_0 \leq 0:\\
\;\;\;\;{x}^{2}\\
\mathbf{else}:\\
\;\;\;\;\left(e^{x} + t_0\right) - 2\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 0.0Initial program 48.7%
+-commutative48.7%
associate-+r-48.7%
metadata-eval48.7%
associate--l-48.7%
associate-+r-48.7%
+-commutative48.7%
associate-+r-48.7%
sub-neg48.7%
associate-+r+48.7%
associate--l+48.7%
+-commutative48.7%
associate--l+48.7%
metadata-eval48.7%
metadata-eval48.7%
Simplified48.7%
Taylor expanded in x around 0 100.0%
if 0.0 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 99.9%
+-commutative99.9%
associate-+r-100.0%
metadata-eval100.0%
associate--l-100.0%
associate-+r-99.9%
+-commutative99.9%
associate-+r-99.9%
sub-neg99.9%
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 inf 100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (let* ((t_0 (+ (- (exp x) 2.0) (exp (- x))))) (if (<= t_0 0.0) (pow x 2.0) t_0)))
double code(double x) {
double t_0 = (exp(x) - 2.0) + exp(-x);
double tmp;
if (t_0 <= 0.0) {
tmp = pow(x, 2.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 <= 0.0d0) then
tmp = x ** 2.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 <= 0.0) {
tmp = Math.pow(x, 2.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 <= 0.0: tmp = math.pow(x, 2.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 <= 0.0) tmp = x ^ 2.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 <= 0.0) tmp = x ^ 2.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, 0.0], N[Power[x, 2.0], $MachinePrecision], t$95$0]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(e^{x} - 2\right) + e^{-x}\\
\mathbf{if}\;t_0 \leq 0:\\
\;\;\;\;{x}^{2}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 0.0Initial program 48.7%
+-commutative48.7%
associate-+r-48.7%
metadata-eval48.7%
associate--l-48.7%
associate-+r-48.7%
+-commutative48.7%
associate-+r-48.7%
sub-neg48.7%
associate-+r+48.7%
associate--l+48.7%
+-commutative48.7%
associate--l+48.7%
metadata-eval48.7%
metadata-eval48.7%
Simplified48.7%
Taylor expanded in x around 0 100.0%
if 0.0 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) Initial program 99.9%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= x 0.00017) (pow x 2.0) (+ (exp x) (+ (exp (- x)) -2.0))))
double code(double x) {
double tmp;
if (x <= 0.00017) {
tmp = pow(x, 2.0);
} else {
tmp = exp(x) + (exp(-x) + -2.0);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 0.00017d0) then
tmp = x ** 2.0d0
else
tmp = exp(x) + (exp(-x) + (-2.0d0))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 0.00017) {
tmp = Math.pow(x, 2.0);
} else {
tmp = Math.exp(x) + (Math.exp(-x) + -2.0);
}
return tmp;
}
def code(x): tmp = 0 if x <= 0.00017: tmp = math.pow(x, 2.0) else: tmp = math.exp(x) + (math.exp(-x) + -2.0) return tmp
function code(x) tmp = 0.0 if (x <= 0.00017) tmp = x ^ 2.0; else tmp = Float64(exp(x) + Float64(exp(Float64(-x)) + -2.0)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 0.00017) tmp = x ^ 2.0; else tmp = exp(x) + (exp(-x) + -2.0); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 0.00017], N[Power[x, 2.0], $MachinePrecision], N[(N[Exp[x], $MachinePrecision] + N[(N[Exp[(-x)], $MachinePrecision] + -2.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 0.00017:\\
\;\;\;\;{x}^{2}\\
\mathbf{else}:\\
\;\;\;\;e^{x} + \left(e^{-x} + -2\right)\\
\end{array}
\end{array}
if x < 1.7e-4Initial program 65.0%
+-commutative65.0%
associate-+r-65.1%
metadata-eval65.1%
associate--l-65.1%
associate-+r-65.0%
+-commutative65.0%
associate-+r-65.0%
sub-neg65.0%
associate-+r+65.0%
associate--l+65.0%
+-commutative65.0%
associate--l+65.0%
metadata-eval65.0%
metadata-eval65.0%
Simplified65.0%
Taylor expanded in x around 0 81.8%
if 1.7e-4 < 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%
Final simplification86.4%
(FPCore (x) :precision binary64 (if (<= x 1.66) (pow x 2.0) (expm1 x)))
double code(double x) {
double tmp;
if (x <= 1.66) {
tmp = pow(x, 2.0);
} else {
tmp = expm1(x);
}
return tmp;
}
public static double code(double x) {
double tmp;
if (x <= 1.66) {
tmp = Math.pow(x, 2.0);
} else {
tmp = Math.expm1(x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.66: tmp = math.pow(x, 2.0) else: tmp = math.expm1(x) return tmp
function code(x) tmp = 0.0 if (x <= 1.66) tmp = x ^ 2.0; else tmp = expm1(x); end return tmp end
code[x_] := If[LessEqual[x, 1.66], N[Power[x, 2.0], $MachinePrecision], N[(Exp[x] - 1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.66:\\
\;\;\;\;{x}^{2}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\
\end{array}
\end{array}
if x < 1.65999999999999992Initial program 65.0%
+-commutative65.0%
associate-+r-65.1%
metadata-eval65.1%
associate--l-65.1%
associate-+r-65.0%
+-commutative65.0%
associate-+r-65.0%
sub-neg65.0%
associate-+r+65.0%
associate--l+65.0%
+-commutative65.0%
associate--l+65.0%
metadata-eval65.0%
metadata-eval65.0%
Simplified65.0%
Taylor expanded in x around 0 81.8%
if 1.65999999999999992 < 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 99.1%
Taylor expanded in x around inf 99.1%
expm1-def99.1%
Simplified99.1%
Final simplification86.2%
(FPCore (x) :precision binary64 (if (<= x 2.2e-103) 0.0 (expm1 x)))
double code(double x) {
double tmp;
if (x <= 2.2e-103) {
tmp = 0.0;
} else {
tmp = expm1(x);
}
return tmp;
}
public static double code(double x) {
double tmp;
if (x <= 2.2e-103) {
tmp = 0.0;
} else {
tmp = Math.expm1(x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 2.2e-103: tmp = 0.0 else: tmp = math.expm1(x) return tmp
function code(x) tmp = 0.0 if (x <= 2.2e-103) tmp = 0.0; else tmp = expm1(x); end return tmp end
code[x_] := If[LessEqual[x, 2.2e-103], 0.0, N[(Exp[x] - 1), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.2 \cdot 10^{-103}:\\
\;\;\;\;0\\
\mathbf{else}:\\
\;\;\;\;\mathsf{expm1}\left(x\right)\\
\end{array}
\end{array}
if x < 2.1999999999999999e-103Initial program 73.4%
+-commutative73.4%
associate-+r-73.4%
metadata-eval73.4%
associate--l-73.4%
associate-+r-73.4%
+-commutative73.4%
associate-+r-73.4%
sub-neg73.4%
associate-+r+73.4%
associate--l+73.4%
+-commutative73.4%
associate--l+73.4%
metadata-eval73.4%
metadata-eval73.4%
Simplified73.4%
Taylor expanded in x around 0 37.3%
Applied egg-rr37.7%
if 2.1999999999999999e-103 < x Initial program 75.0%
+-commutative75.0%
associate-+r-75.0%
metadata-eval75.0%
associate--l-75.0%
associate-+r-75.0%
+-commutative75.0%
associate-+r-75.0%
sub-neg75.0%
associate-+r+75.0%
associate--l+75.0%
+-commutative75.0%
associate--l+75.0%
metadata-eval75.0%
metadata-eval75.0%
Simplified75.0%
Taylor expanded in x around 0 74.3%
Taylor expanded in x around inf 74.3%
expm1-def75.1%
Simplified75.1%
Final simplification50.5%
(FPCore (x) :precision binary64 (if (<= x 2.2e-103) 0.0 x))
double code(double x) {
double tmp;
if (x <= 2.2e-103) {
tmp = 0.0;
} else {
tmp = x;
}
return tmp;
}
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
public static double code(double x) {
double tmp;
if (x <= 2.2e-103) {
tmp = 0.0;
} else {
tmp = x;
}
return tmp;
}
def code(x): tmp = 0 if x <= 2.2e-103: tmp = 0.0 else: tmp = x return tmp
function code(x) tmp = 0.0 if (x <= 2.2e-103) tmp = 0.0; else tmp = x; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 2.2e-103) tmp = 0.0; else tmp = x; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 2.2e-103], 0.0, x]
\begin{array}{l}
\\
\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 73.4%
+-commutative73.4%
associate-+r-73.4%
metadata-eval73.4%
associate--l-73.4%
associate-+r-73.4%
+-commutative73.4%
associate-+r-73.4%
sub-neg73.4%
associate-+r+73.4%
associate--l+73.4%
+-commutative73.4%
associate--l+73.4%
metadata-eval73.4%
metadata-eval73.4%
Simplified73.4%
Taylor expanded in x around 0 37.3%
Applied egg-rr37.7%
if 2.1999999999999999e-103 < x Initial program 75.0%
+-commutative75.0%
associate-+r-75.0%
metadata-eval75.0%
associate--l-75.0%
associate-+r-75.0%
+-commutative75.0%
associate-+r-75.0%
sub-neg75.0%
associate-+r+75.0%
associate--l+75.0%
+-commutative75.0%
associate--l+75.0%
metadata-eval75.0%
metadata-eval75.0%
Simplified75.0%
Taylor expanded in x around 0 74.3%
Taylor expanded in x around 0 5.9%
Final simplification26.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 73.9%
+-commutative73.9%
associate-+r-73.9%
metadata-eval73.9%
associate--l-73.9%
associate-+r-73.9%
+-commutative73.9%
associate-+r-73.9%
sub-neg73.9%
associate-+r+73.9%
associate--l+73.9%
+-commutative73.9%
associate--l+73.9%
metadata-eval73.9%
metadata-eval73.9%
Simplified73.9%
Taylor expanded in x around 0 50.0%
Applied egg-rr25.5%
Final simplification25.5%
(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 2023301
(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))))