
(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 16 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
(let* ((t_1 (+ (* y (exp z)) (- 1.0 y))) (t_2 (* (expm1 z) y)))
(if (<= t_1 0.0)
(fma (- x) (/ 1.0 (* (/ x (log1p (* y z))) t)) x)
(if (<= t_1 2.0) (- x (/ t_2 t)) (- x (/ (log t_2) t))))))
double code(double x, double y, double z, double t) {
double t_1 = (y * exp(z)) + (1.0 - y);
double t_2 = expm1(z) * y;
double tmp;
if (t_1 <= 0.0) {
tmp = fma(-x, (1.0 / ((x / log1p((y * z))) * t)), x);
} else if (t_1 <= 2.0) {
tmp = x - (t_2 / t);
} else {
tmp = x - (log(t_2) / t);
}
return tmp;
}
function code(x, y, z, t) t_1 = Float64(Float64(y * exp(z)) + Float64(1.0 - y)) t_2 = Float64(expm1(z) * y) tmp = 0.0 if (t_1 <= 0.0) tmp = fma(Float64(-x), Float64(1.0 / Float64(Float64(x / log1p(Float64(y * z))) * t)), x); elseif (t_1 <= 2.0) tmp = Float64(x - Float64(t_2 / t)); else tmp = Float64(x - Float64(log(t_2) / t)); end return tmp end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[(N[(y * N[Exp[z], $MachinePrecision]), $MachinePrecision] + N[(1.0 - y), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(Exp[z] - 1), $MachinePrecision] * y), $MachinePrecision]}, If[LessEqual[t$95$1, 0.0], N[((-x) * N[(1.0 / N[(N[(x / N[Log[1 + N[(y * z), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * t), $MachinePrecision]), $MachinePrecision] + x), $MachinePrecision], If[LessEqual[t$95$1, 2.0], N[(x - N[(t$95$2 / t), $MachinePrecision]), $MachinePrecision], N[(x - N[(N[Log[t$95$2], $MachinePrecision] / t), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := y \cdot e^{z} + \left(1 - y\right)\\
t_2 := \mathsf{expm1}\left(z\right) \cdot y\\
\mathbf{if}\;t\_1 \leq 0:\\
\;\;\;\;\mathsf{fma}\left(-x, \frac{1}{\frac{x}{\mathsf{log1p}\left(y \cdot z\right)} \cdot t}, x\right)\\
\mathbf{elif}\;t\_1 \leq 2:\\
\;\;\;\;x - \frac{t\_2}{t}\\
\mathbf{else}:\\
\;\;\;\;x - \frac{\log t\_2}{t}\\
\end{array}
\end{array}
if (+.f64 (-.f64 #s(literal 1 binary64) y) (*.f64 y (exp.f64 z))) < 0.0Initial program 1.7%
Taylor expanded in x around inf
+-commutativeN/A
distribute-lft-inN/A
associate-*r*N/A
*-commutativeN/A
*-rgt-identityN/A
lower-fma.f64N/A
Applied rewrites90.3%
Applied rewrites90.3%
Applied rewrites90.3%
Taylor expanded in z around 0
Applied rewrites90.3%
if 0.0 < (+.f64 (-.f64 #s(literal 1 binary64) y) (*.f64 y (exp.f64 z))) < 2Initial program 85.4%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
lower-expm1.f6498.1
Applied rewrites98.1%
if 2 < (+.f64 (-.f64 #s(literal 1 binary64) y) (*.f64 y (exp.f64 z))) Initial program 91.9%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f64N/A
lower-expm1.f6492.9
Applied rewrites92.9%
Final simplification95.7%
(FPCore (x y z t)
:precision binary64
(let* ((t_1 (+ (* y (exp z)) (- 1.0 y))))
(if (<= t_1 0.0)
(fma (- x) (/ (log1p (* y z)) (* x t)) x)
(if (<= t_1 5e+27) (- x (/ (* (expm1 z) y) t)) (- x (/ (log 1.0) t))))))
double code(double x, double y, double z, double t) {
double t_1 = (y * exp(z)) + (1.0 - y);
double tmp;
if (t_1 <= 0.0) {
tmp = fma(-x, (log1p((y * z)) / (x * t)), x);
} else if (t_1 <= 5e+27) {
tmp = x - ((expm1(z) * y) / t);
} else {
tmp = x - (log(1.0) / t);
}
return tmp;
}
function code(x, y, z, t) t_1 = Float64(Float64(y * exp(z)) + Float64(1.0 - y)) tmp = 0.0 if (t_1 <= 0.0) tmp = fma(Float64(-x), Float64(log1p(Float64(y * z)) / Float64(x * t)), x); elseif (t_1 <= 5e+27) tmp = Float64(x - Float64(Float64(expm1(z) * y) / t)); else tmp = Float64(x - Float64(log(1.0) / t)); end return tmp end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[(N[(y * N[Exp[z], $MachinePrecision]), $MachinePrecision] + N[(1.0 - y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, 0.0], N[((-x) * N[(N[Log[1 + N[(y * z), $MachinePrecision]], $MachinePrecision] / N[(x * t), $MachinePrecision]), $MachinePrecision] + x), $MachinePrecision], If[LessEqual[t$95$1, 5e+27], N[(x - N[(N[(N[(Exp[z] - 1), $MachinePrecision] * y), $MachinePrecision] / t), $MachinePrecision]), $MachinePrecision], N[(x - N[(N[Log[1.0], $MachinePrecision] / t), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := y \cdot e^{z} + \left(1 - y\right)\\
\mathbf{if}\;t\_1 \leq 0:\\
\;\;\;\;\mathsf{fma}\left(-x, \frac{\mathsf{log1p}\left(y \cdot z\right)}{x \cdot t}, x\right)\\
\mathbf{elif}\;t\_1 \leq 5 \cdot 10^{+27}:\\
\;\;\;\;x - \frac{\mathsf{expm1}\left(z\right) \cdot y}{t}\\
\mathbf{else}:\\
\;\;\;\;x - \frac{\log 1}{t}\\
\end{array}
\end{array}
if (+.f64 (-.f64 #s(literal 1 binary64) y) (*.f64 y (exp.f64 z))) < 0.0Initial program 2.1%
Taylor expanded in x around inf
+-commutativeN/A
distribute-lft-inN/A
associate-*r*N/A
*-commutativeN/A
*-rgt-identityN/A
lower-fma.f64N/A
Applied rewrites88.0%
Taylor expanded in z around 0
Applied rewrites88.0%
if 0.0 < (+.f64 (-.f64 #s(literal 1 binary64) y) (*.f64 y (exp.f64 z))) < 4.99999999999999979e27Initial program 81.1%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
lower-expm1.f6496.9
Applied rewrites96.9%
if 4.99999999999999979e27 < (+.f64 (-.f64 #s(literal 1 binary64) y) (*.f64 y (exp.f64 z))) Initial program 94.9%
Taylor expanded in y around 0
Applied rewrites47.8%
Final simplification89.2%
(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 2024230
(FPCore (x y z t)
:name "System.Random.MWC.Distributions:truncatedExp from mwc-random-0.13.3.2"
:precision binary64
:alt
(! :herbie-platform default (if (< z -288746230882079470000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) (- (- x (/ (/ (- 1/2) (* y t)) (* z z))) (* (/ (- 1/2) (* y t)) (/ (/ 2 z) (* z z)))) (- x (/ (log (+ 1 (* z y))) t))))
(- x (/ (log (+ (- 1.0 y) (* y (exp z)))) t)))