
(FPCore (x y z t) :precision binary64 (- x (/ (log (+ (- 1.0 y) (* y (exp z)))) t)))
double code(double x, double y, double z, double t) {
return x - (log(((1.0 - y) + (y * exp(z)))) / t);
}
real(8) function code(x, y, z, t)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: t
code = x - (log(((1.0d0 - y) + (y * exp(z)))) / t)
end function
public static double code(double x, double y, double z, double t) {
return x - (Math.log(((1.0 - y) + (y * Math.exp(z)))) / t);
}
def code(x, y, z, t): return x - (math.log(((1.0 - y) + (y * math.exp(z)))) / t)
function code(x, y, z, t) return Float64(x - Float64(log(Float64(Float64(1.0 - y) + Float64(y * exp(z)))) / t)) end
function tmp = code(x, y, z, t) tmp = x - (log(((1.0 - y) + (y * exp(z)))) / t); end
code[x_, y_, z_, t_] := N[(x - N[(N[Log[N[(N[(1.0 - y), $MachinePrecision] + N[(y * N[Exp[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / t), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x - \frac{\log \left(\left(1 - y\right) + y \cdot e^{z}\right)}{t}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z t) :precision binary64 (- x (/ (log (+ (- 1.0 y) (* y (exp z)))) t)))
double code(double x, double y, double z, double t) {
return x - (log(((1.0 - y) + (y * exp(z)))) / t);
}
real(8) function code(x, y, z, t)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: t
code = x - (log(((1.0d0 - y) + (y * exp(z)))) / t)
end function
public static double code(double x, double y, double z, double t) {
return x - (Math.log(((1.0 - y) + (y * Math.exp(z)))) / t);
}
def code(x, y, z, t): return x - (math.log(((1.0 - y) + (y * math.exp(z)))) / t)
function code(x, y, z, t) return Float64(x - Float64(log(Float64(Float64(1.0 - y) + Float64(y * exp(z)))) / t)) end
function tmp = code(x, y, z, t) tmp = x - (log(((1.0 - y) + (y * exp(z)))) / t); end
code[x_, y_, z_, t_] := N[(x - N[(N[Log[N[(N[(1.0 - y), $MachinePrecision] + N[(y * N[Exp[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / t), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x - \frac{\log \left(\left(1 - y\right) + y \cdot e^{z}\right)}{t}
\end{array}
(FPCore (x y z t) :precision binary64 (- x (/ (log1p (* y (expm1 z))) t)))
double code(double x, double y, double z, double t) {
return x - (log1p((y * expm1(z))) / t);
}
public static double code(double x, double y, double z, double t) {
return x - (Math.log1p((y * Math.expm1(z))) / t);
}
def code(x, y, z, t): return x - (math.log1p((y * math.expm1(z))) / t)
function code(x, y, z, t) return Float64(x - Float64(log1p(Float64(y * expm1(z))) / t)) end
code[x_, y_, z_, t_] := N[(x - N[(N[Log[1 + N[(y * N[(Exp[z] - 1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / t), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x - \frac{\mathsf{log1p}\left(y \cdot \mathsf{expm1}\left(z\right)\right)}{t}
\end{array}
Initial program 66.1%
associate-+l-79.5%
sub-neg79.5%
log1p-def83.9%
neg-sub083.9%
associate-+l-83.9%
neg-sub083.9%
neg-mul-183.9%
*-commutative83.9%
distribute-rgt-out83.9%
+-commutative83.9%
metadata-eval83.9%
sub-neg83.9%
expm1-def97.6%
Simplified97.6%
Final simplification97.6%
(FPCore (x y z t) :precision binary64 (- x (/ 1.0 (+ (/ t (* y (expm1 z))) (* t 0.5)))))
double code(double x, double y, double z, double t) {
return x - (1.0 / ((t / (y * expm1(z))) + (t * 0.5)));
}
public static double code(double x, double y, double z, double t) {
return x - (1.0 / ((t / (y * Math.expm1(z))) + (t * 0.5)));
}
def code(x, y, z, t): return x - (1.0 / ((t / (y * math.expm1(z))) + (t * 0.5)))
function code(x, y, z, t) return Float64(x - Float64(1.0 / Float64(Float64(t / Float64(y * expm1(z))) + Float64(t * 0.5)))) end
code[x_, y_, z_, t_] := N[(x - N[(1.0 / N[(N[(t / N[(y * N[(Exp[z] - 1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(t * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x - \frac{1}{\frac{t}{y \cdot \mathsf{expm1}\left(z\right)} + t \cdot 0.5}
\end{array}
Initial program 66.1%
associate-+l-79.5%
sub-neg79.5%
log1p-def83.9%
neg-sub083.9%
associate-+l-83.9%
neg-sub083.9%
neg-mul-183.9%
*-commutative83.9%
distribute-rgt-out83.9%
+-commutative83.9%
metadata-eval83.9%
sub-neg83.9%
expm1-def97.6%
Simplified97.6%
clear-num97.6%
associate-/r/97.6%
Applied egg-rr97.6%
associate-*l/97.6%
*-un-lft-identity97.6%
clear-num97.6%
Applied egg-rr97.6%
Taylor expanded in y around 0 74.6%
expm1-def84.0%
*-commutative84.0%
Simplified84.0%
Final simplification84.0%
(FPCore (x y z t) :precision binary64 (- x (* y (/ (expm1 z) t))))
double code(double x, double y, double z, double t) {
return x - (y * (expm1(z) / t));
}
public static double code(double x, double y, double z, double t) {
return x - (y * (Math.expm1(z) / t));
}
def code(x, y, z, t): return x - (y * (math.expm1(z) / t))
function code(x, y, z, t) return Float64(x - Float64(y * Float64(expm1(z) / t))) end
code[x_, y_, z_, t_] := N[(x - N[(y * N[(N[(Exp[z] - 1), $MachinePrecision] / t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x - y \cdot \frac{\mathsf{expm1}\left(z\right)}{t}
\end{array}
Initial program 66.1%
associate-+l-79.5%
sub-neg79.5%
log1p-def83.9%
neg-sub083.9%
associate-+l-83.9%
neg-sub083.9%
neg-mul-183.9%
*-commutative83.9%
distribute-rgt-out83.9%
+-commutative83.9%
metadata-eval83.9%
sub-neg83.9%
expm1-def97.6%
Simplified97.6%
Taylor expanded in y around 0 71.3%
associate-/l*71.1%
associate-/r/71.3%
expm1-def80.9%
Simplified80.9%
Final simplification80.9%
(FPCore (x y z t) :precision binary64 (if (<= z -8.6e-6) x (- x (* z (/ y t)))))
double code(double x, double y, double z, double t) {
double tmp;
if (z <= -8.6e-6) {
tmp = x;
} else {
tmp = x - (z * (y / t));
}
return tmp;
}
real(8) function code(x, y, z, t)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: t
real(8) :: tmp
if (z <= (-8.6d-6)) then
tmp = x
else
tmp = x - (z * (y / t))
end if
code = tmp
end function
public static double code(double x, double y, double z, double t) {
double tmp;
if (z <= -8.6e-6) {
tmp = x;
} else {
tmp = x - (z * (y / t));
}
return tmp;
}
def code(x, y, z, t): tmp = 0 if z <= -8.6e-6: tmp = x else: tmp = x - (z * (y / t)) return tmp
function code(x, y, z, t) tmp = 0.0 if (z <= -8.6e-6) tmp = x; else tmp = Float64(x - Float64(z * Float64(y / t))); end return tmp end
function tmp_2 = code(x, y, z, t) tmp = 0.0; if (z <= -8.6e-6) tmp = x; else tmp = x - (z * (y / t)); end tmp_2 = tmp; end
code[x_, y_, z_, t_] := If[LessEqual[z, -8.6e-6], x, N[(x - N[(z * N[(y / t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -8.6 \cdot 10^{-6}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;x - z \cdot \frac{y}{t}\\
\end{array}
\end{array}
if z < -8.60000000000000067e-6Initial program 86.0%
associate-+l-86.0%
sub-neg86.0%
log1p-def99.8%
neg-sub099.8%
associate-+l-99.8%
neg-sub099.8%
neg-mul-199.8%
*-commutative99.8%
distribute-rgt-out99.8%
+-commutative99.8%
metadata-eval99.8%
sub-neg99.8%
expm1-def99.8%
Simplified99.8%
Taylor expanded in x around inf 53.7%
if -8.60000000000000067e-6 < z Initial program 56.7%
associate-+l-76.5%
sub-neg76.5%
log1p-def76.5%
neg-sub076.5%
associate-+l-76.5%
neg-sub076.5%
neg-mul-176.5%
*-commutative76.5%
distribute-rgt-out76.5%
+-commutative76.5%
metadata-eval76.5%
sub-neg76.5%
expm1-def96.6%
Simplified96.6%
Taylor expanded in z around 0 88.7%
associate-/l*90.4%
associate-/r/88.0%
Simplified88.0%
Final simplification77.0%
(FPCore (x y z t) :precision binary64 (if (<= z -1.8e-6) x (- x (* y (/ z t)))))
double code(double x, double y, double z, double t) {
double tmp;
if (z <= -1.8e-6) {
tmp = x;
} else {
tmp = x - (y * (z / t));
}
return tmp;
}
real(8) function code(x, y, z, t)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: t
real(8) :: tmp
if (z <= (-1.8d-6)) then
tmp = x
else
tmp = x - (y * (z / t))
end if
code = tmp
end function
public static double code(double x, double y, double z, double t) {
double tmp;
if (z <= -1.8e-6) {
tmp = x;
} else {
tmp = x - (y * (z / t));
}
return tmp;
}
def code(x, y, z, t): tmp = 0 if z <= -1.8e-6: tmp = x else: tmp = x - (y * (z / t)) return tmp
function code(x, y, z, t) tmp = 0.0 if (z <= -1.8e-6) tmp = x; else tmp = Float64(x - Float64(y * Float64(z / t))); end return tmp end
function tmp_2 = code(x, y, z, t) tmp = 0.0; if (z <= -1.8e-6) tmp = x; else tmp = x - (y * (z / t)); end tmp_2 = tmp; end
code[x_, y_, z_, t_] := If[LessEqual[z, -1.8e-6], x, N[(x - N[(y * N[(z / t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -1.8 \cdot 10^{-6}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;x - y \cdot \frac{z}{t}\\
\end{array}
\end{array}
if z < -1.79999999999999992e-6Initial program 86.0%
associate-+l-86.0%
sub-neg86.0%
log1p-def99.8%
neg-sub099.8%
associate-+l-99.8%
neg-sub099.8%
neg-mul-199.8%
*-commutative99.8%
distribute-rgt-out99.8%
+-commutative99.8%
metadata-eval99.8%
sub-neg99.8%
expm1-def99.8%
Simplified99.8%
Taylor expanded in x around inf 53.7%
if -1.79999999999999992e-6 < z Initial program 56.7%
associate-+l-76.5%
sub-neg76.5%
log1p-def76.5%
neg-sub076.5%
associate-+l-76.5%
neg-sub076.5%
neg-mul-176.5%
*-commutative76.5%
distribute-rgt-out76.5%
+-commutative76.5%
metadata-eval76.5%
sub-neg76.5%
expm1-def96.6%
Simplified96.6%
Taylor expanded in z around 0 88.7%
associate-/l*90.4%
associate-/r/88.0%
Simplified88.0%
*-commutative88.0%
clear-num88.0%
un-div-inv88.0%
Applied egg-rr88.0%
associate-/r/90.4%
Simplified90.4%
Final simplification78.6%
(FPCore (x y z t) :precision binary64 x)
double code(double x, double y, double z, double t) {
return x;
}
real(8) function code(x, y, z, t)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: t
code = x
end function
public static double code(double x, double y, double z, double t) {
return x;
}
def code(x, y, z, t): return x
function code(x, y, z, t) return x end
function tmp = code(x, y, z, t) tmp = x; end
code[x_, y_, z_, t_] := x
\begin{array}{l}
\\
x
\end{array}
Initial program 66.1%
associate-+l-79.5%
sub-neg79.5%
log1p-def83.9%
neg-sub083.9%
associate-+l-83.9%
neg-sub083.9%
neg-mul-183.9%
*-commutative83.9%
distribute-rgt-out83.9%
+-commutative83.9%
metadata-eval83.9%
sub-neg83.9%
expm1-def97.6%
Simplified97.6%
Taylor expanded in x around inf 68.9%
Final simplification68.9%
(FPCore (x y z t)
:precision binary64
(let* ((t_1 (/ (- 0.5) (* y t))))
(if (< z -2.8874623088207947e+119)
(- (- x (/ t_1 (* z z))) (* t_1 (/ (/ 2.0 z) (* z z))))
(- x (/ (log (+ 1.0 (* z y))) t)))))
double code(double x, double y, double z, double t) {
double t_1 = -0.5 / (y * t);
double tmp;
if (z < -2.8874623088207947e+119) {
tmp = (x - (t_1 / (z * z))) - (t_1 * ((2.0 / z) / (z * z)));
} else {
tmp = x - (log((1.0 + (z * y))) / t);
}
return tmp;
}
real(8) function code(x, y, z, t)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8), intent (in) :: t
real(8) :: t_1
real(8) :: tmp
t_1 = -0.5d0 / (y * t)
if (z < (-2.8874623088207947d+119)) then
tmp = (x - (t_1 / (z * z))) - (t_1 * ((2.0d0 / z) / (z * z)))
else
tmp = x - (log((1.0d0 + (z * y))) / t)
end if
code = tmp
end function
public static double code(double x, double y, double z, double t) {
double t_1 = -0.5 / (y * t);
double tmp;
if (z < -2.8874623088207947e+119) {
tmp = (x - (t_1 / (z * z))) - (t_1 * ((2.0 / z) / (z * z)));
} else {
tmp = x - (Math.log((1.0 + (z * y))) / t);
}
return tmp;
}
def code(x, y, z, t): t_1 = -0.5 / (y * t) tmp = 0 if z < -2.8874623088207947e+119: tmp = (x - (t_1 / (z * z))) - (t_1 * ((2.0 / z) / (z * z))) else: tmp = x - (math.log((1.0 + (z * y))) / t) return tmp
function code(x, y, z, t) t_1 = Float64(Float64(-0.5) / Float64(y * t)) tmp = 0.0 if (z < -2.8874623088207947e+119) tmp = Float64(Float64(x - Float64(t_1 / Float64(z * z))) - Float64(t_1 * Float64(Float64(2.0 / z) / Float64(z * z)))); else tmp = Float64(x - Float64(log(Float64(1.0 + Float64(z * y))) / t)); end return tmp end
function tmp_2 = code(x, y, z, t) t_1 = -0.5 / (y * t); tmp = 0.0; if (z < -2.8874623088207947e+119) tmp = (x - (t_1 / (z * z))) - (t_1 * ((2.0 / z) / (z * z))); else tmp = x - (log((1.0 + (z * y))) / t); end tmp_2 = tmp; end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[((-0.5) / N[(y * t), $MachinePrecision]), $MachinePrecision]}, If[Less[z, -2.8874623088207947e+119], N[(N[(x - N[(t$95$1 / N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(t$95$1 * N[(N[(2.0 / z), $MachinePrecision] / N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(x - N[(N[Log[N[(1.0 + N[(z * y), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / t), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := \frac{-0.5}{y \cdot t}\\
\mathbf{if}\;z < -2.8874623088207947 \cdot 10^{+119}:\\
\;\;\;\;\left(x - \frac{t_1}{z \cdot z}\right) - t_1 \cdot \frac{\frac{2}{z}}{z \cdot z}\\
\mathbf{else}:\\
\;\;\;\;x - \frac{\log \left(1 + z \cdot y\right)}{t}\\
\end{array}
\end{array}
herbie shell --seed 2023176
(FPCore (x y z t)
:name "System.Random.MWC.Distributions:truncatedExp from mwc-random-0.13.3.2"
:precision binary64
:herbie-target
(if (< z -2.8874623088207947e+119) (- (- x (/ (/ (- 0.5) (* y t)) (* z z))) (* (/ (- 0.5) (* y t)) (/ (/ 2.0 z) (* z z)))) (- x (/ (log (+ 1.0 (* z y))) t)))
(- x (/ (log (+ (- 1.0 y) (* y (exp z)))) t)))