
(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 (- (+ (log z) 1.0) z) y (* 0.5 x)))
double code(double x, double y, double z) {
return fma(((log(z) + 1.0) - z), y, (0.5 * x));
}
function code(x, y, z) return fma(Float64(Float64(log(z) + 1.0) - z), y, Float64(0.5 * x)) end
code[x_, y_, z_] := N[(N[(N[(N[Log[z], $MachinePrecision] + 1.0), $MachinePrecision] - z), $MachinePrecision] * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\left(\log z + 1\right) - z, y, 0.5 \cdot x\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%
(FPCore (x y z) :precision binary64 (let* ((t_0 (* y (+ (- 1.0 z) (log z))))) (if (or (<= t_0 -3e+90) (not (<= t_0 1e+33))) (* (- z) y) (* 0.5 x))))
double code(double x, double y, double z) {
double t_0 = y * ((1.0 - z) + log(z));
double tmp;
if ((t_0 <= -3e+90) || !(t_0 <= 1e+33)) {
tmp = -z * y;
} else {
tmp = 0.5 * x;
}
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) :: tmp
t_0 = y * ((1.0d0 - z) + log(z))
if ((t_0 <= (-3d+90)) .or. (.not. (t_0 <= 1d+33))) then
tmp = -z * y
else
tmp = 0.5d0 * x
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = y * ((1.0 - z) + Math.log(z));
double tmp;
if ((t_0 <= -3e+90) || !(t_0 <= 1e+33)) {
tmp = -z * y;
} else {
tmp = 0.5 * x;
}
return tmp;
}
def code(x, y, z): t_0 = y * ((1.0 - z) + math.log(z)) tmp = 0 if (t_0 <= -3e+90) or not (t_0 <= 1e+33): tmp = -z * y else: tmp = 0.5 * x return tmp
function code(x, y, z) t_0 = Float64(y * Float64(Float64(1.0 - z) + log(z))) tmp = 0.0 if ((t_0 <= -3e+90) || !(t_0 <= 1e+33)) tmp = Float64(Float64(-z) * y); else tmp = Float64(0.5 * x); end return tmp end
function tmp_2 = code(x, y, z) t_0 = y * ((1.0 - z) + log(z)); tmp = 0.0; if ((t_0 <= -3e+90) || ~((t_0 <= 1e+33))) tmp = -z * y; else tmp = 0.5 * x; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(y * N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, -3e+90], N[Not[LessEqual[t$95$0, 1e+33]], $MachinePrecision]], N[((-z) * y), $MachinePrecision], N[(0.5 * x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y \cdot \left(\left(1 - z\right) + \log z\right)\\
\mathbf{if}\;t\_0 \leq -3 \cdot 10^{+90} \lor \neg \left(t\_0 \leq 10^{+33}\right):\\
\;\;\;\;\left(-z\right) \cdot y\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot x\\
\end{array}
\end{array}
if (*.f64 y (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z))) < -2.99999999999999979e90 or 9.9999999999999995e32 < (*.f64 y (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z))) Initial program 99.8%
Taylor expanded in z around inf
mul-1-negN/A
*-commutativeN/A
distribute-lft-neg-inN/A
lower-*.f64N/A
lower-neg.f6453.0
Applied rewrites53.0%
if -2.99999999999999979e90 < (*.f64 y (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z))) < 9.9999999999999995e32Initial 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 x around inf
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
remove-double-negN/A
log-recN/A
mul-1-negN/A
lower--.f64N/A
+-commutativeN/A
mul-1-negN/A
log-recN/A
remove-double-negN/A
lower-+.f64N/A
lower-log.f64N/A
lower-/.f6497.5
Applied rewrites97.5%
Taylor expanded in x around inf
Applied rewrites65.8%
Final simplification59.3%
(FPCore (x y z) :precision binary64 (if (or (<= (* x 0.5) -2e+62) (not (<= (* x 0.5) 2e-82))) (fma (- z) y (* 0.5 x)) (fma (- (log z) z) y y)))
double code(double x, double y, double z) {
double tmp;
if (((x * 0.5) <= -2e+62) || !((x * 0.5) <= 2e-82)) {
tmp = fma(-z, y, (0.5 * x));
} else {
tmp = fma((log(z) - z), y, y);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((Float64(x * 0.5) <= -2e+62) || !(Float64(x * 0.5) <= 2e-82)) tmp = fma(Float64(-z), y, Float64(0.5 * x)); else tmp = fma(Float64(log(z) - z), y, y); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[N[(x * 0.5), $MachinePrecision], -2e+62], N[Not[LessEqual[N[(x * 0.5), $MachinePrecision], 2e-82]], $MachinePrecision]], N[((-z) * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision], N[(N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] * y + y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot 0.5 \leq -2 \cdot 10^{+62} \lor \neg \left(x \cdot 0.5 \leq 2 \cdot 10^{-82}\right):\\
\;\;\;\;\mathsf{fma}\left(-z, y, 0.5 \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\log z - z, y, y\right)\\
\end{array}
\end{array}
if (*.f64 x #s(literal 1/2 binary64)) < -2.00000000000000007e62 or 2e-82 < (*.f64 x #s(literal 1/2 binary64)) Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6485.7
Applied rewrites85.7%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
lower-fma.f6485.7
Applied rewrites85.7%
if -2.00000000000000007e62 < (*.f64 x #s(literal 1/2 binary64)) < 2e-82Initial program 99.7%
Taylor expanded in x around 0
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.f6486.9
Applied rewrites86.9%
Final simplification86.3%
(FPCore (x y z) :precision binary64 (if (<= z 0.28) (fma (+ (log z) 1.0) y (* 0.5 x)) (fma (- z) y (* 0.5 x))))
double code(double x, double y, double z) {
double tmp;
if (z <= 0.28) {
tmp = fma((log(z) + 1.0), y, (0.5 * x));
} else {
tmp = fma(-z, y, (0.5 * x));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 0.28) tmp = fma(Float64(log(z) + 1.0), y, Float64(0.5 * x)); else tmp = fma(Float64(-z), y, Float64(0.5 * x)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 0.28], N[(N[(N[Log[z], $MachinePrecision] + 1.0), $MachinePrecision] * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision], N[((-z) * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 0.28:\\
\;\;\;\;\mathsf{fma}\left(\log z + 1, y, 0.5 \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, 0.5 \cdot x\right)\\
\end{array}
\end{array}
if z < 0.28000000000000003Initial program 99.7%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.7
lift-+.f64N/A
+-commutativeN/A
lift--.f64N/A
associate-+r-N/A
lower--.f64N/A
lower-+.f6499.7
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.7
Applied rewrites99.7%
Taylor expanded in z around 0
+-commutativeN/A
lower-+.f64N/A
lower-log.f6498.8
Applied rewrites98.8%
if 0.28000000000000003 < z Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6498.2
Applied rewrites98.2%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
lower-fma.f6498.2
Applied rewrites98.2%
(FPCore (x y z) :precision binary64 (if (<= z 0.28) (fma 0.5 x (fma (log z) y y)) (fma (- z) y (* 0.5 x))))
double code(double x, double y, double z) {
double tmp;
if (z <= 0.28) {
tmp = fma(0.5, x, fma(log(z), y, y));
} else {
tmp = fma(-z, y, (0.5 * x));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 0.28) tmp = fma(0.5, x, fma(log(z), y, y)); else tmp = fma(Float64(-z), y, Float64(0.5 * x)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 0.28], N[(0.5 * x + N[(N[Log[z], $MachinePrecision] * y + y), $MachinePrecision]), $MachinePrecision], N[((-z) * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 0.28:\\
\;\;\;\;\mathsf{fma}\left(0.5, x, \mathsf{fma}\left(\log z, y, y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, 0.5 \cdot x\right)\\
\end{array}
\end{array}
if z < 0.28000000000000003Initial program 99.7%
Taylor expanded in z around 0
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.28000000000000003 < z Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6498.2
Applied rewrites98.2%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
lower-fma.f6498.2
Applied rewrites98.2%
(FPCore (x y z) :precision binary64 (fma (- (log z) z) y (fma 0.5 x y)))
double code(double x, double y, double z) {
return fma((log(z) - z), y, fma(0.5, x, y));
}
function code(x, y, z) return fma(Float64(log(z) - z), y, fma(0.5, x, y)) end
code[x_, y_, z_] := N[(N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] * y + N[(0.5 * x + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\log z - z, y, \mathsf{fma}\left(0.5, x, y\right)\right)
\end{array}
Initial program 99.8%
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
*-commutativeN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64N/A
lower-log.f64N/A
lower-fma.f6499.8
Applied rewrites99.8%
(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.8%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6470.5
Applied rewrites70.5%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
lower-fma.f6470.5
Applied rewrites70.5%
(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.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%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
remove-double-negN/A
log-recN/A
mul-1-negN/A
lower--.f64N/A
+-commutativeN/A
mul-1-negN/A
log-recN/A
remove-double-negN/A
lower-+.f64N/A
lower-log.f64N/A
lower-/.f6488.4
Applied rewrites88.4%
Taylor expanded in x around inf
Applied rewrites38.0%
Final simplification38.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 2024317
(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)))))