
(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 10 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.9%
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
(let* ((t_0 (fma (- z) y (* x 0.5))))
(if (<= (* x 0.5) -4e-97)
t_0
(if (<= (* x 0.5) 2e-91) (fma (- (log z) z) y y) t_0))))
double code(double x, double y, double z) {
double t_0 = fma(-z, y, (x * 0.5));
double tmp;
if ((x * 0.5) <= -4e-97) {
tmp = t_0;
} else if ((x * 0.5) <= 2e-91) {
tmp = fma((log(z) - z), y, y);
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = fma(Float64(-z), y, Float64(x * 0.5)) tmp = 0.0 if (Float64(x * 0.5) <= -4e-97) tmp = t_0; elseif (Float64(x * 0.5) <= 2e-91) tmp = fma(Float64(log(z) - z), y, y); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(x * 0.5), $MachinePrecision], -4e-97], t$95$0, If[LessEqual[N[(x * 0.5), $MachinePrecision], 2e-91], N[(N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] * y + y), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\mathbf{if}\;x \cdot 0.5 \leq -4 \cdot 10^{-97}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;x \cdot 0.5 \leq 2 \cdot 10^{-91}:\\
\;\;\;\;\mathsf{fma}\left(\log z - z, y, y\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if (*.f64 x #s(literal 1/2 binary64)) < -4.00000000000000014e-97 or 2.00000000000000004e-91 < (*.f64 x #s(literal 1/2 binary64)) Initial program 99.9%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.9
lift-+.f64N/A
+-commutativeN/A
lift--.f64N/A
associate-+r-N/A
lower--.f64N/A
lower-+.f6499.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6484.1
Applied rewrites84.1%
if -4.00000000000000014e-97 < (*.f64 x #s(literal 1/2 binary64)) < 2.00000000000000004e-91Initial 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.f6484.9
Applied rewrites84.9%
Final simplification84.4%
(FPCore (x y z) :precision binary64 (if (<= z 0.0074) (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.0074) {
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.0074) 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.0074], 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.0074:\\
\;\;\;\;\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.0074000000000000003Initial 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.f6498.8
Applied rewrites98.8%
if 0.0074000000000000003 < z Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f64100.0
lift-+.f64N/A
+-commutativeN/A
lift--.f64N/A
associate-+r-N/A
lower--.f64N/A
lower-+.f64100.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f64100.0
Applied rewrites100.0%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6498.8
Applied rewrites98.8%
Final simplification98.8%
(FPCore (x y z) :precision binary64 (+ (fma y (- (log z) z) (* x 0.5)) y))
double code(double x, double y, double z) {
return fma(y, (log(z) - z), (x * 0.5)) + y;
}
function code(x, y, z) return Float64(fma(y, Float64(log(z) - z), Float64(x * 0.5)) + y) end
code[x_, y_, z_] := N[(N[(y * N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] + N[(x * 0.5), $MachinePrecision]), $MachinePrecision] + y), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, \log z - z, x \cdot 0.5\right) + y
\end{array}
Initial program 99.9%
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%
lift-fma.f64N/A
+-commutativeN/A
lift-fma.f64N/A
+-commutativeN/A
associate-+r+N/A
lift-*.f64N/A
*-commutativeN/A
distribute-rgt-inN/A
+-commutativeN/A
lift--.f64N/A
associate--l+N/A
lift-+.f64N/A
lift--.f64N/A
*-commutativeN/A
lower-fma.f64N/A
lift--.f64N/A
lift-+.f64N/A
+-commutativeN/A
associate--l+N/A
lift--.f64N/A
+-commutativeN/A
distribute-lft1-inN/A
lift-fma.f6499.9
Applied rewrites99.9%
lift-fma.f64N/A
lift-*.f64N/A
lift-fma.f64N/A
associate-+r+N/A
lower-+.f64N/A
+-commutativeN/A
*-commutativeN/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 (fma x 0.5 (fma (- (log z) z) y y)))
double code(double x, double y, double z) {
return fma(x, 0.5, fma((log(z) - z), y, y));
}
function code(x, y, z) return fma(x, 0.5, fma(Float64(log(z) - z), y, y)) end
code[x_, y_, z_] := N[(x * 0.5 + N[(N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] * y + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, 0.5, \mathsf{fma}\left(\log z - z, y, y\right)\right)
\end{array}
Initial program 99.9%
Taylor expanded in z around 0
+-commutativeN/A
associate-+l+N/A
mul-1-negN/A
unsub-negN/A
distribute-lft-out--N/A
*-commutativeN/A
lower-fma.f64N/A
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.f6499.9
Applied rewrites99.9%
(FPCore (x y z) :precision binary64 (if (<= y 6.4e+268) (fma (- z) y (* x 0.5)) (fma (log z) y y)))
double code(double x, double y, double z) {
double tmp;
if (y <= 6.4e+268) {
tmp = fma(-z, y, (x * 0.5));
} else {
tmp = fma(log(z), y, y);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= 6.4e+268) tmp = fma(Float64(-z), y, Float64(x * 0.5)); else tmp = fma(log(z), y, y); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, 6.4e+268], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision], N[(N[Log[z], $MachinePrecision] * y + y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 6.4 \cdot 10^{+268}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\log z, y, y\right)\\
\end{array}
\end{array}
if y < 6.3999999999999998e268Initial program 99.9%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.9
lift-+.f64N/A
+-commutativeN/A
lift--.f64N/A
associate-+r-N/A
lower--.f64N/A
lower-+.f6499.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6477.5
Applied rewrites77.5%
if 6.3999999999999998e268 < y Initial program 99.7%
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.f6499.7
Applied rewrites99.7%
Taylor expanded in z around 0
Applied rewrites74.4%
Final simplification77.3%
(FPCore (x y z) :precision binary64 (if (<= z 2050000000.0) (* x 0.5) (fma (- z) y y)))
double code(double x, double y, double z) {
double tmp;
if (z <= 2050000000.0) {
tmp = x * 0.5;
} else {
tmp = fma(-z, y, y);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 2050000000.0) tmp = Float64(x * 0.5); else tmp = fma(Float64(-z), y, y); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 2050000000.0], N[(x * 0.5), $MachinePrecision], N[((-z) * y + y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 2050000000:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, y\right)\\
\end{array}
\end{array}
if z < 2.05e9Initial program 99.8%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f6455.6
Applied rewrites55.6%
if 2.05e9 < z Initial program 100.0%
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.f6478.0
Applied rewrites78.0%
Taylor expanded in z around inf
Applied rewrites78.0%
(FPCore (x y z) :precision binary64 (if (<= z 2050000000.0) (* x 0.5) (* (- z) y)))
double code(double x, double y, double z) {
double tmp;
if (z <= 2050000000.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 <= 2050000000.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 <= 2050000000.0) {
tmp = x * 0.5;
} else {
tmp = -z * y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 2050000000.0: tmp = x * 0.5 else: tmp = -z * y return tmp
function code(x, y, z) tmp = 0.0 if (z <= 2050000000.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 <= 2050000000.0) tmp = x * 0.5; else tmp = -z * y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 2050000000.0], N[(x * 0.5), $MachinePrecision], N[((-z) * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 2050000000:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\left(-z\right) \cdot y\\
\end{array}
\end{array}
if z < 2.05e9Initial program 99.8%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f6455.6
Applied rewrites55.6%
if 2.05e9 < 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.f6478.0
Applied rewrites78.0%
(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
*-commutativeN/A
lower-fma.f6499.9
lift-+.f64N/A
+-commutativeN/A
lift--.f64N/A
associate-+r-N/A
lower--.f64N/A
lower-+.f6499.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.9
Applied rewrites99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6475.7
Applied rewrites75.7%
Final simplification75.7%
(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 y around 0
*-commutativeN/A
lower-*.f6440.8
Applied rewrites40.8%
(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 2024244
(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)))))