
(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 (- 1.0 z) y (* x 0.5))))
double code(double x, double y, double z) {
return fma(log(z), y, fma((1.0 - z), y, (x * 0.5)));
}
function code(x, y, z) return fma(log(z), y, fma(Float64(1.0 - z), y, Float64(x * 0.5))) end
code[x_, y_, z_] := N[(N[Log[z], $MachinePrecision] * y + N[(N[(1.0 - z), $MachinePrecision] * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\log z, y, \mathsf{fma}\left(1 - z, y, x \cdot 0.5\right)\right)
\end{array}
Initial program 99.8%
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
lower-fma.f6499.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
Final simplification99.9%
(FPCore (x y z) :precision binary64 (if (or (<= z 2.7e-99) (not (or (<= z 2.2e-43) (not (<= z 0.00335))))) (fma (log z) y y) (fma (- z) y (* x 0.5))))
double code(double x, double y, double z) {
double tmp;
if ((z <= 2.7e-99) || !((z <= 2.2e-43) || !(z <= 0.00335))) {
tmp = fma(log(z), y, y);
} else {
tmp = fma(-z, y, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((z <= 2.7e-99) || !((z <= 2.2e-43) || !(z <= 0.00335))) tmp = fma(log(z), y, y); else tmp = fma(Float64(-z), y, Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[z, 2.7e-99], N[Not[Or[LessEqual[z, 2.2e-43], N[Not[LessEqual[z, 0.00335]], $MachinePrecision]]], $MachinePrecision]], N[(N[Log[z], $MachinePrecision] * y + y), $MachinePrecision], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 2.7 \cdot 10^{-99} \lor \neg \left(z \leq 2.2 \cdot 10^{-43} \lor \neg \left(z \leq 0.00335\right)\right):\\
\;\;\;\;\mathsf{fma}\left(\log z, y, y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 2.7e-99 or 2.19999999999999997e-43 < z < 0.00335000000000000011Initial program 99.7%
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.f6497.9
Applied rewrites97.9%
Taylor expanded in y around inf
Applied rewrites64.7%
if 2.7e-99 < z < 2.19999999999999997e-43 or 0.00335000000000000011 < z Initial program 99.9%
lift-+.f64N/A
flip3-+N/A
clear-numN/A
lower-/.f64N/A
clear-numN/A
flip3-+N/A
Applied rewrites99.7%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6494.0
Applied rewrites94.0%
lift-/.f64N/A
lift-/.f64N/A
remove-double-div94.2
Applied rewrites94.2%
Final simplification82.7%
(FPCore (x y z) :precision binary64 (if (or (<= y -1.75e-112) (not (<= y 1.75e+30))) (fma (- (log z) z) y y) (fma (- z) y (* x 0.5))))
double code(double x, double y, double z) {
double tmp;
if ((y <= -1.75e-112) || !(y <= 1.75e+30)) {
tmp = fma((log(z) - z), y, y);
} else {
tmp = fma(-z, y, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((y <= -1.75e-112) || !(y <= 1.75e+30)) tmp = fma(Float64(log(z) - z), y, y); else tmp = fma(Float64(-z), y, Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[y, -1.75e-112], N[Not[LessEqual[y, 1.75e+30]], $MachinePrecision]], N[(N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] * y + y), $MachinePrecision], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.75 \cdot 10^{-112} \lor \neg \left(y \leq 1.75 \cdot 10^{+30}\right):\\
\;\;\;\;\mathsf{fma}\left(\log z - z, y, y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\end{array}
\end{array}
if y < -1.74999999999999997e-112 or 1.75000000000000011e30 < y Initial 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.f6490.8
Applied rewrites90.8%
if -1.74999999999999997e-112 < y < 1.75000000000000011e30Initial program 99.9%
lift-+.f64N/A
flip3-+N/A
clear-numN/A
lower-/.f64N/A
clear-numN/A
flip3-+N/A
Applied rewrites99.7%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6489.7
Applied rewrites89.7%
lift-/.f64N/A
lift-/.f64N/A
remove-double-div89.9
Applied rewrites89.9%
Final simplification90.4%
(FPCore (x y z) :precision binary64 (+ (* (+ (- 1.0 z) (log z)) y) (* x 0.5)))
double code(double x, double y, double z) {
return (((1.0 - z) + log(z)) * y) + (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 = (((1.0d0 - z) + log(z)) * y) + (x * 0.5d0)
end function
public static double code(double x, double y, double z) {
return (((1.0 - z) + Math.log(z)) * y) + (x * 0.5);
}
def code(x, y, z): return (((1.0 - z) + math.log(z)) * y) + (x * 0.5)
function code(x, y, z) return Float64(Float64(Float64(Float64(1.0 - z) + log(z)) * y) + Float64(x * 0.5)) end
function tmp = code(x, y, z) tmp = (((1.0 - z) + log(z)) * y) + (x * 0.5); end
code[x_, y_, z_] := N[(N[(N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $MachinePrecision] * y), $MachinePrecision] + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(1 - z\right) + \log z\right) \cdot y + x \cdot 0.5
\end{array}
Initial program 99.8%
Final simplification99.8%
(FPCore (x y z) :precision binary64 (if (<= z 0.275) (fma x 0.5 (fma (log z) y y)) (fma (- z) y (* x 0.5))))
double code(double x, double y, double z) {
double tmp;
if (z <= 0.275) {
tmp = fma(x, 0.5, fma(log(z), y, y));
} else {
tmp = fma(-z, y, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 0.275) tmp = fma(x, 0.5, fma(log(z), y, y)); else tmp = fma(Float64(-z), y, Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 0.275], N[(x * 0.5 + N[(N[Log[z], $MachinePrecision] * y + y), $MachinePrecision]), $MachinePrecision], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 0.275:\\
\;\;\;\;\mathsf{fma}\left(x, 0.5, \mathsf{fma}\left(\log z, y, y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 0.27500000000000002Initial program 99.7%
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.2
Applied rewrites98.2%
if 0.27500000000000002 < z Initial program 99.9%
lift-+.f64N/A
flip3-+N/A
clear-numN/A
lower-/.f64N/A
clear-numN/A
flip3-+N/A
Applied rewrites99.7%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6498.5
Applied rewrites98.5%
lift-/.f64N/A
lift-/.f64N/A
remove-double-div98.7
Applied rewrites98.7%
Final simplification98.5%
(FPCore (x y z) :precision binary64 (fma (- (+ 1.0 (log z)) z) y (* x 0.5)))
double code(double x, double y, double z) {
return fma(((1.0 + log(z)) - z), y, (x * 0.5));
}
function code(x, y, z) return fma(Float64(Float64(1.0 + log(z)) - z), y, Float64(x * 0.5)) end
code[x_, y_, z_] := N[(N[(N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision] * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\left(1 + \log z\right) - z, y, x \cdot 0.5\right)
\end{array}
Initial program 99.8%
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%
Final simplification99.8%
(FPCore (x y z) :precision binary64 (if (<= z 65000.0) (* x 0.5) (* (- z) y)))
double code(double x, double y, double z) {
double tmp;
if (z <= 65000.0) {
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 <= 65000.0d0) 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 <= 65000.0) {
tmp = x * 0.5;
} else {
tmp = -z * y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 65000.0: tmp = x * 0.5 else: tmp = -z * y return tmp
function code(x, y, z) tmp = 0.0 if (z <= 65000.0) 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 <= 65000.0) tmp = x * 0.5; else tmp = -z * y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 65000.0], N[(x * 0.5), $MachinePrecision], N[((-z) * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 65000:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\left(-z\right) \cdot y\\
\end{array}
\end{array}
if z < 65000Initial program 99.7%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f6440.5
Applied rewrites40.5%
if 65000 < 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.f6477.5
Applied rewrites77.5%
(FPCore (x y z) :precision binary64 (fma (- z) y (* x 0.5)))
double code(double x, double y, double z) {
return fma(-z, y, (x * 0.5));
}
function code(x, y, z) return fma(Float64(-z), y, Float64(x * 0.5)) end
code[x_, y_, z_] := N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-z, y, x \cdot 0.5\right)
\end{array}
Initial program 99.8%
lift-+.f64N/A
flip3-+N/A
clear-numN/A
lower-/.f64N/A
clear-numN/A
flip3-+N/A
Applied rewrites99.6%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6471.1
Applied rewrites71.1%
lift-/.f64N/A
lift-/.f64N/A
remove-double-div71.3
Applied rewrites71.3%
Final simplification71.3%
(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.8%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f6432.0
Applied rewrites32.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 2024271
(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)))))