
(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 8 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 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 (<= (+ (log z) (- 1.0 z)) -4000.0) (fma y (- z) (fma 0.5 x y)) (fma y (log z) (fma 0.5 x y))))
double code(double x, double y, double z) {
double tmp;
if ((log(z) + (1.0 - z)) <= -4000.0) {
tmp = fma(y, -z, fma(0.5, x, y));
} else {
tmp = fma(y, log(z), fma(0.5, x, y));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (Float64(log(z) + Float64(1.0 - z)) <= -4000.0) tmp = fma(y, Float64(-z), fma(0.5, x, y)); else tmp = fma(y, log(z), fma(0.5, x, y)); end return tmp end
code[x_, y_, z_] := If[LessEqual[N[(N[Log[z], $MachinePrecision] + N[(1.0 - z), $MachinePrecision]), $MachinePrecision], -4000.0], N[(y * (-z) + N[(0.5 * x + y), $MachinePrecision]), $MachinePrecision], N[(y * N[Log[z], $MachinePrecision] + N[(0.5 * x + y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\log z + \left(1 - z\right) \leq -4000:\\
\;\;\;\;\mathsf{fma}\left(y, -z, \mathsf{fma}\left(0.5, x, y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, \log z, \mathsf{fma}\left(0.5, x, y\right)\right)\\
\end{array}
\end{array}
if (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z)) < -4e3Initial program 100.0%
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.f64100.0
Applied rewrites100.0%
Taylor expanded in z around inf
Applied rewrites99.1%
if -4e3 < (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z)) Initial 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.4
Applied rewrites99.4%
Final simplification99.3%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (- (log z) z)))
(if (<= y -3.25e+41)
(+ y (* y t_0))
(if (<= y 1.12e+51) (fma (- z) y (* x 0.5)) (fma y t_0 y)))))
double code(double x, double y, double z) {
double t_0 = log(z) - z;
double tmp;
if (y <= -3.25e+41) {
tmp = y + (y * t_0);
} else if (y <= 1.12e+51) {
tmp = fma(-z, y, (x * 0.5));
} else {
tmp = fma(y, t_0, y);
}
return tmp;
}
function code(x, y, z) t_0 = Float64(log(z) - z) tmp = 0.0 if (y <= -3.25e+41) tmp = Float64(y + Float64(y * t_0)); elseif (y <= 1.12e+51) tmp = fma(Float64(-z), y, Float64(x * 0.5)); else tmp = fma(y, t_0, y); end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision]}, If[LessEqual[y, -3.25e+41], N[(y + N[(y * t$95$0), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 1.12e+51], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision], N[(y * t$95$0 + y), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \log z - z\\
\mathbf{if}\;y \leq -3.25 \cdot 10^{+41}:\\
\;\;\;\;y + y \cdot t\_0\\
\mathbf{elif}\;y \leq 1.12 \cdot 10^{+51}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, t\_0, y\right)\\
\end{array}
\end{array}
if y < -3.24999999999999988e41Initial 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
*-commutativeN/A
lower-fma.f6499.8
Applied rewrites99.8%
Taylor expanded in y around inf
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.f6487.2
Applied rewrites87.2%
Applied rewrites87.2%
if -3.24999999999999988e41 < y < 1.11999999999999992e51Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6486.2
Applied rewrites86.2%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6486.2
Applied rewrites86.2%
if 1.11999999999999992e51 < y Initial program 99.9%
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.f6492.1
Applied rewrites92.1%
Final simplification87.4%
(FPCore (x y z) :precision binary64 (let* ((t_0 (fma y (- (log z) z) y))) (if (<= y -3.25e+41) t_0 (if (<= y 1.12e+51) (fma (- z) y (* x 0.5)) t_0))))
double code(double x, double y, double z) {
double t_0 = fma(y, (log(z) - z), y);
double tmp;
if (y <= -3.25e+41) {
tmp = t_0;
} else if (y <= 1.12e+51) {
tmp = fma(-z, y, (x * 0.5));
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = fma(y, Float64(log(z) - z), y) tmp = 0.0 if (y <= -3.25e+41) tmp = t_0; elseif (y <= 1.12e+51) tmp = fma(Float64(-z), y, Float64(x * 0.5)); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(y * N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] + y), $MachinePrecision]}, If[LessEqual[y, -3.25e+41], t$95$0, If[LessEqual[y, 1.12e+51], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(y, \log z - z, y\right)\\
\mathbf{if}\;y \leq -3.25 \cdot 10^{+41}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 1.12 \cdot 10^{+51}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < -3.24999999999999988e41 or 1.11999999999999992e51 < 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-lft-inN/A
*-rgt-identityN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64N/A
lower-log.f6489.0
Applied rewrites89.0%
if -3.24999999999999988e41 < y < 1.11999999999999992e51Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6486.2
Applied rewrites86.2%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6486.2
Applied rewrites86.2%
(FPCore (x y z) :precision binary64 (if (<= (+ (log z) (- 1.0 z)) -1e+54) (* y (- z)) (* x 0.5)))
double code(double x, double y, double z) {
double tmp;
if ((log(z) + (1.0 - z)) <= -1e+54) {
tmp = y * -z;
} else {
tmp = x * 0.5;
}
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 ((log(z) + (1.0d0 - z)) <= (-1d+54)) then
tmp = y * -z
else
tmp = x * 0.5d0
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((Math.log(z) + (1.0 - z)) <= -1e+54) {
tmp = y * -z;
} else {
tmp = x * 0.5;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (math.log(z) + (1.0 - z)) <= -1e+54: tmp = y * -z else: tmp = x * 0.5 return tmp
function code(x, y, z) tmp = 0.0 if (Float64(log(z) + Float64(1.0 - z)) <= -1e+54) tmp = Float64(y * Float64(-z)); else tmp = Float64(x * 0.5); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((log(z) + (1.0 - z)) <= -1e+54) tmp = y * -z; else tmp = x * 0.5; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[N[(N[Log[z], $MachinePrecision] + N[(1.0 - z), $MachinePrecision]), $MachinePrecision], -1e+54], N[(y * (-z)), $MachinePrecision], N[(x * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\log z + \left(1 - z\right) \leq -1 \cdot 10^{+54}:\\
\;\;\;\;y \cdot \left(-z\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5\\
\end{array}
\end{array}
if (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z)) < -1.0000000000000001e54Initial program 100.0%
Taylor expanded in z around inf
mul-1-negN/A
distribute-rgt-neg-inN/A
lower-*.f64N/A
lower-neg.f6482.4
Applied rewrites82.4%
if -1.0000000000000001e54 < (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z)) Initial program 99.8%
Taylor expanded in x around inf
lower-*.f6448.5
Applied rewrites48.5%
Final simplification62.9%
(FPCore (x y z) :precision binary64 (if (<= z 1.4e-128) (fma y (log z) y) (fma (- z) y (* x 0.5))))
double code(double x, double y, double z) {
double tmp;
if (z <= 1.4e-128) {
tmp = fma(y, log(z), y);
} else {
tmp = fma(-z, y, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 1.4e-128) tmp = fma(y, log(z), y); else tmp = fma(Float64(-z), y, Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 1.4e-128], N[(y * N[Log[z], $MachinePrecision] + y), $MachinePrecision], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 1.4 \cdot 10^{-128}:\\
\;\;\;\;\mathsf{fma}\left(y, \log z, y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 1.3999999999999999e-128Initial 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.8
Applied rewrites99.8%
Taylor expanded in y around inf
Applied rewrites61.5%
if 1.3999999999999999e-128 < z Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6488.6
Applied rewrites88.6%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6488.7
Applied rewrites88.7%
(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.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6473.8
Applied rewrites73.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6473.8
Applied rewrites73.8%
(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-*.f6436.0
Applied rewrites36.0%
Final simplification36.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 2024223
(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)))))