
(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 6 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 (/ -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(-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}
Initial program 38.1%
sub-neg38.1%
+-commutative38.1%
rgt-mult-inverse5.3%
exp-neg5.3%
distribute-rgt-neg-out5.3%
*-rgt-identity5.3%
distribute-lft-in5.3%
neg-sub05.3%
associate-+l-5.3%
neg-sub05.4%
associate-/r*5.4%
*-rgt-identity5.4%
associate-*r/5.4%
rgt-mult-inverse38.2%
distribute-frac-neg238.2%
distribute-neg-frac38.2%
metadata-eval38.2%
expm1-define100.0%
Simplified100.0%
(FPCore (x) :precision binary64 (if (<= x -3.95) (/ (exp x) x) (/ (+ 1.0 (* x (+ 0.5 (* x 0.08333333333333333)))) x)))
double code(double x) {
double tmp;
if (x <= -3.95) {
tmp = exp(x) / x;
} else {
tmp = (1.0 + (x * (0.5 + (x * 0.08333333333333333)))) / x;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-3.95d0)) then
tmp = exp(x) / x
else
tmp = (1.0d0 + (x * (0.5d0 + (x * 0.08333333333333333d0)))) / x
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= -3.95) {
tmp = Math.exp(x) / x;
} else {
tmp = (1.0 + (x * (0.5 + (x * 0.08333333333333333)))) / x;
}
return tmp;
}
def code(x): tmp = 0 if x <= -3.95: tmp = math.exp(x) / x else: tmp = (1.0 + (x * (0.5 + (x * 0.08333333333333333)))) / x return tmp
function code(x) tmp = 0.0 if (x <= -3.95) tmp = Float64(exp(x) / x); else tmp = Float64(Float64(1.0 + Float64(x * Float64(0.5 + Float64(x * 0.08333333333333333)))) / x); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= -3.95) tmp = exp(x) / x; else tmp = (1.0 + (x * (0.5 + (x * 0.08333333333333333)))) / x; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, -3.95], N[(N[Exp[x], $MachinePrecision] / x), $MachinePrecision], N[(N[(1.0 + N[(x * N[(0.5 + N[(x * 0.08333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.95:\\
\;\;\;\;\frac{e^{x}}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{1 + x \cdot \left(0.5 + x \cdot 0.08333333333333333\right)}{x}\\
\end{array}
\end{array}
if x < -3.9500000000000002Initial program 100.0%
expm1-define100.0%
Simplified100.0%
Taylor expanded in x around 0 100.0%
if -3.9500000000000002 < x Initial program 7.9%
sub-neg7.9%
+-commutative7.9%
rgt-mult-inverse7.8%
exp-neg7.9%
distribute-rgt-neg-out7.9%
*-rgt-identity7.9%
distribute-lft-in7.9%
neg-sub07.9%
associate-+l-7.9%
neg-sub08.0%
associate-/r*8.0%
*-rgt-identity8.0%
associate-*r/8.0%
rgt-mult-inverse8.0%
distribute-frac-neg28.0%
distribute-neg-frac8.0%
metadata-eval8.0%
expm1-define100.0%
Simplified100.0%
Taylor expanded in x around 0 99.1%
*-commutative99.1%
Simplified99.1%
(FPCore (x) :precision binary64 (/ (+ 1.0 (* x (+ 0.5 (* x 0.08333333333333333)))) x))
double code(double x) {
return (1.0 + (x * (0.5 + (x * 0.08333333333333333)))) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 + (x * (0.5d0 + (x * 0.08333333333333333d0)))) / x
end function
public static double code(double x) {
return (1.0 + (x * (0.5 + (x * 0.08333333333333333)))) / x;
}
def code(x): return (1.0 + (x * (0.5 + (x * 0.08333333333333333)))) / x
function code(x) return Float64(Float64(1.0 + Float64(x * Float64(0.5 + Float64(x * 0.08333333333333333)))) / x) end
function tmp = code(x) tmp = (1.0 + (x * (0.5 + (x * 0.08333333333333333)))) / x; end
code[x_] := N[(N[(1.0 + N[(x * N[(0.5 + N[(x * 0.08333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{1 + x \cdot \left(0.5 + x \cdot 0.08333333333333333\right)}{x}
\end{array}
Initial program 38.1%
sub-neg38.1%
+-commutative38.1%
rgt-mult-inverse5.3%
exp-neg5.3%
distribute-rgt-neg-out5.3%
*-rgt-identity5.3%
distribute-lft-in5.3%
neg-sub05.3%
associate-+l-5.3%
neg-sub05.4%
associate-/r*5.4%
*-rgt-identity5.4%
associate-*r/5.4%
rgt-mult-inverse38.2%
distribute-frac-neg238.2%
distribute-neg-frac38.2%
metadata-eval38.2%
expm1-define100.0%
Simplified100.0%
Taylor expanded in x around 0 67.3%
*-commutative67.3%
Simplified67.3%
(FPCore (x) :precision binary64 (+ 0.5 (/ 1.0 x)))
double code(double x) {
return 0.5 + (1.0 / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.5d0 + (1.0d0 / x)
end function
public static double code(double x) {
return 0.5 + (1.0 / x);
}
def code(x): return 0.5 + (1.0 / x)
function code(x) return Float64(0.5 + Float64(1.0 / x)) end
function tmp = code(x) tmp = 0.5 + (1.0 / x); end
code[x_] := N[(0.5 + N[(1.0 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 + \frac{1}{x}
\end{array}
Initial program 38.1%
sub-neg38.1%
+-commutative38.1%
rgt-mult-inverse5.3%
exp-neg5.3%
distribute-rgt-neg-out5.3%
*-rgt-identity5.3%
distribute-lft-in5.3%
neg-sub05.3%
associate-+l-5.3%
neg-sub05.4%
associate-/r*5.4%
*-rgt-identity5.4%
associate-*r/5.4%
rgt-mult-inverse38.2%
distribute-frac-neg238.2%
distribute-neg-frac38.2%
metadata-eval38.2%
expm1-define100.0%
Simplified100.0%
Taylor expanded in x around 0 66.9%
*-commutative66.9%
Simplified66.9%
Taylor expanded in x around 0 66.9%
+-commutative66.9%
*-commutative66.9%
fma-undefine66.9%
*-lft-identity66.9%
associate-*l/66.9%
fma-undefine66.9%
distribute-lft-in66.9%
*-commutative66.9%
associate-*l*66.9%
*-commutative66.9%
associate-*l*66.9%
lft-mult-inverse66.9%
metadata-eval66.9%
*-rgt-identity66.9%
+-commutative66.9%
Simplified66.9%
Final simplification66.9%
(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 38.1%
sub-neg38.1%
+-commutative38.1%
rgt-mult-inverse5.3%
exp-neg5.3%
distribute-rgt-neg-out5.3%
*-rgt-identity5.3%
distribute-lft-in5.3%
neg-sub05.3%
associate-+l-5.3%
neg-sub05.4%
associate-/r*5.4%
*-rgt-identity5.4%
associate-*r/5.4%
rgt-mult-inverse38.2%
distribute-frac-neg238.2%
distribute-neg-frac38.2%
metadata-eval38.2%
expm1-define100.0%
Simplified100.0%
Taylor expanded in x around 0 66.5%
(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 38.1%
sub-neg38.1%
+-commutative38.1%
rgt-mult-inverse5.3%
exp-neg5.3%
distribute-rgt-neg-out5.3%
*-rgt-identity5.3%
distribute-lft-in5.3%
neg-sub05.3%
associate-+l-5.3%
neg-sub05.4%
associate-/r*5.4%
*-rgt-identity5.4%
associate-*r/5.4%
rgt-mult-inverse38.2%
distribute-frac-neg238.2%
distribute-neg-frac38.2%
metadata-eval38.2%
expm1-define100.0%
Simplified100.0%
Taylor expanded in x around 0 66.9%
*-commutative66.9%
Simplified66.9%
Taylor expanded in x around inf 3.1%
(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 2024085
(FPCore (x)
:name "expq2 (section 3.11)"
:precision binary64
:pre (> 710.0 x)
:alt
(/ (- 1.0) (expm1 (- x)))
(/ (exp x) (- (exp x) 1.0)))