
(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 (let* ((t_0 (* y (+ (log z) (- 1.0 z)))) (t_1 (* y (- z)))) (if (<= t_0 -2e+19) t_1 (if (<= t_0 2e+147) (* x 0.5) t_1))))
double code(double x, double y, double z) {
double t_0 = y * (log(z) + (1.0 - z));
double t_1 = y * -z;
double tmp;
if (t_0 <= -2e+19) {
tmp = t_1;
} else if (t_0 <= 2e+147) {
tmp = x * 0.5;
} else {
tmp = t_1;
}
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) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = y * (log(z) + (1.0d0 - z))
t_1 = y * -z
if (t_0 <= (-2d+19)) then
tmp = t_1
else if (t_0 <= 2d+147) then
tmp = x * 0.5d0
else
tmp = t_1
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = y * (Math.log(z) + (1.0 - z));
double t_1 = y * -z;
double tmp;
if (t_0 <= -2e+19) {
tmp = t_1;
} else if (t_0 <= 2e+147) {
tmp = x * 0.5;
} else {
tmp = t_1;
}
return tmp;
}
def code(x, y, z): t_0 = y * (math.log(z) + (1.0 - z)) t_1 = y * -z tmp = 0 if t_0 <= -2e+19: tmp = t_1 elif t_0 <= 2e+147: tmp = x * 0.5 else: tmp = t_1 return tmp
function code(x, y, z) t_0 = Float64(y * Float64(log(z) + Float64(1.0 - z))) t_1 = Float64(y * Float64(-z)) tmp = 0.0 if (t_0 <= -2e+19) tmp = t_1; elseif (t_0 <= 2e+147) tmp = Float64(x * 0.5); else tmp = t_1; end return tmp end
function tmp_2 = code(x, y, z) t_0 = y * (log(z) + (1.0 - z)); t_1 = y * -z; tmp = 0.0; if (t_0 <= -2e+19) tmp = t_1; elseif (t_0 <= 2e+147) tmp = x * 0.5; else tmp = t_1; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(y * N[(N[Log[z], $MachinePrecision] + N[(1.0 - z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(y * (-z)), $MachinePrecision]}, If[LessEqual[t$95$0, -2e+19], t$95$1, If[LessEqual[t$95$0, 2e+147], N[(x * 0.5), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y \cdot \left(\log z + \left(1 - z\right)\right)\\
t_1 := y \cdot \left(-z\right)\\
\mathbf{if}\;t\_0 \leq -2 \cdot 10^{+19}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;t\_0 \leq 2 \cdot 10^{+147}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (*.f64 y (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z))) < -2e19 or 2e147 < (*.f64 y (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z))) Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
distribute-rgt-neg-inN/A
lower-*.f64N/A
lower-neg.f6458.7
Applied rewrites58.7%
if -2e19 < (*.f64 y (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z))) < 2e147Initial program 100.0%
Taylor expanded in x around inf
lower-*.f6475.7
Applied rewrites75.7%
Final simplification67.7%
(FPCore (x y z) :precision binary64 (if (<= (* x 0.5) -2e-168) (+ (* x 0.5) (* y (- z))) (if (<= (* x 0.5) 1e-106) (fma y (- (log z) z) y) (fma (- z) y (* x 0.5)))))
double code(double x, double y, double z) {
double tmp;
if ((x * 0.5) <= -2e-168) {
tmp = (x * 0.5) + (y * -z);
} else if ((x * 0.5) <= 1e-106) {
tmp = fma(y, (log(z) - z), y);
} else {
tmp = fma(-z, y, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (Float64(x * 0.5) <= -2e-168) tmp = Float64(Float64(x * 0.5) + Float64(y * Float64(-z))); elseif (Float64(x * 0.5) <= 1e-106) tmp = fma(y, Float64(log(z) - z), y); else tmp = fma(Float64(-z), y, Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := If[LessEqual[N[(x * 0.5), $MachinePrecision], -2e-168], N[(N[(x * 0.5), $MachinePrecision] + N[(y * (-z)), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(x * 0.5), $MachinePrecision], 1e-106], N[(y * N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] + y), $MachinePrecision], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot 0.5 \leq -2 \cdot 10^{-168}:\\
\;\;\;\;x \cdot 0.5 + y \cdot \left(-z\right)\\
\mathbf{elif}\;x \cdot 0.5 \leq 10^{-106}:\\
\;\;\;\;\mathsf{fma}\left(y, \log z - z, y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\end{array}
\end{array}
if (*.f64 x #s(literal 1/2 binary64)) < -2.0000000000000001e-168Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6484.6
Applied rewrites84.6%
if -2.0000000000000001e-168 < (*.f64 x #s(literal 1/2 binary64)) < 9.99999999999999941e-107Initial 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.f6494.5
Applied rewrites94.5%
if 9.99999999999999941e-107 < (*.f64 x #s(literal 1/2 binary64)) Initial 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
*-commutativeN/A
lower-fma.f6499.9
Applied rewrites99.9%
Taylor expanded in y around 0
lower-*.f6490.6
Applied rewrites90.6%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6490.8
Applied rewrites90.8%
Final simplification89.4%
(FPCore (x y z) :precision binary64 (if (<= z 7.5e-14) (fma 0.5 x (fma y (log z) y)) (fma (- 1.0 z) y (* x 0.5))))
double code(double x, double y, double z) {
double tmp;
if (z <= 7.5e-14) {
tmp = fma(0.5, x, fma(y, log(z), y));
} else {
tmp = fma((1.0 - z), y, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 7.5e-14) tmp = fma(0.5, x, fma(y, log(z), y)); else tmp = fma(Float64(1.0 - z), y, Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 7.5e-14], N[(0.5 * x + N[(y * N[Log[z], $MachinePrecision] + y), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 - z), $MachinePrecision] * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 7.5 \cdot 10^{-14}:\\
\;\;\;\;\mathsf{fma}\left(0.5, x, \mathsf{fma}\left(y, \log z, y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(1 - z, y, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 7.4999999999999996e-14Initial 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.7
Applied rewrites99.7%
Taylor expanded in z around 0
+-commutativeN/A
associate-+l+N/A
*-rgt-identityN/A
distribute-lft-inN/A
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
distribute-lft-inN/A
*-rgt-identityN/A
lower-fma.f64N/A
lower-log.f6499.8
Applied rewrites99.8%
if 7.4999999999999996e-14 < z Initial 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
*-commutativeN/A
lower-fma.f6499.3
Applied rewrites99.3%
Taylor expanded in y around 0
lower-*.f6499.1
Applied rewrites99.1%
Final simplification99.4%
(FPCore (x y z) :precision binary64 (if (<= z 7.5e-14) (fma y (log z) (fma 0.5 x y)) (fma (- 1.0 z) y (* x 0.5))))
double code(double x, double y, double z) {
double tmp;
if (z <= 7.5e-14) {
tmp = fma(y, log(z), fma(0.5, x, y));
} else {
tmp = fma((1.0 - z), y, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 7.5e-14) tmp = fma(y, log(z), fma(0.5, x, y)); else tmp = fma(Float64(1.0 - z), y, Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 7.5e-14], N[(y * N[Log[z], $MachinePrecision] + N[(0.5 * x + y), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 - z), $MachinePrecision] * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 7.5 \cdot 10^{-14}:\\
\;\;\;\;\mathsf{fma}\left(y, \log z, \mathsf{fma}\left(0.5, x, y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(1 - z, y, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 7.4999999999999996e-14Initial 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%
if 7.4999999999999996e-14 < z Initial 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
*-commutativeN/A
lower-fma.f6499.3
Applied rewrites99.3%
Taylor expanded in y around 0
lower-*.f6499.1
Applied rewrites99.1%
Final simplification99.4%
(FPCore (x y z) :precision binary64 (if (<= z 2.6e-188) (fma y (log z) y) (fma (- z) y (* x 0.5))))
double code(double x, double y, double z) {
double tmp;
if (z <= 2.6e-188) {
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 <= 2.6e-188) 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, 2.6e-188], 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 2.6 \cdot 10^{-188}:\\
\;\;\;\;\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 < 2.6000000000000001e-188Initial 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.f6455.6
Applied rewrites55.6%
Taylor expanded in z around 0
Applied rewrites55.6%
if 2.6000000000000001e-188 < z 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
*-commutativeN/A
lower-fma.f6499.5
Applied rewrites99.5%
Taylor expanded in y around 0
lower-*.f6486.9
Applied rewrites86.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6487.5
Applied rewrites87.5%
Final simplification82.6%
(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%
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.5
Applied rewrites99.5%
Taylor expanded in y around 0
lower-*.f6480.3
Applied rewrites80.3%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6481.0
Applied rewrites81.0%
Final simplification81.0%
(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-*.f6447.6
Applied rewrites47.6%
Final simplification47.6%
(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 2024233
(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)))))