
(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 8 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 39.7%
lift--.f64N/A
lift-exp.f64N/A
lower-expm1.f64100.0
Applied rewrites100.0%
(FPCore (x)
:precision binary64
(if (<= (exp x) 0.0)
(/ (exp x) (- (+ 1.0 x) 1.0))
(fma
(fma (* x x) -0.001388888888888889 0.08333333333333333)
x
(- (pow x -1.0) -0.5))))
double code(double x) {
double tmp;
if (exp(x) <= 0.0) {
tmp = exp(x) / ((1.0 + x) - 1.0);
} else {
tmp = fma(fma((x * x), -0.001388888888888889, 0.08333333333333333), x, (pow(x, -1.0) - -0.5));
}
return tmp;
}
function code(x) tmp = 0.0 if (exp(x) <= 0.0) tmp = Float64(exp(x) / Float64(Float64(1.0 + x) - 1.0)); else tmp = fma(fma(Float64(x * x), -0.001388888888888889, 0.08333333333333333), x, Float64((x ^ -1.0) - -0.5)); end return tmp end
code[x_] := If[LessEqual[N[Exp[x], $MachinePrecision], 0.0], N[(N[Exp[x], $MachinePrecision] / N[(N[(1.0 + x), $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision], N[(N[(N[(x * x), $MachinePrecision] * -0.001388888888888889 + 0.08333333333333333), $MachinePrecision] * x + N[(N[Power[x, -1.0], $MachinePrecision] - -0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{x} \leq 0:\\
\;\;\;\;\frac{e^{x}}{\left(1 + x\right) - 1}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(x \cdot x, -0.001388888888888889, 0.08333333333333333\right), x, {x}^{-1} - -0.5\right)\\
\end{array}
\end{array}
if (exp.f64 x) < 0.0Initial program 100.0%
Taylor expanded in x around 0
lower-+.f64100.0
Applied rewrites100.0%
if 0.0 < (exp.f64 x) Initial program 5.9%
lift--.f64N/A
lift-exp.f64N/A
lower-expm1.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites99.8%
Final simplification99.9%
(FPCore (x)
:precision binary64
(if (<= (exp x) 0.0)
(pow (* (* x x) (* x x)) -0.25)
(fma
(fma (* x x) -0.001388888888888889 0.08333333333333333)
x
(- (pow x -1.0) -0.5))))
double code(double x) {
double tmp;
if (exp(x) <= 0.0) {
tmp = pow(((x * x) * (x * x)), -0.25);
} else {
tmp = fma(fma((x * x), -0.001388888888888889, 0.08333333333333333), x, (pow(x, -1.0) - -0.5));
}
return tmp;
}
function code(x) tmp = 0.0 if (exp(x) <= 0.0) tmp = Float64(Float64(x * x) * Float64(x * x)) ^ -0.25; else tmp = fma(fma(Float64(x * x), -0.001388888888888889, 0.08333333333333333), x, Float64((x ^ -1.0) - -0.5)); end return tmp end
code[x_] := If[LessEqual[N[Exp[x], $MachinePrecision], 0.0], N[Power[N[(N[(x * x), $MachinePrecision] * N[(x * x), $MachinePrecision]), $MachinePrecision], -0.25], $MachinePrecision], N[(N[(N[(x * x), $MachinePrecision] * -0.001388888888888889 + 0.08333333333333333), $MachinePrecision] * x + N[(N[Power[x, -1.0], $MachinePrecision] - -0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{x} \leq 0:\\
\;\;\;\;{\left(\left(x \cdot x\right) \cdot \left(x \cdot x\right)\right)}^{-0.25}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(x \cdot x, -0.001388888888888889, 0.08333333333333333\right), x, {x}^{-1} - -0.5\right)\\
\end{array}
\end{array}
if (exp.f64 x) < 0.0Initial program 100.0%
Taylor expanded in x around 0
lower-/.f645.4
Applied rewrites5.4%
Applied rewrites5.4%
Applied rewrites74.8%
if 0.0 < (exp.f64 x) Initial program 5.9%
lift--.f64N/A
lift-exp.f64N/A
lower-expm1.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites99.8%
Final simplification90.8%
(FPCore (x)
:precision binary64
(if (<= x -6.3)
(sqrt (pow (* x x) -1.0))
(fma
(fma (* x x) -0.001388888888888889 0.08333333333333333)
x
(- (pow x -1.0) -0.5))))
double code(double x) {
double tmp;
if (x <= -6.3) {
tmp = sqrt(pow((x * x), -1.0));
} else {
tmp = fma(fma((x * x), -0.001388888888888889, 0.08333333333333333), x, (pow(x, -1.0) - -0.5));
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= -6.3) tmp = sqrt((Float64(x * x) ^ -1.0)); else tmp = fma(fma(Float64(x * x), -0.001388888888888889, 0.08333333333333333), x, Float64((x ^ -1.0) - -0.5)); end return tmp end
code[x_] := If[LessEqual[x, -6.3], N[Sqrt[N[Power[N[(x * x), $MachinePrecision], -1.0], $MachinePrecision]], $MachinePrecision], N[(N[(N[(x * x), $MachinePrecision] * -0.001388888888888889 + 0.08333333333333333), $MachinePrecision] * x + N[(N[Power[x, -1.0], $MachinePrecision] - -0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -6.3:\\
\;\;\;\;\sqrt{{\left(x \cdot x\right)}^{-1}}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(x \cdot x, -0.001388888888888889, 0.08333333333333333\right), x, {x}^{-1} - -0.5\right)\\
\end{array}
\end{array}
if x < -6.29999999999999982Initial program 100.0%
Taylor expanded in x around 0
lower-/.f645.4
Applied rewrites5.4%
Applied rewrites50.9%
Applied rewrites51.9%
if -6.29999999999999982 < x Initial program 5.9%
lift--.f64N/A
lift-exp.f64N/A
lower-expm1.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites99.8%
Final simplification82.6%
(FPCore (x) :precision binary64 (if (<= x -700.0) (sqrt (pow (* x x) -1.0)) (fma 0.08333333333333333 x (- (pow x -1.0) -0.5))))
double code(double x) {
double tmp;
if (x <= -700.0) {
tmp = sqrt(pow((x * x), -1.0));
} else {
tmp = fma(0.08333333333333333, x, (pow(x, -1.0) - -0.5));
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= -700.0) tmp = sqrt((Float64(x * x) ^ -1.0)); else tmp = fma(0.08333333333333333, x, Float64((x ^ -1.0) - -0.5)); end return tmp end
code[x_] := If[LessEqual[x, -700.0], N[Sqrt[N[Power[N[(x * x), $MachinePrecision], -1.0], $MachinePrecision]], $MachinePrecision], N[(0.08333333333333333 * x + N[(N[Power[x, -1.0], $MachinePrecision] - -0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -700:\\
\;\;\;\;\sqrt{{\left(x \cdot x\right)}^{-1}}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(0.08333333333333333, x, {x}^{-1} - -0.5\right)\\
\end{array}
\end{array}
if x < -700Initial program 100.0%
Taylor expanded in x around 0
lower-/.f645.4
Applied rewrites5.4%
Applied rewrites50.9%
Applied rewrites51.9%
if -700 < x Initial program 5.9%
Taylor expanded in x around 0
distribute-lft-inN/A
*-commutativeN/A
associate-+r+N/A
div-addN/A
*-commutativeN/A
associate-/l*N/A
*-inversesN/A
*-rgt-identityN/A
+-commutativeN/A
lower-fma.f64N/A
fp-cancel-sign-sub-invN/A
div-subN/A
associate-/l*N/A
*-inversesN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
lower--.f64N/A
lower-/.f64N/A
metadata-eval99.7
Applied rewrites99.7%
Final simplification82.5%
(FPCore (x) :precision binary64 (pow x -1.0))
double code(double x) {
return pow(x, -1.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = x ** (-1.0d0)
end function
public static double code(double x) {
return Math.pow(x, -1.0);
}
def code(x): return math.pow(x, -1.0)
function code(x) return x ^ -1.0 end
function tmp = code(x) tmp = x ^ -1.0; end
code[x_] := N[Power[x, -1.0], $MachinePrecision]
\begin{array}{l}
\\
{x}^{-1}
\end{array}
Initial program 39.7%
Taylor expanded in x around 0
lower-/.f6464.7
Applied rewrites64.7%
Final simplification64.7%
(FPCore (x) :precision binary64 (* 0.08333333333333333 x))
double code(double x) {
return 0.08333333333333333 * x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.08333333333333333d0 * x
end function
public static double code(double x) {
return 0.08333333333333333 * x;
}
def code(x): return 0.08333333333333333 * x
function code(x) return Float64(0.08333333333333333 * x) end
function tmp = code(x) tmp = 0.08333333333333333 * x; end
code[x_] := N[(0.08333333333333333 * x), $MachinePrecision]
\begin{array}{l}
\\
0.08333333333333333 \cdot x
\end{array}
Initial program 39.7%
Taylor expanded in x around 0
distribute-lft-inN/A
*-commutativeN/A
associate-+r+N/A
div-addN/A
*-commutativeN/A
associate-/l*N/A
*-inversesN/A
*-rgt-identityN/A
+-commutativeN/A
lower-fma.f64N/A
fp-cancel-sign-sub-invN/A
div-subN/A
associate-/l*N/A
*-inversesN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
lower--.f64N/A
lower-/.f64N/A
metadata-eval64.7
Applied rewrites64.7%
Taylor expanded in x around inf
Applied rewrites3.3%
(FPCore (x) :precision binary64 0.5)
double code(double x) {
return 0.5;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0
end function
public static double code(double x) {
return 0.5;
}
def code(x): return 0.5
function code(x) return 0.5 end
function tmp = code(x) tmp = 0.5; end
code[x_] := 0.5
\begin{array}{l}
\\
0.5
\end{array}
Initial program 39.7%
Taylor expanded in x around 0
fp-cancel-sign-sub-invN/A
div-subN/A
associate-/l*N/A
*-inversesN/A
metadata-evalN/A
metadata-evalN/A
metadata-evalN/A
lower--.f64N/A
lower-/.f64N/A
metadata-eval64.6
Applied rewrites64.6%
Taylor expanded in x around inf
Applied rewrites3.2%
(FPCore (x) :precision binary64 (/ (- 1.0) (expm1 (- x))))
double code(double x) {
return -1.0 / expm1(-x);
}
public static double code(double x) {
return -1.0 / Math.expm1(-x);
}
def code(x): return -1.0 / math.expm1(-x)
function code(x) return Float64(Float64(-1.0) / expm1(Float64(-x))) end
code[x_] := N[((-1.0) / N[(Exp[(-x)] - 1), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1}{\mathsf{expm1}\left(-x\right)}
\end{array}
herbie shell --seed 2024326
(FPCore (x)
:name "expq2 (section 3.11)"
:precision binary64
:pre (> 710.0 x)
:alt
(! :herbie-platform default (/ (- 1) (expm1 (- x))))
(/ (exp x) (- (exp x) 1.0)))