
(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 8 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) 4e-10)
(fma x x (* 0.08333333333333333 (pow x 4.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) <= 4e-10) {
tmp = fma(x, x, (0.08333333333333333 * pow(x, 4.0)));
} else {
tmp = 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) <= 4e-10) tmp = fma(x, x, Float64(0.08333333333333333 * (x ^ 4.0))); else tmp = Float64(exp(x) + Float64(t_0 + -2.0)); end return 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], 4e-10], N[(x * x + N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Exp[x], $MachinePrecision] + N[(t$95$0 + -2.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{-x}\\
\mathbf{if}\;\left(e^{x} - 2\right) + t_0 \leq 4 \cdot 10^{-10}:\\
\;\;\;\;\mathsf{fma}\left(x, x, 0.08333333333333333 \cdot {x}^{4}\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.00000000000000015e-10Initial program 54.7%
associate-+l-54.5%
sub-neg54.5%
sub-neg54.5%
+-commutative54.5%
sub0-neg54.5%
associate--r-54.5%
neg-sub054.5%
remove-double-neg54.5%
sub-neg54.5%
metadata-eval54.5%
Simplified54.5%
Taylor expanded in x around 0 100.0%
+-commutative100.0%
unpow2100.0%
fma-def100.0%
Applied egg-rr100.0%
if 4.00000000000000015e-10 < (+.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%
sub0-neg100.0%
associate--r-100.0%
neg-sub0100.0%
remove-double-neg100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x)
:precision binary64
(let* ((t_0 (exp (- x))))
(if (<= (+ (- (exp x) 2.0) t_0) 4e-10)
(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) <= 4e-10) {
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) <= 4d-10) 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) <= 4e-10) {
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) <= 4e-10: 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) <= 4e-10) tmp = x ^ 2.0; else tmp = Float64(exp(x) + Float64(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) <= 4e-10) 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], 4e-10], N[Power[x, 2.0], $MachinePrecision], N[(N[Exp[x], $MachinePrecision] + N[(t$95$0 + -2.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{-x}\\
\mathbf{if}\;\left(e^{x} - 2\right) + t_0 \leq 4 \cdot 10^{-10}:\\
\;\;\;\;{x}^{2}\\
\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.00000000000000015e-10Initial program 54.7%
associate-+l-54.5%
sub-neg54.5%
sub-neg54.5%
+-commutative54.5%
sub0-neg54.5%
associate--r-54.5%
neg-sub054.5%
remove-double-neg54.5%
sub-neg54.5%
metadata-eval54.5%
Simplified54.5%
Taylor expanded in x around 0 99.4%
if 4.00000000000000015e-10 < (+.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%
sub0-neg100.0%
associate--r-100.0%
neg-sub0100.0%
remove-double-neg100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
Final simplification99.7%
(FPCore (x) :precision binary64 (if (<= x 3.1) (pow x 2.0) (- (* (exp x) 2.0) 2.0)))
double code(double x) {
double tmp;
if (x <= 3.1) {
tmp = pow(x, 2.0);
} else {
tmp = (exp(x) * 2.0) - 2.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 3.1d0) then
tmp = x ** 2.0d0
else
tmp = (exp(x) * 2.0d0) - 2.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 3.1) {
tmp = Math.pow(x, 2.0);
} else {
tmp = (Math.exp(x) * 2.0) - 2.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= 3.1: tmp = math.pow(x, 2.0) else: tmp = (math.exp(x) * 2.0) - 2.0 return tmp
function code(x) tmp = 0.0 if (x <= 3.1) tmp = x ^ 2.0; else tmp = Float64(Float64(exp(x) * 2.0) - 2.0); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 3.1) tmp = x ^ 2.0; else tmp = (exp(x) * 2.0) - 2.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 3.1], N[Power[x, 2.0], $MachinePrecision], N[(N[(N[Exp[x], $MachinePrecision] * 2.0), $MachinePrecision] - 2.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.1:\\
\;\;\;\;{x}^{2}\\
\mathbf{else}:\\
\;\;\;\;e^{x} \cdot 2 - 2\\
\end{array}
\end{array}
if x < 3.10000000000000009Initial program 73.4%
associate-+l-73.3%
sub-neg73.3%
sub-neg73.3%
+-commutative73.3%
sub0-neg73.3%
associate--r-73.3%
neg-sub073.3%
remove-double-neg73.3%
sub-neg73.3%
metadata-eval73.3%
Simplified73.3%
Taylor expanded in x around 0 77.2%
if 3.10000000000000009 < x Initial program 100.0%
associate-+l-100.0%
sub-neg100.0%
sub-neg100.0%
+-commutative100.0%
sub0-neg100.0%
associate--r-100.0%
neg-sub0100.0%
remove-double-neg100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
associate-+r+100.0%
+-commutative100.0%
associate-+r+100.0%
metadata-eval100.0%
sub-neg100.0%
associate-+r-100.0%
add-sqr-sqrt0.0%
sqrt-unprod100.0%
sqr-neg100.0%
sqrt-unprod100.0%
add-sqr-sqrt100.0%
count-2100.0%
*-commutative100.0%
Applied egg-rr100.0%
Final simplification83.2%
(FPCore (x) :precision binary64 (if (<= x 3.5) (pow x 2.0) (* 0.08333333333333333 (pow x 4.0))))
double code(double x) {
double tmp;
if (x <= 3.5) {
tmp = pow(x, 2.0);
} 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 ** 2.0d0
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 = Math.pow(x, 2.0);
} else {
tmp = 0.08333333333333333 * Math.pow(x, 4.0);
}
return tmp;
}
def code(x): tmp = 0 if x <= 3.5: tmp = math.pow(x, 2.0) else: tmp = 0.08333333333333333 * math.pow(x, 4.0) return tmp
function code(x) tmp = 0.0 if (x <= 3.5) tmp = x ^ 2.0; 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 ^ 2.0; else tmp = 0.08333333333333333 * (x ^ 4.0); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 3.5], N[Power[x, 2.0], $MachinePrecision], N[(0.08333333333333333 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.5:\\
\;\;\;\;{x}^{2}\\
\mathbf{else}:\\
\;\;\;\;0.08333333333333333 \cdot {x}^{4}\\
\end{array}
\end{array}
if x < 3.5Initial program 73.4%
associate-+l-73.3%
sub-neg73.3%
sub-neg73.3%
+-commutative73.3%
sub0-neg73.3%
associate--r-73.3%
neg-sub073.3%
remove-double-neg73.3%
sub-neg73.3%
metadata-eval73.3%
Simplified73.3%
Taylor expanded in x around 0 77.2%
if 3.5 < x Initial program 100.0%
associate-+l-100.0%
sub-neg100.0%
sub-neg100.0%
+-commutative100.0%
sub0-neg100.0%
associate--r-100.0%
neg-sub0100.0%
remove-double-neg100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 80.2%
Taylor expanded in x around inf 80.2%
Final simplification78.0%
(FPCore (x) :precision binary64 (pow x 2.0))
double code(double x) {
return pow(x, 2.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = x ** 2.0d0
end function
public static double code(double x) {
return Math.pow(x, 2.0);
}
def code(x): return math.pow(x, 2.0)
function code(x) return x ^ 2.0 end
function tmp = code(x) tmp = x ^ 2.0; end
code[x_] := N[Power[x, 2.0], $MachinePrecision]
\begin{array}{l}
\\
{x}^{2}
\end{array}
Initial program 80.3%
associate-+l-80.3%
sub-neg80.3%
sub-neg80.3%
+-commutative80.3%
sub0-neg80.3%
associate--r-80.3%
neg-sub080.3%
remove-double-neg80.3%
sub-neg80.3%
metadata-eval80.3%
Simplified80.3%
Taylor expanded in x around 0 70.2%
Final simplification70.2%
(FPCore (x) :precision binary64 (* x (+ x 2.0)))
double code(double x) {
return x * (x + 2.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = x * (x + 2.0d0)
end function
public static double code(double x) {
return x * (x + 2.0);
}
def code(x): return x * (x + 2.0)
function code(x) return Float64(x * Float64(x + 2.0)) end
function tmp = code(x) tmp = x * (x + 2.0); end
code[x_] := N[(x * N[(x + 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \left(x + 2\right)
\end{array}
Initial program 80.3%
associate-+l-80.3%
sub-neg80.3%
sub-neg80.3%
+-commutative80.3%
sub0-neg80.3%
associate--r-80.3%
neg-sub080.3%
remove-double-neg80.3%
sub-neg80.3%
metadata-eval80.3%
Simplified80.3%
associate-+r+80.3%
+-commutative80.3%
associate-+r+80.3%
metadata-eval80.3%
sub-neg80.3%
associate-+r-80.3%
add-sqr-sqrt45.1%
sqrt-unprod80.2%
sqr-neg80.2%
sqrt-unprod35.1%
add-sqr-sqrt49.6%
count-249.6%
*-commutative49.6%
Applied egg-rr49.6%
Taylor expanded in x around 0 29.8%
unpow229.8%
distribute-rgt-out29.8%
+-commutative29.8%
Simplified29.8%
Final simplification29.8%
(FPCore (x) :precision binary64 (/ x (/ 1.0 x)))
double code(double x) {
return x / (1.0 / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = x / (1.0d0 / x)
end function
public static double code(double x) {
return x / (1.0 / x);
}
def code(x): return x / (1.0 / x)
function code(x) return Float64(x / Float64(1.0 / x)) end
function tmp = code(x) tmp = x / (1.0 / x); end
code[x_] := N[(x / N[(1.0 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{\frac{1}{x}}
\end{array}
Initial program 80.3%
associate-+l-80.3%
sub-neg80.3%
sub-neg80.3%
+-commutative80.3%
sub0-neg80.3%
associate--r-80.3%
neg-sub080.3%
remove-double-neg80.3%
sub-neg80.3%
metadata-eval80.3%
Simplified80.3%
associate-+r+80.3%
+-commutative80.3%
associate-+r+80.3%
metadata-eval80.3%
sub-neg80.3%
associate-+r-80.3%
add-sqr-sqrt45.1%
sqrt-unprod80.2%
sqr-neg80.2%
sqrt-unprod35.1%
add-sqr-sqrt49.6%
count-249.6%
*-commutative49.6%
Applied egg-rr49.6%
Taylor expanded in x around 0 29.8%
unpow229.8%
distribute-rgt-out29.8%
+-commutative29.8%
Simplified29.8%
flip3-+16.3%
associate-*r/22.6%
associate-/l*16.3%
clear-num16.3%
flip3-+29.8%
Applied egg-rr29.8%
Taylor expanded in x around inf 70.2%
Final simplification70.2%
(FPCore (x) :precision binary64 (* x 2.0))
double code(double x) {
return x * 2.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = x * 2.0d0
end function
public static double code(double x) {
return x * 2.0;
}
def code(x): return x * 2.0
function code(x) return Float64(x * 2.0) end
function tmp = code(x) tmp = x * 2.0; end
code[x_] := N[(x * 2.0), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 2
\end{array}
Initial program 80.3%
associate-+l-80.3%
sub-neg80.3%
sub-neg80.3%
+-commutative80.3%
sub0-neg80.3%
associate--r-80.3%
neg-sub080.3%
remove-double-neg80.3%
sub-neg80.3%
metadata-eval80.3%
Simplified80.3%
associate-+r+80.3%
+-commutative80.3%
associate-+r+80.3%
metadata-eval80.3%
sub-neg80.3%
associate-+r-80.3%
add-sqr-sqrt45.1%
sqrt-unprod80.2%
sqr-neg80.2%
sqrt-unprod35.1%
add-sqr-sqrt49.6%
count-249.6%
*-commutative49.6%
Applied egg-rr49.6%
Taylor expanded in x around 0 4.5%
*-commutative4.5%
Simplified4.5%
Final simplification4.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 2023336
(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))))