
(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 (+ (* 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}
Initial program 99.9%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (+ (- 1.0 z) (log z))))
(if (<= t_0 -330.0)
(fma (- z) y (* 0.5 x))
(if (<= t_0 -140.0)
(fma (log z) y y)
(fma (* (/ -1.0 (* z z)) (/ (* (- z) z) (/ -1.0 z))) y (* 0.5 x))))))
double code(double x, double y, double z) {
double t_0 = (1.0 - z) + log(z);
double tmp;
if (t_0 <= -330.0) {
tmp = fma(-z, y, (0.5 * x));
} else if (t_0 <= -140.0) {
tmp = fma(log(z), y, y);
} else {
tmp = fma(((-1.0 / (z * z)) * ((-z * z) / (-1.0 / z))), y, (0.5 * x));
}
return tmp;
}
function code(x, y, z) t_0 = Float64(Float64(1.0 - z) + log(z)) tmp = 0.0 if (t_0 <= -330.0) tmp = fma(Float64(-z), y, Float64(0.5 * x)); elseif (t_0 <= -140.0) tmp = fma(log(z), y, y); else tmp = fma(Float64(Float64(-1.0 / Float64(z * z)) * Float64(Float64(Float64(-z) * z) / Float64(-1.0 / z))), y, Float64(0.5 * x)); end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -330.0], N[((-z) * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, -140.0], N[(N[Log[z], $MachinePrecision] * y + y), $MachinePrecision], N[(N[(N[(-1.0 / N[(z * z), $MachinePrecision]), $MachinePrecision] * N[(N[((-z) * z), $MachinePrecision] / N[(-1.0 / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(1 - z\right) + \log z\\
\mathbf{if}\;t\_0 \leq -330:\\
\;\;\;\;\mathsf{fma}\left(-z, y, 0.5 \cdot x\right)\\
\mathbf{elif}\;t\_0 \leq -140:\\
\;\;\;\;\mathsf{fma}\left(\log z, y, y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{-1}{z \cdot z} \cdot \frac{\left(-z\right) \cdot z}{\frac{-1}{z}}, y, 0.5 \cdot x\right)\\
\end{array}
\end{array}
if (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z)) < -330Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6484.6
Applied rewrites84.6%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6484.6
lift-*.f64N/A
*-commutativeN/A
lower-*.f6484.6
Applied rewrites84.6%
if -330 < (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z)) < -140Initial 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.7
Applied rewrites99.7%
Taylor expanded in x around 0
Applied rewrites70.6%
if -140 < (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z)) Initial program 99.8%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6468.6
Applied rewrites68.6%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6468.6
lift-*.f64N/A
*-commutativeN/A
lower-*.f6468.6
Applied rewrites68.6%
Applied rewrites68.6%
(FPCore (x y z) :precision binary64 (if (<= (+ (- 1.0 z) (log z)) -15000000000.0) (fma (- 1.0 z) y (* 0.5 x)) (fma 0.5 x (fma (log z) y y))))
double code(double x, double y, double z) {
double tmp;
if (((1.0 - z) + log(z)) <= -15000000000.0) {
tmp = fma((1.0 - z), y, (0.5 * x));
} else {
tmp = fma(0.5, x, fma(log(z), y, y));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (Float64(Float64(1.0 - z) + log(z)) <= -15000000000.0) tmp = fma(Float64(1.0 - z), y, Float64(0.5 * x)); else tmp = fma(0.5, x, 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], -15000000000.0], N[(N[(1.0 - z), $MachinePrecision] * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision], N[(0.5 * x + N[(N[Log[z], $MachinePrecision] * y + y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left(1 - z\right) + \log z \leq -15000000000:\\
\;\;\;\;\mathsf{fma}\left(1 - z, y, 0.5 \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(0.5, x, \mathsf{fma}\left(\log z, y, y\right)\right)\\
\end{array}
\end{array}
if (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z)) < -1.5e10Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift-+.f64N/A
distribute-rgt-inN/A
associate-+l+N/A
lower-fma.f64N/A
lower-fma.f64100.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f64100.0
Applied rewrites100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-log.f6481.0
Applied rewrites81.0%
Taylor expanded in x around inf
Applied rewrites99.1%
if -1.5e10 < (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z)) Initial program 99.8%
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.1
Applied rewrites99.1%
(FPCore (x y z) :precision binary64 (if (or (<= y -2.1e+52) (not (<= y 1e+132))) (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.1e+52) || !(y <= 1e+132)) {
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.1e+52) || !(y <= 1e+132)) 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.1e+52], N[Not[LessEqual[y, 1e+132]], $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.1 \cdot 10^{+52} \lor \neg \left(y \leq 10^{+132}\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.1e52 or 9.99999999999999991e131 < y Initial program 99.8%
Taylor expanded in x around 0
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.f6493.1
Applied rewrites93.1%
if -2.1e52 < y < 9.99999999999999991e131Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6487.1
Applied rewrites87.1%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6487.1
lift-*.f64N/A
*-commutativeN/A
lower-*.f6487.1
Applied rewrites87.1%
Final simplification89.4%
(FPCore (x y z) :precision binary64 (if (<= z 15500000000.0) (* 0.5 x) (* (- z) y)))
double code(double x, double y, double z) {
double tmp;
if (z <= 15500000000.0) {
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 <= 15500000000.0d0) 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 <= 15500000000.0) {
tmp = 0.5 * x;
} else {
tmp = -z * y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 15500000000.0: tmp = 0.5 * x else: tmp = -z * y return tmp
function code(x, y, z) tmp = 0.0 if (z <= 15500000000.0) 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 <= 15500000000.0) tmp = 0.5 * x; else tmp = -z * y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 15500000000.0], N[(0.5 * x), $MachinePrecision], N[((-z) * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 15500000000:\\
\;\;\;\;0.5 \cdot x\\
\mathbf{else}:\\
\;\;\;\;\left(-z\right) \cdot y\\
\end{array}
\end{array}
if z < 1.55e10Initial program 99.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift-+.f64N/A
distribute-rgt-inN/A
associate-+l+N/A
lower-fma.f64N/A
lower-fma.f6499.7
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.7
Applied rewrites99.7%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-log.f6491.1
Applied rewrites91.1%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
associate-/l*N/A
distribute-rgt-outN/A
associate-+r-N/A
+-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lower--.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-log.f6491.1
Applied rewrites91.1%
Taylor expanded in x around inf
Applied rewrites54.3%
if 1.55e10 < 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.f6476.6
Applied rewrites76.6%
Final simplification65.3%
(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.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6476.7
Applied rewrites76.7%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6476.7
lift-*.f64N/A
*-commutativeN/A
lower-*.f6476.7
Applied rewrites76.7%
(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%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lift-+.f64N/A
distribute-rgt-inN/A
associate-+l+N/A
lower-fma.f64N/A
lower-fma.f6499.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-log.f6486.1
Applied rewrites86.1%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
associate-/l*N/A
distribute-rgt-outN/A
associate-+r-N/A
+-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lower--.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-log.f6491.8
Applied rewrites91.8%
Taylor expanded in x around inf
Applied rewrites39.9%
Final simplification39.9%
(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 2024324
(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)))))