
(FPCore (x) :precision binary64 (/ (exp x) (- (exp x) 1.0)))
double code(double x) {
return exp(x) / (exp(x) - 1.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = exp(x) / (exp(x) - 1.0d0)
end function
public static double code(double x) {
return Math.exp(x) / (Math.exp(x) - 1.0);
}
def code(x): return math.exp(x) / (math.exp(x) - 1.0)
function code(x) return Float64(exp(x) / Float64(exp(x) - 1.0)) end
function tmp = code(x) tmp = exp(x) / (exp(x) - 1.0); end
code[x_] := N[(N[Exp[x], $MachinePrecision] / N[(N[Exp[x], $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{e^{x}}{e^{x} - 1}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 10 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (/ (exp x) (- (exp x) 1.0)))
double code(double x) {
return exp(x) / (exp(x) - 1.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = exp(x) / (exp(x) - 1.0d0)
end function
public static double code(double x) {
return Math.exp(x) / (Math.exp(x) - 1.0);
}
def code(x): return math.exp(x) / (math.exp(x) - 1.0)
function code(x) return Float64(exp(x) / Float64(exp(x) - 1.0)) end
function tmp = code(x) tmp = exp(x) / (exp(x) - 1.0); end
code[x_] := N[(N[Exp[x], $MachinePrecision] / N[(N[Exp[x], $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{e^{x}}{e^{x} - 1}
\end{array}
(FPCore (x) :precision binary64 (/ (exp x) (expm1 x)))
double code(double x) {
return exp(x) / expm1(x);
}
public static double code(double x) {
return Math.exp(x) / Math.expm1(x);
}
def code(x): return math.exp(x) / math.expm1(x)
function code(x) return Float64(exp(x) / expm1(x)) end
code[x_] := N[(N[Exp[x], $MachinePrecision] / N[(Exp[x] - 1), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{e^{x}}{\mathsf{expm1}\left(x\right)}
\end{array}
Initial program 40.7%
expm1-def100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x)
:precision binary64
(let* ((t_0 (+ (/ 1.0 x) 0.5)))
(if (<= x -3.8)
(/
(- (* t_0 t_0) (* x (* x 0.006944444444444444)))
(+
(/ 1.0 x)
(- 0.5 (cast (! :precision binary32 (* x 0.08333333333333333))))))
(+
0.5
(+
(* -0.001388888888888889 (pow x 3.0))
(+ (/ 1.0 x) (* x 0.08333333333333333)))))))
double code(double x) {
double t_0 = (1.0 / x) + 0.5;
double tmp_1;
if (x <= -3.8) {
float tmp_2 = x * 0.08333333333333333f;
tmp_1 = ((t_0 * t_0) - (x * (x * 0.006944444444444444))) / ((1.0 / x) + (0.5 - ((double) ((double) tmp_2))));
} else {
tmp_1 = 0.5 + ((-0.001388888888888889 * pow(x, 3.0)) + ((1.0 / x) + (x * 0.08333333333333333)));
}
return tmp_1;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
real(4) :: tmp
real(8) :: tmp_1
real(4) :: tmp_2
t_0 = (1.0d0 / x) + 0.5d0
if (x <= (-3.8d0)) then
tmp_2 = x * 0.08333333333333333e0
tmp_1 = ((t_0 * t_0) - (x * (x * 0.006944444444444444d0))) / ((1.0d0 / x) + (0.5d0 - real(real(tmp_2, 8), 8)))
else
tmp_1 = 0.5d0 + (((-0.001388888888888889d0) * (x ** 3.0d0)) + ((1.0d0 / x) + (x * 0.08333333333333333d0)))
end if
code = tmp_1
end function
function code(x) t_0 = Float64(Float64(1.0 / x) + 0.5) tmp_1 = 0.0 if (x <= -3.8) tmp_2 = Float32(x * Float32(0.08333333333333333)) tmp_1 = Float64(Float64(Float64(t_0 * t_0) - Float64(x * Float64(x * 0.006944444444444444))) / Float64(Float64(1.0 / x) + Float64(0.5 - Float64(Float64(tmp_2))))); else tmp_1 = Float64(0.5 + Float64(Float64(-0.001388888888888889 * (x ^ 3.0)) + Float64(Float64(1.0 / x) + Float64(x * 0.08333333333333333)))); end return tmp_1 end
function tmp_4 = code(x) t_0 = (1.0 / x) + 0.5; tmp_2 = 0.0; if (x <= -3.8) tmp_3 = single((x * double(single(0.08333333333333333)))); tmp_2 = ((t_0 * t_0) - (x * (x * 0.006944444444444444))) / ((1.0 / x) + (0.5 - double(double(tmp_3)))); else tmp_2 = 0.5 + ((-0.001388888888888889 * (x ^ 3.0)) + ((1.0 / x) + (x * 0.08333333333333333))); end tmp_4 = tmp_2; end
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{1}{x} + 0.5\\
\mathbf{if}\;x \leq -3.8:\\
\;\;\;\;\frac{t_0 \cdot t_0 - x \cdot \left(x \cdot 0.006944444444444444\right)}{\frac{1}{x} + \left(0.5 - \langle \left( x \cdot 0.08333333333333333 \right)_{\text{binary32}} \rangle_{\text{binary64}}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 + \left(-0.001388888888888889 \cdot {x}^{3} + \left(\frac{1}{x} + x \cdot 0.08333333333333333\right)\right)\\
\end{array}
\end{array}
if x < -3.7999999999999998Initial program 100.0%
expm1-def100.0%
Simplified100.0%
Taylor expanded in x around 0 2.5%
associate-+r+2.5%
+-commutative2.5%
+-commutative2.5%
*-commutative2.5%
fma-def2.5%
Simplified2.5%
+-commutative2.5%
fma-udef2.5%
*-commutative2.5%
associate-+l+2.5%
+-commutative2.5%
flip-+2.3%
+-commutative2.3%
+-commutative2.3%
swap-sqr2.3%
*-commutative2.3%
associate-*l*2.3%
metadata-eval2.3%
+-commutative2.3%
*-commutative2.3%
Applied egg-rr2.3%
cancel-sign-sub-inv2.3%
associate-+l+2.3%
cancel-sign-sub-inv2.3%
Simplified2.3%
rewrite-binary64/binary3242.2%
Applied rewrite-once42.2%
if -3.7999999999999998 < x Initial program 7.9%
expm1-def100.0%
Simplified100.0%
Taylor expanded in x around 0 99.5%
Final simplification79.2%
(FPCore (x)
:precision binary64
(if (<= x -3.8)
(/
(* x (* x -0.006944444444444444))
(- 0.5 (cast (! :precision binary32 (* x 0.08333333333333333)))))
(+
0.5
(+
(* -0.001388888888888889 (pow x 3.0))
(+ (/ 1.0 x) (* x 0.08333333333333333))))))
double code(double x) {
double tmp_1;
if (x <= -3.8) {
float tmp_2 = x * 0.08333333333333333f;
tmp_1 = (x * (x * -0.006944444444444444)) / (0.5 - ((double) ((double) tmp_2)));
} else {
tmp_1 = 0.5 + ((-0.001388888888888889 * pow(x, 3.0)) + ((1.0 / x) + (x * 0.08333333333333333)));
}
return tmp_1;
}
real(8) function code(x)
real(8), intent (in) :: x
real(4) :: tmp
real(8) :: tmp_1
real(4) :: tmp_2
if (x <= (-3.8d0)) then
tmp_2 = x * 0.08333333333333333e0
tmp_1 = (x * (x * (-0.006944444444444444d0))) / (0.5d0 - real(real(tmp_2, 8), 8))
else
tmp_1 = 0.5d0 + (((-0.001388888888888889d0) * (x ** 3.0d0)) + ((1.0d0 / x) + (x * 0.08333333333333333d0)))
end if
code = tmp_1
end function
function code(x) tmp_1 = 0.0 if (x <= -3.8) tmp_2 = Float32(x * Float32(0.08333333333333333)) tmp_1 = Float64(Float64(x * Float64(x * -0.006944444444444444)) / Float64(0.5 - Float64(Float64(tmp_2)))); else tmp_1 = Float64(0.5 + Float64(Float64(-0.001388888888888889 * (x ^ 3.0)) + Float64(Float64(1.0 / x) + Float64(x * 0.08333333333333333)))); end return tmp_1 end
function tmp_4 = code(x) tmp_2 = 0.0; if (x <= -3.8) tmp_3 = single((x * double(single(0.08333333333333333)))); tmp_2 = (x * (x * -0.006944444444444444)) / (0.5 - double(double(tmp_3))); else tmp_2 = 0.5 + ((-0.001388888888888889 * (x ^ 3.0)) + ((1.0 / x) + (x * 0.08333333333333333))); end tmp_4 = tmp_2; end
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.8:\\
\;\;\;\;\frac{x \cdot \left(x \cdot -0.006944444444444444\right)}{0.5 - \langle \left( x \cdot 0.08333333333333333 \right)_{\text{binary32}} \rangle_{\text{binary64}}}\\
\mathbf{else}:\\
\;\;\;\;0.5 + \left(-0.001388888888888889 \cdot {x}^{3} + \left(\frac{1}{x} + x \cdot 0.08333333333333333\right)\right)\\
\end{array}
\end{array}
if x < -3.7999999999999998Initial program 100.0%
expm1-def100.0%
Simplified100.0%
Taylor expanded in x around 0 2.5%
associate-+r+2.5%
+-commutative2.5%
+-commutative2.5%
*-commutative2.5%
fma-def2.5%
Simplified2.5%
+-commutative2.5%
fma-udef2.5%
*-commutative2.5%
associate-+l+2.5%
+-commutative2.5%
flip-+2.3%
+-commutative2.3%
+-commutative2.3%
swap-sqr2.3%
*-commutative2.3%
associate-*l*2.3%
metadata-eval2.3%
+-commutative2.3%
*-commutative2.3%
Applied egg-rr2.3%
cancel-sign-sub-inv2.3%
associate-+l+2.3%
cancel-sign-sub-inv2.3%
Simplified2.3%
rewrite-binary64/binary3242.2%
Applied rewrite-once42.2%
Taylor expanded in x around inf 42.2%
associate-*r/42.2%
unpow242.2%
*-commutative42.2%
associate-*l*42.2%
Simplified42.2%
if -3.7999999999999998 < x Initial program 7.9%
expm1-def100.0%
Simplified100.0%
Taylor expanded in x around 0 99.5%
Final simplification79.2%
(FPCore (x)
:precision binary64
(let* ((t_0 (+ (/ 1.0 x) 0.5)))
(if (<= x -3.8)
(/
(- (* t_0 t_0) (* x (* x 0.006944444444444444)))
(+
(/ 1.0 x)
(-
0.5
(cbrt
(*
(* x 0.08333333333333333)
(* (* x 0.08333333333333333) (* x 0.08333333333333333)))))))
(+
0.5
(+
(* -0.001388888888888889 (pow x 3.0))
(+ (/ 1.0 x) (* x 0.08333333333333333)))))))
double code(double x) {
double t_0 = (1.0 / x) + 0.5;
double tmp;
if (x <= -3.8) {
tmp = ((t_0 * t_0) - (x * (x * 0.006944444444444444))) / ((1.0 / x) + (0.5 - cbrt(((x * 0.08333333333333333) * ((x * 0.08333333333333333) * (x * 0.08333333333333333))))));
} else {
tmp = 0.5 + ((-0.001388888888888889 * pow(x, 3.0)) + ((1.0 / x) + (x * 0.08333333333333333)));
}
return tmp;
}
public static double code(double x) {
double t_0 = (1.0 / x) + 0.5;
double tmp;
if (x <= -3.8) {
tmp = ((t_0 * t_0) - (x * (x * 0.006944444444444444))) / ((1.0 / x) + (0.5 - Math.cbrt(((x * 0.08333333333333333) * ((x * 0.08333333333333333) * (x * 0.08333333333333333))))));
} else {
tmp = 0.5 + ((-0.001388888888888889 * Math.pow(x, 3.0)) + ((1.0 / x) + (x * 0.08333333333333333)));
}
return tmp;
}
function code(x) t_0 = Float64(Float64(1.0 / x) + 0.5) tmp = 0.0 if (x <= -3.8) tmp = Float64(Float64(Float64(t_0 * t_0) - Float64(x * Float64(x * 0.006944444444444444))) / Float64(Float64(1.0 / x) + Float64(0.5 - cbrt(Float64(Float64(x * 0.08333333333333333) * Float64(Float64(x * 0.08333333333333333) * Float64(x * 0.08333333333333333))))))); else tmp = Float64(0.5 + Float64(Float64(-0.001388888888888889 * (x ^ 3.0)) + Float64(Float64(1.0 / x) + Float64(x * 0.08333333333333333)))); end return tmp end
code[x_] := Block[{t$95$0 = N[(N[(1.0 / x), $MachinePrecision] + 0.5), $MachinePrecision]}, If[LessEqual[x, -3.8], N[(N[(N[(t$95$0 * t$95$0), $MachinePrecision] - N[(x * N[(x * 0.006944444444444444), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[(1.0 / x), $MachinePrecision] + N[(0.5 - N[Power[N[(N[(x * 0.08333333333333333), $MachinePrecision] * N[(N[(x * 0.08333333333333333), $MachinePrecision] * N[(x * 0.08333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 + N[(N[(-0.001388888888888889 * N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision] + N[(N[(1.0 / x), $MachinePrecision] + N[(x * 0.08333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{1}{x} + 0.5\\
\mathbf{if}\;x \leq -3.8:\\
\;\;\;\;\frac{t_0 \cdot t_0 - x \cdot \left(x \cdot 0.006944444444444444\right)}{\frac{1}{x} + \left(0.5 - \sqrt[3]{\left(x \cdot 0.08333333333333333\right) \cdot \left(\left(x \cdot 0.08333333333333333\right) \cdot \left(x \cdot 0.08333333333333333\right)\right)}\right)}\\
\mathbf{else}:\\
\;\;\;\;0.5 + \left(-0.001388888888888889 \cdot {x}^{3} + \left(\frac{1}{x} + x \cdot 0.08333333333333333\right)\right)\\
\end{array}
\end{array}
if x < -3.7999999999999998Initial program 100.0%
expm1-def100.0%
Simplified100.0%
Taylor expanded in x around 0 2.5%
associate-+r+2.5%
+-commutative2.5%
+-commutative2.5%
*-commutative2.5%
fma-def2.5%
Simplified2.5%
+-commutative2.5%
fma-udef2.5%
*-commutative2.5%
associate-+l+2.5%
+-commutative2.5%
flip-+2.3%
+-commutative2.3%
+-commutative2.3%
swap-sqr2.3%
*-commutative2.3%
associate-*l*2.3%
metadata-eval2.3%
+-commutative2.3%
*-commutative2.3%
Applied egg-rr2.3%
cancel-sign-sub-inv2.3%
associate-+l+2.3%
cancel-sign-sub-inv2.3%
Simplified2.3%
add-cbrt-cube_binary6418.7%
Applied rewrite-once18.7%
if -3.7999999999999998 < x Initial program 7.9%
expm1-def100.0%
Simplified100.0%
Taylor expanded in x around 0 99.5%
Final simplification70.8%
(FPCore (x) :precision binary64 (+ 0.5 (+ (* -0.001388888888888889 (pow x 3.0)) (+ (/ 1.0 x) (* x 0.08333333333333333)))))
double code(double x) {
return 0.5 + ((-0.001388888888888889 * pow(x, 3.0)) + ((1.0 / x) + (x * 0.08333333333333333)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0 + (((-0.001388888888888889d0) * (x ** 3.0d0)) + ((1.0d0 / x) + (x * 0.08333333333333333d0)))
end function
public static double code(double x) {
return 0.5 + ((-0.001388888888888889 * Math.pow(x, 3.0)) + ((1.0 / x) + (x * 0.08333333333333333)));
}
def code(x): return 0.5 + ((-0.001388888888888889 * math.pow(x, 3.0)) + ((1.0 / x) + (x * 0.08333333333333333)))
function code(x) return Float64(0.5 + Float64(Float64(-0.001388888888888889 * (x ^ 3.0)) + Float64(Float64(1.0 / x) + Float64(x * 0.08333333333333333)))) end
function tmp = code(x) tmp = 0.5 + ((-0.001388888888888889 * (x ^ 3.0)) + ((1.0 / x) + (x * 0.08333333333333333))); end
code[x_] := N[(0.5 + N[(N[(-0.001388888888888889 * N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision] + N[(N[(1.0 / x), $MachinePrecision] + N[(x * 0.08333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 + \left(-0.001388888888888889 \cdot {x}^{3} + \left(\frac{1}{x} + x \cdot 0.08333333333333333\right)\right)
\end{array}
Initial program 40.7%
expm1-def100.0%
Simplified100.0%
Taylor expanded in x around 0 64.8%
Final simplification64.8%
(FPCore (x) :precision binary64 (+ 0.5 (+ (/ 1.0 x) (* x 0.08333333333333333))))
double code(double x) {
return 0.5 + ((1.0 / x) + (x * 0.08333333333333333));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0 + ((1.0d0 / x) + (x * 0.08333333333333333d0))
end function
public static double code(double x) {
return 0.5 + ((1.0 / x) + (x * 0.08333333333333333));
}
def code(x): return 0.5 + ((1.0 / x) + (x * 0.08333333333333333))
function code(x) return Float64(0.5 + Float64(Float64(1.0 / x) + Float64(x * 0.08333333333333333))) end
function tmp = code(x) tmp = 0.5 + ((1.0 / x) + (x * 0.08333333333333333)); end
code[x_] := N[(0.5 + N[(N[(1.0 / x), $MachinePrecision] + N[(x * 0.08333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 + \left(\frac{1}{x} + x \cdot 0.08333333333333333\right)
\end{array}
Initial program 40.7%
expm1-def100.0%
Simplified100.0%
Taylor expanded in x around 0 64.7%
Final simplification64.7%
(FPCore (x) :precision binary64 (/ 1.0 x))
double code(double x) {
return 1.0 / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / x
end function
public static double code(double x) {
return 1.0 / x;
}
def code(x): return 1.0 / x
function code(x) return Float64(1.0 / x) end
function tmp = code(x) tmp = 1.0 / x; end
code[x_] := N[(1.0 / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x}
\end{array}
Initial program 40.7%
expm1-def100.0%
Simplified100.0%
Taylor expanded in x around 0 64.2%
Final simplification64.2%
(FPCore (x) :precision binary64 -2.0)
double code(double x) {
return -2.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = -2.0d0
end function
public static double code(double x) {
return -2.0;
}
def code(x): return -2.0
function code(x) return -2.0 end
function tmp = code(x) tmp = -2.0; end
code[x_] := -2.0
\begin{array}{l}
\\
-2
\end{array}
Initial program 40.7%
expm1-def100.0%
Simplified100.0%
Applied egg-rr3.1%
Final simplification3.1%
(FPCore (x) :precision binary64 -1.0)
double code(double x) {
return -1.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = -1.0d0
end function
public static double code(double x) {
return -1.0;
}
def code(x): return -1.0
function code(x) return -1.0 end
function tmp = code(x) tmp = -1.0; end
code[x_] := -1.0
\begin{array}{l}
\\
-1
\end{array}
Initial program 40.7%
expm1-def100.0%
Simplified100.0%
Applied egg-rr3.1%
Final simplification3.1%
(FPCore (x) :precision binary64 1.0)
double code(double x) {
return 1.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0
end function
public static double code(double x) {
return 1.0;
}
def code(x): return 1.0
function code(x) return 1.0 end
function tmp = code(x) tmp = 1.0; end
code[x_] := 1.0
\begin{array}{l}
\\
1
\end{array}
Initial program 40.7%
expm1-def100.0%
Simplified100.0%
Applied egg-rr3.4%
Final simplification3.4%
(FPCore (x) :precision binary64 (/ 1.0 (- 1.0 (exp (- x)))))
double code(double x) {
return 1.0 / (1.0 - exp(-x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / (1.0d0 - exp(-x))
end function
public static double code(double x) {
return 1.0 / (1.0 - Math.exp(-x));
}
def code(x): return 1.0 / (1.0 - math.exp(-x))
function code(x) return Float64(1.0 / Float64(1.0 - exp(Float64(-x)))) end
function tmp = code(x) tmp = 1.0 / (1.0 - exp(-x)); end
code[x_] := N[(1.0 / N[(1.0 - N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{1 - e^{-x}}
\end{array}
herbie shell --seed 2023297
(FPCore (x)
:name "expq2 (section 3.11)"
:precision binary64
:herbie-target
(/ 1.0 (- 1.0 (exp (- x))))
(/ (exp x) (- (exp x) 1.0)))