
(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 (+ (* (+ (log z) (- 1.0 z)) y) (* 0.5 x)))
double code(double x, double y, double z) {
return ((log(z) + (1.0 - z)) * y) + (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 = ((log(z) + (1.0d0 - z)) * y) + (0.5d0 * x)
end function
public static double code(double x, double y, double z) {
return ((Math.log(z) + (1.0 - z)) * y) + (0.5 * x);
}
def code(x, y, z): return ((math.log(z) + (1.0 - z)) * y) + (0.5 * x)
function code(x, y, z) return Float64(Float64(Float64(log(z) + Float64(1.0 - z)) * y) + Float64(0.5 * x)) end
function tmp = code(x, y, z) tmp = ((log(z) + (1.0 - z)) * y) + (0.5 * x); end
code[x_, y_, z_] := N[(N[(N[(N[Log[z], $MachinePrecision] + N[(1.0 - z), $MachinePrecision]), $MachinePrecision] * y), $MachinePrecision] + N[(0.5 * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\log z + \left(1 - z\right)\right) \cdot y + 0.5 \cdot x
\end{array}
Initial program 99.9%
Final simplification99.9%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (fma (log z) y y)) (t_1 (+ (log z) (- 1.0 z))))
(if (<= t_1 -520.0)
(fma (- z) y (* 0.5 x))
(if (<= t_1 -370.0)
t_0
(if (<= t_1 -100.0) (* (fma (/ y x) (- z) 0.5) x) t_0)))))
double code(double x, double y, double z) {
double t_0 = fma(log(z), y, y);
double t_1 = log(z) + (1.0 - z);
double tmp;
if (t_1 <= -520.0) {
tmp = fma(-z, y, (0.5 * x));
} else if (t_1 <= -370.0) {
tmp = t_0;
} else if (t_1 <= -100.0) {
tmp = fma((y / x), -z, 0.5) * x;
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = fma(log(z), y, y) t_1 = Float64(log(z) + Float64(1.0 - z)) tmp = 0.0 if (t_1 <= -520.0) tmp = fma(Float64(-z), y, Float64(0.5 * x)); elseif (t_1 <= -370.0) tmp = t_0; elseif (t_1 <= -100.0) tmp = Float64(fma(Float64(y / x), Float64(-z), 0.5) * x); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[Log[z], $MachinePrecision] * y + y), $MachinePrecision]}, Block[{t$95$1 = N[(N[Log[z], $MachinePrecision] + N[(1.0 - z), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, -520.0], N[((-z) * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, -370.0], t$95$0, If[LessEqual[t$95$1, -100.0], N[(N[(N[(y / x), $MachinePrecision] * (-z) + 0.5), $MachinePrecision] * x), $MachinePrecision], t$95$0]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(\log z, y, y\right)\\
t_1 := \log z + \left(1 - z\right)\\
\mathbf{if}\;t\_1 \leq -520:\\
\;\;\;\;\mathsf{fma}\left(-z, y, 0.5 \cdot x\right)\\
\mathbf{elif}\;t\_1 \leq -370:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;t\_1 \leq -100:\\
\;\;\;\;\mathsf{fma}\left(\frac{y}{x}, -z, 0.5\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z)) < -520Initial program 100.0%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6492.0
Applied rewrites92.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6492.0
Applied rewrites92.0%
if -520 < (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z)) < -370 or -100 < (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z)) Initial 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%
Taylor expanded in y around inf
Applied rewrites67.1%
if -370 < (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z)) < -100Initial 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.8
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.8
Applied rewrites99.8%
Taylor expanded in z around inf
mul-1-negN/A
*-commutativeN/A
distribute-lft-neg-inN/A
lower-*.f64N/A
lower-neg.f644.1
Applied rewrites4.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.f6486.3
Applied rewrites86.3%
Taylor expanded in z around inf
Applied rewrites62.5%
Final simplification81.0%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (fma (- (log z) z) y y)))
(if (<= y -1.38e+93)
t_0
(if (<= y 32500000000000.0) (fma (- z) y (* 0.5 x)) t_0))))
double code(double x, double y, double z) {
double t_0 = fma((log(z) - z), y, y);
double tmp;
if (y <= -1.38e+93) {
tmp = t_0;
} else if (y <= 32500000000000.0) {
tmp = fma(-z, y, (0.5 * x));
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = fma(Float64(log(z) - z), y, y) tmp = 0.0 if (y <= -1.38e+93) tmp = t_0; elseif (y <= 32500000000000.0) tmp = fma(Float64(-z), y, Float64(0.5 * x)); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] * y + y), $MachinePrecision]}, If[LessEqual[y, -1.38e+93], t$95$0, If[LessEqual[y, 32500000000000.0], N[((-z) * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(\log z - z, y, y\right)\\
\mathbf{if}\;y \leq -1.38 \cdot 10^{+93}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 32500000000000:\\
\;\;\;\;\mathsf{fma}\left(-z, y, 0.5 \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < -1.38000000000000005e93 or 3.25e13 < y Initial program 99.9%
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.f6493.2
Applied rewrites93.2%
if -1.38000000000000005e93 < y < 3.25e13Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6486.7
Applied rewrites86.7%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6486.7
Applied rewrites86.7%
Final simplification89.3%
(FPCore (x y z) :precision binary64 (if (<= z 0.1) (fma x 0.5 (fma (log z) y y)) (fma (- 1.0 z) y (* 0.5 x))))
double code(double x, double y, double z) {
double tmp;
if (z <= 0.1) {
tmp = fma(x, 0.5, fma(log(z), y, y));
} else {
tmp = fma((1.0 - z), y, (0.5 * x));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 0.1) tmp = fma(x, 0.5, fma(log(z), y, y)); else tmp = fma(Float64(1.0 - z), y, Float64(0.5 * x)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 0.1], N[(x * 0.5 + N[(N[Log[z], $MachinePrecision] * y + y), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 - z), $MachinePrecision] * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 0.1:\\
\;\;\;\;\mathsf{fma}\left(x, 0.5, \mathsf{fma}\left(\log z, y, y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(1 - z, y, 0.5 \cdot x\right)\\
\end{array}
\end{array}
if z < 0.10000000000000001Initial program 99.8%
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.f6499.2
Applied rewrites99.2%
if 0.10000000000000001 < 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
lower-fma.f64100.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f64100.0
Applied rewrites100.0%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f6499.2
Applied rewrites99.2%
Final simplification99.2%
(FPCore (x y z) :precision binary64 (if (<= z 3.2e+50) (* 0.5 x) (* (- z) y)))
double code(double x, double y, double z) {
double tmp;
if (z <= 3.2e+50) {
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 <= 3.2d+50) 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 <= 3.2e+50) {
tmp = 0.5 * x;
} else {
tmp = -z * y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 3.2e+50: tmp = 0.5 * x else: tmp = -z * y return tmp
function code(x, y, z) tmp = 0.0 if (z <= 3.2e+50) 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 <= 3.2e+50) tmp = 0.5 * x; else tmp = -z * y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 3.2e+50], N[(0.5 * x), $MachinePrecision], N[((-z) * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 3.2 \cdot 10^{+50}:\\
\;\;\;\;0.5 \cdot x\\
\mathbf{else}:\\
\;\;\;\;\left(-z\right) \cdot y\\
\end{array}
\end{array}
if z < 3.19999999999999983e50Initial program 99.8%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f6451.4
Applied rewrites51.4%
if 3.19999999999999983e50 < 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.f6480.1
Applied rewrites80.1%
Final simplification64.4%
(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.f6475.2
Applied rewrites75.2%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6475.2
Applied rewrites75.2%
Final simplification75.2%
(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%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f6438.1
Applied rewrites38.1%
Final simplification38.1%
(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 2024256
(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)))))