
(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 9 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 (- (log z) (- z 1.0)) y (* 0.5 x)))
double code(double x, double y, double z) {
return fma((log(z) - (z - 1.0)), y, (0.5 * x));
}
function code(x, y, z) return fma(Float64(log(z) - Float64(z - 1.0)), y, Float64(0.5 * x)) end
code[x_, y_, z_] := N[(N[(N[Log[z], $MachinePrecision] - N[(z - 1.0), $MachinePrecision]), $MachinePrecision] * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\log z - \left(z - 1\right), y, 0.5 \cdot x\right)
\end{array}
Initial program 99.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.9
lift-+.f64N/A
+-commutativeN/A
lift--.f64N/A
associate-+r-N/A
lower--.f64N/A
lower-+.f6499.8
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.8
Applied rewrites99.8%
lift--.f64N/A
lift-+.f64N/A
+-commutativeN/A
associate--l+N/A
lift--.f64N/A
+-commutativeN/A
lift--.f64N/A
associate-+l-N/A
lower--.f64N/A
lower--.f6499.9
Applied rewrites99.9%
(FPCore (x y z) :precision binary64 (if (or (<= y -2.75e+50) (not (<= y 8.2e+86))) (fma (- (log z) z) y y) (fma (- z) y (* 0.5 x))))
double code(double x, double y, double z) {
double tmp;
if ((y <= -2.75e+50) || !(y <= 8.2e+86)) {
tmp = fma((log(z) - z), y, y);
} else {
tmp = fma(-z, y, (0.5 * x));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((y <= -2.75e+50) || !(y <= 8.2e+86)) tmp = fma(Float64(log(z) - z), y, y); else tmp = fma(Float64(-z), y, Float64(0.5 * x)); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[y, -2.75e+50], N[Not[LessEqual[y, 8.2e+86]], $MachinePrecision]], N[(N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] * y + y), $MachinePrecision], N[((-z) * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.75 \cdot 10^{+50} \lor \neg \left(y \leq 8.2 \cdot 10^{+86}\right):\\
\;\;\;\;\mathsf{fma}\left(\log z - z, y, y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, 0.5 \cdot x\right)\\
\end{array}
\end{array}
if y < -2.7499999999999999e50 or 8.1999999999999998e86 < y Initial program 99.8%
Taylor expanded in x around 0
associate--l+N/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
lower--.f64N/A
lower-log.f6491.2
Applied rewrites91.2%
if -2.7499999999999999e50 < y < 8.1999999999999998e86Initial program 99.9%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.9
lift-+.f64N/A
+-commutativeN/A
lift--.f64N/A
associate-+r-N/A
lower--.f64N/A
lower-+.f6499.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6480.0
Applied rewrites80.0%
Final simplification84.8%
(FPCore (x y z) :precision binary64 (if (<= z 9e-5) (fma (+ (log z) 1.0) y (* 0.5 x)) (fma (- z) y (* 0.5 x))))
double code(double x, double y, double z) {
double tmp;
if (z <= 9e-5) {
tmp = fma((log(z) + 1.0), y, (0.5 * x));
} else {
tmp = fma(-z, y, (0.5 * x));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 9e-5) tmp = fma(Float64(log(z) + 1.0), y, Float64(0.5 * x)); else tmp = fma(Float64(-z), y, Float64(0.5 * x)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 9e-5], N[(N[(N[Log[z], $MachinePrecision] + 1.0), $MachinePrecision] * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision], N[((-z) * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 9 \cdot 10^{-5}:\\
\;\;\;\;\mathsf{fma}\left(\log z + 1, y, 0.5 \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, 0.5 \cdot x\right)\\
\end{array}
\end{array}
if z < 9.00000000000000057e-5Initial program 99.7%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.8
lift-+.f64N/A
+-commutativeN/A
lift--.f64N/A
associate-+r-N/A
lower--.f64N/A
lower-+.f6499.8
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.8
Applied rewrites99.8%
Taylor expanded in z around 0
+-commutativeN/A
lower-+.f64N/A
lower-log.f6499.2
Applied rewrites99.2%
if 9.00000000000000057e-5 < z Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f64100.0
lift-+.f64N/A
+-commutativeN/A
lift--.f64N/A
associate-+r-N/A
lower--.f64N/A
lower-+.f6499.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6496.8
Applied rewrites96.8%
(FPCore (x y z) :precision binary64 (if (<= z 9e-5) (fma 0.5 x (fma (log z) y y)) (fma (- z) y (* 0.5 x))))
double code(double x, double y, double z) {
double tmp;
if (z <= 9e-5) {
tmp = fma(0.5, x, fma(log(z), y, y));
} else {
tmp = fma(-z, y, (0.5 * x));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 9e-5) tmp = fma(0.5, x, fma(log(z), y, y)); else tmp = fma(Float64(-z), y, Float64(0.5 * x)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 9e-5], N[(0.5 * x + N[(N[Log[z], $MachinePrecision] * y + y), $MachinePrecision]), $MachinePrecision], N[((-z) * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 9 \cdot 10^{-5}:\\
\;\;\;\;\mathsf{fma}\left(0.5, x, \mathsf{fma}\left(\log z, y, y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, 0.5 \cdot x\right)\\
\end{array}
\end{array}
if z < 9.00000000000000057e-5Initial program 99.7%
Taylor expanded in z around 0
lower-fma.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
lower-log.f6499.2
Applied rewrites99.2%
if 9.00000000000000057e-5 < z Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f64100.0
lift-+.f64N/A
+-commutativeN/A
lift--.f64N/A
associate-+r-N/A
lower--.f64N/A
lower-+.f6499.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6496.8
Applied rewrites96.8%
Final simplification98.1%
(FPCore (x y z) :precision binary64 (fma (- (log z) z) y (fma 0.5 x y)))
double code(double x, double y, double z) {
return fma((log(z) - z), y, fma(0.5, x, y));
}
function code(x, y, z) return fma(Float64(log(z) - z), y, fma(0.5, x, y)) end
code[x_, y_, z_] := N[(N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] * y + N[(0.5 * x + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\log z - z, y, \mathsf{fma}\left(0.5, x, y\right)\right)
\end{array}
Initial program 99.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.9
lift-+.f64N/A
+-commutativeN/A
lift--.f64N/A
associate-+r-N/A
lower--.f64N/A
lower-+.f6499.8
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.8
Applied rewrites99.8%
Taylor expanded in x around 0
associate--l+N/A
distribute-lft-inN/A
*-rgt-identityN/A
+-commutativeN/A
associate-+l+N/A
+-commutativeN/A
associate-+l+N/A
*-commutativeN/A
lower-fma.f64N/A
lower--.f64N/A
lower-log.f64N/A
lower-fma.f6499.9
Applied rewrites99.9%
(FPCore (x y z) :precision binary64 (fma 0.5 x (fma (- (log z) z) y y)))
double code(double x, double y, double z) {
return fma(0.5, x, fma((log(z) - z), y, y));
}
function code(x, y, z) return fma(0.5, x, fma(Float64(log(z) - z), y, y)) end
code[x_, y_, z_] := N[(0.5 * x + N[(N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] * y + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(0.5, x, \mathsf{fma}\left(\log z - z, y, y\right)\right)
\end{array}
Initial program 99.8%
Taylor expanded in x around 0
fp-cancel-sign-sub-invN/A
distribute-lft-neg-outN/A
distribute-rgt-neg-outN/A
mul-1-negN/A
fp-cancel-sub-sign-invN/A
mul-1-negN/A
lower-fma.f64N/A
mul-1-negN/A
distribute-rgt-neg-inN/A
distribute-lft-neg-inN/A
distribute-lft-neg-inN/A
metadata-evalN/A
*-lft-identityN/A
associate--l+N/A
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
Applied rewrites99.8%
Final simplification99.8%
(FPCore (x y z) :precision binary64 (if (<= z 4.1e-10) (fma (log z) y y) (fma (- z) y (* 0.5 x))))
double code(double x, double y, double z) {
double tmp;
if (z <= 4.1e-10) {
tmp = fma(log(z), y, y);
} else {
tmp = fma(-z, y, (0.5 * x));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 4.1e-10) tmp = fma(log(z), y, y); else tmp = fma(Float64(-z), y, Float64(0.5 * x)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 4.1e-10], N[(N[Log[z], $MachinePrecision] * y + y), $MachinePrecision], N[((-z) * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 4.1 \cdot 10^{-10}:\\
\;\;\;\;\mathsf{fma}\left(\log z, y, y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, 0.5 \cdot x\right)\\
\end{array}
\end{array}
if z < 4.0999999999999998e-10Initial program 99.7%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.8
lift-+.f64N/A
+-commutativeN/A
lift--.f64N/A
associate-+r-N/A
lower--.f64N/A
lower-+.f6499.8
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.8
Applied rewrites99.8%
Taylor expanded in x around 0
associate--l+N/A
distribute-lft-inN/A
*-rgt-identityN/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
lower--.f64N/A
lower-log.f6459.0
Applied rewrites59.0%
Taylor expanded in z around 0
Applied rewrites58.5%
if 4.0999999999999998e-10 < z Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f64100.0
lift-+.f64N/A
+-commutativeN/A
lift--.f64N/A
associate-+r-N/A
lower--.f64N/A
lower-+.f6499.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6496.8
Applied rewrites96.8%
(FPCore (x y z) :precision binary64 (fma (- z) y (* 0.5 x)))
double code(double x, double y, double z) {
return fma(-z, y, (0.5 * x));
}
function code(x, y, z) return fma(Float64(-z), y, Float64(0.5 * x)) end
code[x_, y_, z_] := N[((-z) * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-z, y, 0.5 \cdot x\right)
\end{array}
Initial program 99.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.9
lift-+.f64N/A
+-commutativeN/A
lift--.f64N/A
associate-+r-N/A
lower--.f64N/A
lower-+.f6499.8
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.8
Applied rewrites99.8%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6468.8
Applied rewrites68.8%
(FPCore (x y z) :precision binary64 (* (- y) z))
double code(double x, double y, double z) {
return -y * 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 * z
end function
public static double code(double x, double y, double z) {
return -y * z;
}
def code(x, y, z): return -y * z
function code(x, y, z) return Float64(Float64(-y) * z) end
function tmp = code(x, y, z) tmp = -y * z; end
code[x_, y_, z_] := N[((-y) * z), $MachinePrecision]
\begin{array}{l}
\\
\left(-y\right) \cdot z
\end{array}
Initial program 99.8%
Taylor expanded in z around inf
associate-*r*N/A
lower-*.f64N/A
mul-1-negN/A
lower-neg.f6434.8
Applied rewrites34.8%
Final simplification34.8%
(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 2024326
(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)))))