
(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) y (fma y (- 1.0 z) (* x 0.5))))
double code(double x, double y, double z) {
return fma(log(z), y, fma(y, (1.0 - z), (x * 0.5)));
}
function code(x, y, z) return fma(log(z), y, fma(y, Float64(1.0 - z), Float64(x * 0.5))) end
code[x_, y_, z_] := N[(N[Log[z], $MachinePrecision] * y + N[(y * N[(1.0 - z), $MachinePrecision] + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\log z, y, \mathsf{fma}\left(y, 1 - z, x \cdot 0.5\right)\right)
\end{array}
Initial program 99.9%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift-+.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
associate-+l+N/A
lower-fma.f64N/A
*-commutativeN/A
lower-fma.f6499.9
Applied rewrites99.9%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (+ (* x 0.5) (* y (- z)))))
(if (<= (* x 0.5) -1e-134)
t_0
(if (<= (* x 0.5) 1e-52) (fma y (- (log z) z) y) t_0))))
double code(double x, double y, double z) {
double t_0 = (x * 0.5) + (y * -z);
double tmp;
if ((x * 0.5) <= -1e-134) {
tmp = t_0;
} else if ((x * 0.5) <= 1e-52) {
tmp = fma(y, (log(z) - z), y);
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = Float64(Float64(x * 0.5) + Float64(y * Float64(-z))) tmp = 0.0 if (Float64(x * 0.5) <= -1e-134) tmp = t_0; elseif (Float64(x * 0.5) <= 1e-52) tmp = fma(y, Float64(log(z) - z), y); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(x * 0.5), $MachinePrecision] + N[(y * (-z)), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(x * 0.5), $MachinePrecision], -1e-134], t$95$0, If[LessEqual[N[(x * 0.5), $MachinePrecision], 1e-52], N[(y * N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] + y), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x \cdot 0.5 + y \cdot \left(-z\right)\\
\mathbf{if}\;x \cdot 0.5 \leq -1 \cdot 10^{-134}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;x \cdot 0.5 \leq 10^{-52}:\\
\;\;\;\;\mathsf{fma}\left(y, \log z - z, y\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if (*.f64 x #s(literal 1/2 binary64)) < -1.00000000000000004e-134 or 1e-52 < (*.f64 x #s(literal 1/2 binary64)) Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6483.2
Applied rewrites83.2%
if -1.00000000000000004e-134 < (*.f64 x #s(literal 1/2 binary64)) < 1e-52Initial program 99.7%
Taylor expanded in x around 0
sub-negN/A
associate-+r+N/A
mul-1-negN/A
+-commutativeN/A
distribute-lft-inN/A
*-rgt-identityN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64N/A
lower-log.f6496.2
Applied rewrites96.2%
(FPCore (x y z) :precision binary64 (if (<= z 0.000135) (fma x 0.5 (fma y (log z) y)) (+ (* x 0.5) (* y (- z)))))
double code(double x, double y, double z) {
double tmp;
if (z <= 0.000135) {
tmp = fma(x, 0.5, fma(y, log(z), y));
} else {
tmp = (x * 0.5) + (y * -z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 0.000135) tmp = fma(x, 0.5, fma(y, log(z), y)); else tmp = Float64(Float64(x * 0.5) + Float64(y * Float64(-z))); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 0.000135], N[(x * 0.5 + N[(y * N[Log[z], $MachinePrecision] + y), $MachinePrecision]), $MachinePrecision], N[(N[(x * 0.5), $MachinePrecision] + N[(y * (-z)), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 0.000135:\\
\;\;\;\;\mathsf{fma}\left(x, 0.5, \mathsf{fma}\left(y, \log z, y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 + y \cdot \left(-z\right)\\
\end{array}
\end{array}
if z < 1.35000000000000002e-4Initial program 99.8%
lift-*.f64N/A
lift-+.f64N/A
flip-+N/A
clear-numN/A
un-div-invN/A
lower-/.f64N/A
clear-numN/A
flip-+N/A
lift-+.f64N/A
lower-/.f6499.7
Applied rewrites99.7%
Taylor expanded in z around inf
sub-negN/A
metadata-evalN/A
distribute-lft-inN/A
*-commutativeN/A
mul-1-negN/A
lower-fma.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-/.f64N/A
mul-1-negN/A
distribute-frac-negN/A
log-recN/A
remove-double-negN/A
lower-/.f64N/A
lower-log.f64N/A
lower-neg.f6498.0
Applied rewrites98.0%
Taylor expanded in z around 0
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
lower-fma.f64N/A
lower-log.f6499.6
Applied rewrites99.6%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f6499.6
Applied rewrites99.6%
if 1.35000000000000002e-4 < z Initial program 100.0%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6498.8
Applied rewrites98.8%
(FPCore (x y z) :precision binary64 (if (<= z 0.000135) (fma y (log z) (fma 0.5 x y)) (+ (* x 0.5) (* y (- z)))))
double code(double x, double y, double z) {
double tmp;
if (z <= 0.000135) {
tmp = fma(y, log(z), fma(0.5, x, y));
} else {
tmp = (x * 0.5) + (y * -z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 0.000135) tmp = fma(y, log(z), fma(0.5, x, y)); else tmp = Float64(Float64(x * 0.5) + Float64(y * Float64(-z))); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 0.000135], N[(y * N[Log[z], $MachinePrecision] + N[(0.5 * x + y), $MachinePrecision]), $MachinePrecision], N[(N[(x * 0.5), $MachinePrecision] + N[(y * (-z)), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 0.000135:\\
\;\;\;\;\mathsf{fma}\left(y, \log z, \mathsf{fma}\left(0.5, x, y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 + y \cdot \left(-z\right)\\
\end{array}
\end{array}
if z < 1.35000000000000002e-4Initial program 99.8%
Taylor expanded in z around 0
distribute-lft-inN/A
*-rgt-identityN/A
associate-+r+N/A
+-commutativeN/A
lower-fma.f64N/A
lower-log.f64N/A
lower-fma.f6499.6
Applied rewrites99.6%
if 1.35000000000000002e-4 < z Initial program 100.0%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6498.8
Applied rewrites98.8%
(FPCore (x y z) :precision binary64 (fma y (- (log z) z) (fma 0.5 x y)))
double code(double x, double y, double z) {
return fma(y, (log(z) - z), fma(0.5, x, y));
}
function code(x, y, z) return fma(y, Float64(log(z) - z), fma(0.5, x, y)) end
code[x_, y_, z_] := N[(y * N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] + N[(0.5 * x + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, \log z - z, \mathsf{fma}\left(0.5, x, y\right)\right)
\end{array}
Initial program 99.9%
Taylor expanded in x around 0
sub-negN/A
associate-+r+N/A
mul-1-negN/A
distribute-lft-inN/A
*-rgt-identityN/A
associate-+r+N/A
+-commutativeN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64N/A
lower-log.f64N/A
lower-fma.f6499.9
Applied rewrites99.9%
(FPCore (x y z) :precision binary64 (if (<= z 5.8e-12) (fma y (log z) y) (+ (* x 0.5) (* y (- z)))))
double code(double x, double y, double z) {
double tmp;
if (z <= 5.8e-12) {
tmp = fma(y, log(z), y);
} else {
tmp = (x * 0.5) + (y * -z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 5.8e-12) tmp = fma(y, log(z), y); else tmp = Float64(Float64(x * 0.5) + Float64(y * Float64(-z))); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 5.8e-12], N[(y * N[Log[z], $MachinePrecision] + y), $MachinePrecision], N[(N[(x * 0.5), $MachinePrecision] + N[(y * (-z)), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 5.8 \cdot 10^{-12}:\\
\;\;\;\;\mathsf{fma}\left(y, \log z, y\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 + y \cdot \left(-z\right)\\
\end{array}
\end{array}
if z < 5.8000000000000003e-12Initial program 99.8%
Taylor expanded in z around 0
distribute-lft-inN/A
*-rgt-identityN/A
associate-+r+N/A
+-commutativeN/A
lower-fma.f64N/A
lower-log.f64N/A
lower-fma.f6499.6
Applied rewrites99.6%
Taylor expanded in y around inf
Applied rewrites60.2%
if 5.8000000000000003e-12 < z Initial program 100.0%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6498.8
Applied rewrites98.8%
(FPCore (x y z) :precision binary64 (+ (* x 0.5) (* y (- z))))
double code(double x, double y, double z) {
return (x * 0.5) + (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 = (x * 0.5d0) + (y * -z)
end function
public static double code(double x, double y, double z) {
return (x * 0.5) + (y * -z);
}
def code(x, y, z): return (x * 0.5) + (y * -z)
function code(x, y, z) return Float64(Float64(x * 0.5) + Float64(y * Float64(-z))) end
function tmp = code(x, y, z) tmp = (x * 0.5) + (y * -z); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] + N[(y * (-z)), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 + y \cdot \left(-z\right)
\end{array}
Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6470.9
Applied rewrites70.9%
(FPCore (x y z) :precision binary64 (if (<= z 3.8e+86) (* x 0.5) (- (* z y))))
double code(double x, double y, double z) {
double tmp;
if (z <= 3.8e+86) {
tmp = x * 0.5;
} 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 <= 3.8d+86) then
tmp = x * 0.5d0
else
tmp = -(z * y)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (z <= 3.8e+86) {
tmp = x * 0.5;
} else {
tmp = -(z * y);
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 3.8e+86: tmp = x * 0.5 else: tmp = -(z * y) return tmp
function code(x, y, z) tmp = 0.0 if (z <= 3.8e+86) tmp = Float64(x * 0.5); else tmp = Float64(-Float64(z * y)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= 3.8e+86) tmp = x * 0.5; else tmp = -(z * y); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 3.8e+86], N[(x * 0.5), $MachinePrecision], (-N[(z * y), $MachinePrecision])]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 3.8 \cdot 10^{+86}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;-z \cdot y\\
\end{array}
\end{array}
if z < 3.79999999999999978e86Initial program 99.8%
Taylor expanded in x around inf
lower-*.f6445.1
Applied rewrites45.1%
if 3.79999999999999978e86 < z Initial program 100.0%
Taylor expanded in z around inf
mul-1-negN/A
distribute-rgt-neg-inN/A
lower-*.f64N/A
lower-neg.f6478.1
Applied rewrites78.1%
Final simplification56.2%
(FPCore (x y z) :precision binary64 (* x 0.5))
double code(double x, double y, double z) {
return x * 0.5;
}
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
end function
public static double code(double x, double y, double z) {
return x * 0.5;
}
def code(x, y, z): return x * 0.5
function code(x, y, z) return Float64(x * 0.5) end
function tmp = code(x, y, z) tmp = x * 0.5; end
code[x_, y_, z_] := N[(x * 0.5), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5
\end{array}
Initial program 99.9%
Taylor expanded in x around inf
lower-*.f6437.5
Applied rewrites37.5%
Final simplification37.5%
(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 2024220
(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)))))