
(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 (- 1.0 (- z (log z))) y (* x 0.5)))
double code(double x, double y, double z) {
return fma((1.0 - (z - log(z))), y, (x * 0.5));
}
function code(x, y, z) return fma(Float64(1.0 - Float64(z - log(z))), y, Float64(x * 0.5)) end
code[x_, y_, z_] := N[(N[(1.0 - N[(z - N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(1 - \left(z - \log z\right), y, x \cdot 0.5\right)
\end{array}
Initial program 99.9%
+-commutative99.9%
*-commutative99.9%
fma-def99.9%
sub-neg99.9%
associate-+l+99.9%
+-commutative99.9%
sub-neg99.9%
Applied egg-rr99.9%
Final simplification99.9%
(FPCore (x y z)
:precision binary64
(if (<= (* x 0.5) -4e-19)
(- (* x 0.5) (* z y))
(if (<= (* x 0.5) 1e-62)
(+ y (* y (- (log z) z)))
(fma (- z) y (* x 0.5)))))
double code(double x, double y, double z) {
double tmp;
if ((x * 0.5) <= -4e-19) {
tmp = (x * 0.5) - (z * y);
} else if ((x * 0.5) <= 1e-62) {
tmp = y + (y * (log(z) - z));
} else {
tmp = fma(-z, y, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (Float64(x * 0.5) <= -4e-19) tmp = Float64(Float64(x * 0.5) - Float64(z * y)); elseif (Float64(x * 0.5) <= 1e-62) tmp = Float64(y + Float64(y * Float64(log(z) - z))); 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], -4e-19], N[(N[(x * 0.5), $MachinePrecision] - N[(z * y), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(x * 0.5), $MachinePrecision], 1e-62], N[(y + N[(y * N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot 0.5 \leq -4 \cdot 10^{-19}:\\
\;\;\;\;x \cdot 0.5 - z \cdot y\\
\mathbf{elif}\;x \cdot 0.5 \leq 10^{-62}:\\
\;\;\;\;y + y \cdot \left(\log z - z\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\end{array}
\end{array}
if (*.f64 x 1/2) < -3.9999999999999999e-19Initial program 100.0%
distribute-lft-in100.0%
Applied egg-rr100.0%
Taylor expanded in z around inf 94.4%
associate-*r*94.4%
neg-mul-194.4%
Simplified94.4%
distribute-lft-neg-out94.4%
unsub-neg94.4%
Applied egg-rr94.4%
if -3.9999999999999999e-19 < (*.f64 x 1/2) < 1e-62Initial program 99.8%
sub-neg99.8%
associate-+l+99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
associate-+r+99.8%
fma-def99.8%
+-commutative99.8%
unsub-neg99.8%
Simplified99.8%
Taylor expanded in x around 0 90.7%
if 1e-62 < (*.f64 x 1/2) Initial program 99.9%
+-commutative99.9%
*-commutative99.9%
fma-def99.9%
sub-neg99.9%
associate-+l+99.9%
+-commutative99.9%
sub-neg99.9%
Applied egg-rr99.9%
Taylor expanded in z around inf 91.6%
mul-1-neg91.6%
Simplified91.6%
Final simplification91.8%
(FPCore (x y z)
:precision binary64
(if (<= (* x 0.5) -4e-19)
(- (* x 0.5) (* z y))
(if (<= (* x 0.5) 1e-62)
(* y (- 1.0 (- z (log z))))
(fma (- z) y (* x 0.5)))))
double code(double x, double y, double z) {
double tmp;
if ((x * 0.5) <= -4e-19) {
tmp = (x * 0.5) - (z * y);
} else if ((x * 0.5) <= 1e-62) {
tmp = y * (1.0 - (z - log(z)));
} else {
tmp = fma(-z, y, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (Float64(x * 0.5) <= -4e-19) tmp = Float64(Float64(x * 0.5) - Float64(z * y)); elseif (Float64(x * 0.5) <= 1e-62) tmp = Float64(y * Float64(1.0 - Float64(z - log(z)))); 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], -4e-19], N[(N[(x * 0.5), $MachinePrecision] - N[(z * y), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(x * 0.5), $MachinePrecision], 1e-62], N[(y * N[(1.0 - N[(z - N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot 0.5 \leq -4 \cdot 10^{-19}:\\
\;\;\;\;x \cdot 0.5 - z \cdot y\\
\mathbf{elif}\;x \cdot 0.5 \leq 10^{-62}:\\
\;\;\;\;y \cdot \left(1 - \left(z - \log z\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\end{array}
\end{array}
if (*.f64 x 1/2) < -3.9999999999999999e-19Initial program 100.0%
distribute-lft-in100.0%
Applied egg-rr100.0%
Taylor expanded in z around inf 94.4%
associate-*r*94.4%
neg-mul-194.4%
Simplified94.4%
distribute-lft-neg-out94.4%
unsub-neg94.4%
Applied egg-rr94.4%
if -3.9999999999999999e-19 < (*.f64 x 1/2) < 1e-62Initial program 99.8%
sub-neg99.8%
associate-+l+99.8%
distribute-lft-in99.8%
*-rgt-identity99.8%
associate-+r+99.8%
fma-def99.8%
+-commutative99.8%
unsub-neg99.8%
Simplified99.8%
Taylor expanded in y around -inf 90.7%
mul-1-neg90.7%
distribute-rgt-neg-in90.7%
sub-neg90.7%
mul-1-neg90.7%
sub-neg90.7%
+-commutative90.7%
distribute-neg-in90.7%
remove-double-neg90.7%
sub-neg90.7%
metadata-eval90.7%
+-commutative90.7%
Simplified90.7%
if 1e-62 < (*.f64 x 1/2) Initial program 99.9%
+-commutative99.9%
*-commutative99.9%
fma-def99.9%
sub-neg99.9%
associate-+l+99.9%
+-commutative99.9%
sub-neg99.9%
Applied egg-rr99.9%
Taylor expanded in z around inf 91.6%
mul-1-neg91.6%
Simplified91.6%
Final simplification91.8%
(FPCore (x y z) :precision binary64 (if (<= z 0.28) (+ (* (log z) y) (+ y (* x 0.5))) (fma (- z) y (* x 0.5))))
double code(double x, double y, double z) {
double tmp;
if (z <= 0.28) {
tmp = (log(z) * y) + (y + (x * 0.5));
} else {
tmp = fma(-z, y, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 0.28) tmp = Float64(Float64(log(z) * y) + Float64(y + Float64(x * 0.5))); else tmp = fma(Float64(-z), y, Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 0.28], N[(N[(N[Log[z], $MachinePrecision] * y), $MachinePrecision] + N[(y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 0.28:\\
\;\;\;\;\log z \cdot y + \left(y + x \cdot 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 0.28000000000000003Initial program 99.7%
sub-neg99.7%
associate-+l+99.7%
distribute-lft-in99.7%
*-rgt-identity99.7%
associate-+r+99.7%
fma-def99.7%
+-commutative99.7%
unsub-neg99.7%
Simplified99.7%
Taylor expanded in z around 0 97.8%
if 0.28000000000000003 < z Initial program 100.0%
+-commutative100.0%
*-commutative100.0%
fma-def100.0%
sub-neg100.0%
associate-+l+100.0%
+-commutative100.0%
sub-neg100.0%
Applied egg-rr100.0%
Taylor expanded in z around inf 99.3%
mul-1-neg99.3%
Simplified99.3%
Final simplification98.7%
(FPCore (x y z) :precision binary64 (+ (* x 0.5) (* y (+ (log z) (- 1.0 z)))))
double code(double x, double y, double z) {
return (x * 0.5) + (y * (log(z) + (1.0 - 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 * (log(z) + (1.0d0 - z)))
end function
public static double code(double x, double y, double z) {
return (x * 0.5) + (y * (Math.log(z) + (1.0 - z)));
}
def code(x, y, z): return (x * 0.5) + (y * (math.log(z) + (1.0 - z)))
function code(x, y, z) return Float64(Float64(x * 0.5) + Float64(y * Float64(log(z) + Float64(1.0 - z)))) end
function tmp = code(x, y, z) tmp = (x * 0.5) + (y * (log(z) + (1.0 - z))); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] + N[(y * N[(N[Log[z], $MachinePrecision] + N[(1.0 - z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 + y \cdot \left(\log z + \left(1 - z\right)\right)
\end{array}
Initial program 99.9%
Final simplification99.9%
(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%
+-commutative99.9%
*-commutative99.9%
fma-def99.9%
sub-neg99.9%
associate-+l+99.9%
+-commutative99.9%
sub-neg99.9%
Applied egg-rr99.9%
Taylor expanded in z around inf 78.1%
mul-1-neg78.1%
Simplified78.1%
Final simplification78.1%
(FPCore (x y z) :precision binary64 (- (* x 0.5) (* z y)))
double code(double x, double y, double z) {
return (x * 0.5) - (z * y);
}
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) - (z * y)
end function
public static double code(double x, double y, double z) {
return (x * 0.5) - (z * y);
}
def code(x, y, z): return (x * 0.5) - (z * y)
function code(x, y, z) return Float64(Float64(x * 0.5) - Float64(z * y)) end
function tmp = code(x, y, z) tmp = (x * 0.5) - (z * y); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] - N[(z * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 - z \cdot y
\end{array}
Initial program 99.9%
distribute-lft-in99.9%
Applied egg-rr99.9%
Taylor expanded in z around inf 78.1%
associate-*r*78.1%
neg-mul-178.1%
Simplified78.1%
distribute-lft-neg-out78.1%
unsub-neg78.1%
Applied egg-rr78.1%
Final simplification78.1%
(FPCore (x y z) :precision binary64 (if (<= z 2.4e+24) (* x 0.5) (* z (- y))))
double code(double x, double y, double z) {
double tmp;
if (z <= 2.4e+24) {
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 <= 2.4d+24) 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 <= 2.4e+24) {
tmp = x * 0.5;
} else {
tmp = z * -y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 2.4e+24: tmp = x * 0.5 else: tmp = z * -y return tmp
function code(x, y, z) tmp = 0.0 if (z <= 2.4e+24) tmp = Float64(x * 0.5); else tmp = Float64(z * Float64(-y)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= 2.4e+24) tmp = x * 0.5; else tmp = z * -y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 2.4e+24], N[(x * 0.5), $MachinePrecision], N[(z * (-y)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 2.4 \cdot 10^{+24}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;z \cdot \left(-y\right)\\
\end{array}
\end{array}
if z < 2.4000000000000001e24Initial program 99.8%
Taylor expanded in x around inf 48.1%
if 2.4000000000000001e24 < z Initial program 100.0%
+-commutative100.0%
*-commutative100.0%
fma-def100.0%
sub-neg100.0%
associate-+l+100.0%
+-commutative100.0%
sub-neg100.0%
Applied egg-rr100.0%
Taylor expanded in z around inf 77.1%
mul-1-neg77.1%
*-commutative77.1%
distribute-rgt-neg-in77.1%
Simplified77.1%
Final simplification63.9%
(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 35.5%
Final simplification35.5%
(FPCore (x y z) :precision binary64 y)
double code(double x, double y, double z) {
return y;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = y
end function
public static double code(double x, double y, double z) {
return y;
}
def code(x, y, z): return y
function code(x, y, z) return y end
function tmp = code(x, y, z) tmp = y; end
code[x_, y_, z_] := y
\begin{array}{l}
\\
y
\end{array}
Initial program 99.9%
sub-neg99.9%
associate-+l+99.9%
distribute-lft-in99.9%
*-rgt-identity99.9%
associate-+r+99.9%
fma-def99.9%
+-commutative99.9%
unsub-neg99.9%
Simplified99.9%
Taylor expanded in y around -inf 66.4%
mul-1-neg66.4%
distribute-rgt-neg-in66.4%
sub-neg66.4%
mul-1-neg66.4%
sub-neg66.4%
+-commutative66.4%
distribute-neg-in66.4%
remove-double-neg66.4%
sub-neg66.4%
metadata-eval66.4%
+-commutative66.4%
Simplified66.4%
Taylor expanded in z around 0 22.7%
*-commutative22.7%
distribute-rgt-in22.7%
*-lft-identity22.7%
Simplified22.7%
*-commutative22.7%
add-cube-cbrt22.4%
pow322.4%
Applied egg-rr22.4%
Taylor expanded in y around 0 1.7%
Final simplification1.7%
(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 2023257
(FPCore (x y z)
:name "System.Random.MWC.Distributions:gamma from mwc-random-0.13.3.2"
:precision binary64
:herbie-target
(- (+ y (* 0.5 x)) (* y (- z (log z))))
(+ (* x 0.5) (* y (+ (- 1.0 z) (log z)))))