
(FPCore (x y z) :precision binary64 (+ (* x 0.5) (* y (+ (- 1.0 z) (log z)))))
double code(double x, double y, double z) {
return (x * 0.5) + (y * ((1.0 - z) + log(z)));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (x * 0.5d0) + (y * ((1.0d0 - z) + log(z)))
end function
public static double code(double x, double y, double z) {
return (x * 0.5) + (y * ((1.0 - z) + Math.log(z)));
}
def code(x, y, z): return (x * 0.5) + (y * ((1.0 - z) + math.log(z)))
function code(x, y, z) return Float64(Float64(x * 0.5) + Float64(y * Float64(Float64(1.0 - z) + log(z)))) end
function tmp = code(x, y, z) tmp = (x * 0.5) + (y * ((1.0 - z) + log(z))); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] + N[(y * N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 + y \cdot \left(\left(1 - z\right) + \log z\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (* x 0.5) (* y (+ (- 1.0 z) (log z)))))
double code(double x, double y, double z) {
return (x * 0.5) + (y * ((1.0 - z) + log(z)));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (x * 0.5d0) + (y * ((1.0d0 - z) + log(z)))
end function
public static double code(double x, double y, double z) {
return (x * 0.5) + (y * ((1.0 - z) + Math.log(z)));
}
def code(x, y, z): return (x * 0.5) + (y * ((1.0 - z) + math.log(z)))
function code(x, y, z) return Float64(Float64(x * 0.5) + Float64(y * Float64(Float64(1.0 - z) + log(z)))) end
function tmp = code(x, y, z) tmp = (x * 0.5) + (y * ((1.0 - z) + log(z))); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] + N[(y * N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 + y \cdot \left(\left(1 - z\right) + \log z\right)
\end{array}
(FPCore (x y z) :precision binary64 (fma x 0.5 (fma (- (log z) z) y y)))
double code(double x, double y, double z) {
return fma(x, 0.5, fma((log(z) - z), y, y));
}
function code(x, y, z) return fma(x, 0.5, fma(Float64(log(z) - z), y, y)) end
code[x_, y_, z_] := N[(x * 0.5 + N[(N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] * y + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, 0.5, \mathsf{fma}\left(\log z - z, y, y\right)\right)
\end{array}
Initial program 99.9%
Taylor expanded in z around 0
+-commutativeN/A
associate-+l+N/A
mul-1-negN/A
unsub-negN/A
distribute-lft-out--N/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
associate-+r+N/A
mul-1-negN/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64N/A
lower-log.f6499.9
Applied rewrites99.9%
(FPCore (x y z) :precision binary64 (if (<= (+ (- 1.0 z) (log z)) -800.0) (fma x 0.5 (* (- z) y)) (fma x 0.5 (fma (log z) y y))))
double code(double x, double y, double z) {
double tmp;
if (((1.0 - z) + log(z)) <= -800.0) {
tmp = fma(x, 0.5, (-z * y));
} else {
tmp = fma(x, 0.5, fma(log(z), y, y));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (Float64(Float64(1.0 - z) + log(z)) <= -800.0) tmp = fma(x, 0.5, Float64(Float64(-z) * y)); else tmp = fma(x, 0.5, fma(log(z), y, y)); end return tmp end
code[x_, y_, z_] := If[LessEqual[N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $MachinePrecision], -800.0], N[(x * 0.5 + N[((-z) * y), $MachinePrecision]), $MachinePrecision], N[(x * 0.5 + N[(N[Log[z], $MachinePrecision] * y + y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left(1 - z\right) + \log z \leq -800:\\
\;\;\;\;\mathsf{fma}\left(x, 0.5, \left(-z\right) \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(x, 0.5, \mathsf{fma}\left(\log z, y, y\right)\right)\\
\end{array}
\end{array}
if (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z)) < -800Initial program 100.0%
Taylor expanded in z around 0
+-commutativeN/A
associate-+l+N/A
mul-1-negN/A
unsub-negN/A
distribute-lft-out--N/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
associate-+r+N/A
mul-1-negN/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64N/A
lower-log.f64100.0
Applied rewrites100.0%
Taylor expanded in z around inf
Applied rewrites99.0%
if -800 < (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z)) Initial program 99.8%
Taylor expanded in z around 0
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
lower-log.f6498.8
Applied rewrites98.8%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (fma x 0.5 (* (- z) y))))
(if (<= (* 0.5 x) -5e-57)
t_0
(if (<= (* 0.5 x) 5e-74) (fma (- (log z) z) y y) t_0))))
double code(double x, double y, double z) {
double t_0 = fma(x, 0.5, (-z * y));
double tmp;
if ((0.5 * x) <= -5e-57) {
tmp = t_0;
} else if ((0.5 * x) <= 5e-74) {
tmp = fma((log(z) - z), y, y);
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = fma(x, 0.5, Float64(Float64(-z) * y)) tmp = 0.0 if (Float64(0.5 * x) <= -5e-57) tmp = t_0; elseif (Float64(0.5 * x) <= 5e-74) tmp = fma(Float64(log(z) - z), y, y); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(x * 0.5 + N[((-z) * y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(0.5 * x), $MachinePrecision], -5e-57], t$95$0, If[LessEqual[N[(0.5 * x), $MachinePrecision], 5e-74], N[(N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] * y + y), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(x, 0.5, \left(-z\right) \cdot y\right)\\
\mathbf{if}\;0.5 \cdot x \leq -5 \cdot 10^{-57}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;0.5 \cdot x \leq 5 \cdot 10^{-74}:\\
\;\;\;\;\mathsf{fma}\left(\log z - z, y, y\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if (*.f64 x #s(literal 1/2 binary64)) < -5.0000000000000002e-57 or 4.99999999999999998e-74 < (*.f64 x #s(literal 1/2 binary64)) Initial program 100.0%
Taylor expanded in z around 0
+-commutativeN/A
associate-+l+N/A
mul-1-negN/A
unsub-negN/A
distribute-lft-out--N/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
associate-+r+N/A
mul-1-negN/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64N/A
lower-log.f64100.0
Applied rewrites100.0%
Taylor expanded in z around inf
Applied rewrites88.2%
if -5.0000000000000002e-57 < (*.f64 x #s(literal 1/2 binary64)) < 4.99999999999999998e-74Initial program 99.9%
Taylor expanded in y around inf
sub-negN/A
associate-+r+N/A
mul-1-negN/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64N/A
lower-log.f6490.0
Applied rewrites90.0%
Final simplification89.0%
(FPCore (x y z) :precision binary64 (let* ((t_0 (fma x 0.5 (* (- z) y)))) (if (<= z 2.7e-267) t_0 (if (<= z 3.8e-63) (fma (log z) y y) t_0))))
double code(double x, double y, double z) {
double t_0 = fma(x, 0.5, (-z * y));
double tmp;
if (z <= 2.7e-267) {
tmp = t_0;
} else if (z <= 3.8e-63) {
tmp = fma(log(z), y, y);
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = fma(x, 0.5, Float64(Float64(-z) * y)) tmp = 0.0 if (z <= 2.7e-267) tmp = t_0; elseif (z <= 3.8e-63) tmp = fma(log(z), y, y); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(x * 0.5 + N[((-z) * y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, 2.7e-267], t$95$0, If[LessEqual[z, 3.8e-63], N[(N[Log[z], $MachinePrecision] * y + y), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(x, 0.5, \left(-z\right) \cdot y\right)\\
\mathbf{if}\;z \leq 2.7 \cdot 10^{-267}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 3.8 \cdot 10^{-63}:\\
\;\;\;\;\mathsf{fma}\left(\log z, y, y\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if z < 2.69999999999999987e-267 or 3.80000000000000017e-63 < z Initial program 100.0%
Taylor expanded in z around 0
+-commutativeN/A
associate-+l+N/A
mul-1-negN/A
unsub-negN/A
distribute-lft-out--N/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
associate-+r+N/A
mul-1-negN/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64N/A
lower-log.f64100.0
Applied rewrites100.0%
Taylor expanded in z around inf
Applied rewrites91.5%
if 2.69999999999999987e-267 < z < 3.80000000000000017e-63Initial program 99.8%
Taylor expanded in y around inf
sub-negN/A
associate-+r+N/A
mul-1-negN/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64N/A
lower-log.f6460.3
Applied rewrites60.3%
Taylor expanded in z around 0
Applied rewrites60.3%
(FPCore (x y z) :precision binary64 (if (<= z 6.2e+25) (* 0.5 x) (* (- z) y)))
double code(double x, double y, double z) {
double tmp;
if (z <= 6.2e+25) {
tmp = 0.5 * x;
} else {
tmp = -z * y;
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if (z <= 6.2d+25) then
tmp = 0.5d0 * x
else
tmp = -z * y
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (z <= 6.2e+25) {
tmp = 0.5 * x;
} else {
tmp = -z * y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 6.2e+25: tmp = 0.5 * x else: tmp = -z * y return tmp
function code(x, y, z) tmp = 0.0 if (z <= 6.2e+25) tmp = Float64(0.5 * x); else tmp = Float64(Float64(-z) * y); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= 6.2e+25) tmp = 0.5 * x; else tmp = -z * y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 6.2e+25], N[(0.5 * x), $MachinePrecision], N[((-z) * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 6.2 \cdot 10^{+25}:\\
\;\;\;\;0.5 \cdot x\\
\mathbf{else}:\\
\;\;\;\;\left(-z\right) \cdot y\\
\end{array}
\end{array}
if z < 6.1999999999999996e25Initial program 99.8%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f6447.9
Applied rewrites47.9%
if 6.1999999999999996e25 < z Initial program 100.0%
Taylor expanded in z around inf
mul-1-negN/A
*-commutativeN/A
distribute-lft-neg-inN/A
lower-*.f64N/A
lower-neg.f6483.2
Applied rewrites83.2%
Final simplification66.1%
(FPCore (x y z) :precision binary64 (fma x 0.5 (* (- z) y)))
double code(double x, double y, double z) {
return fma(x, 0.5, (-z * y));
}
function code(x, y, z) return fma(x, 0.5, Float64(Float64(-z) * y)) end
code[x_, y_, z_] := N[(x * 0.5 + N[((-z) * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, 0.5, \left(-z\right) \cdot y\right)
\end{array}
Initial program 99.9%
Taylor expanded in z around 0
+-commutativeN/A
associate-+l+N/A
mul-1-negN/A
unsub-negN/A
distribute-lft-out--N/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
associate-+r+N/A
mul-1-negN/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64N/A
lower-log.f6499.9
Applied rewrites99.9%
Taylor expanded in z around inf
Applied rewrites75.8%
(FPCore (x y z) :precision binary64 (* 0.5 x))
double code(double x, double y, double z) {
return 0.5 * x;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = 0.5d0 * x
end function
public static double code(double x, double y, double z) {
return 0.5 * x;
}
def code(x, y, z): return 0.5 * x
function code(x, y, z) return Float64(0.5 * x) end
function tmp = code(x, y, z) tmp = 0.5 * x; end
code[x_, y_, z_] := N[(0.5 * x), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot x
\end{array}
Initial program 99.9%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f6433.0
Applied rewrites33.0%
Final simplification33.0%
(FPCore (x y z) :precision binary64 (- (+ y (* 0.5 x)) (* y (- z (log z)))))
double code(double x, double y, double z) {
return (y + (0.5 * x)) - (y * (z - log(z)));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (y + (0.5d0 * x)) - (y * (z - log(z)))
end function
public static double code(double x, double y, double z) {
return (y + (0.5 * x)) - (y * (z - Math.log(z)));
}
def code(x, y, z): return (y + (0.5 * x)) - (y * (z - math.log(z)))
function code(x, y, z) return Float64(Float64(y + Float64(0.5 * x)) - Float64(y * Float64(z - log(z)))) end
function tmp = code(x, y, z) tmp = (y + (0.5 * x)) - (y * (z - log(z))); end
code[x_, y_, z_] := N[(N[(y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision] - N[(y * N[(z - N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(y + 0.5 \cdot x\right) - y \cdot \left(z - \log z\right)
\end{array}
herbie shell --seed 2024272
(FPCore (x y z)
:name "System.Random.MWC.Distributions:gamma from mwc-random-0.13.3.2"
:precision binary64
:alt
(! :herbie-platform default (- (+ y (* 1/2 x)) (* y (- z (log z)))))
(+ (* x 0.5) (* y (+ (- 1.0 z) (log z)))))