
(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 (+ (* 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}
Initial program 99.9%
Final simplification99.9%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (+ y (* y (log z)))) (t_1 (- (* x 0.5) (* y z))))
(if (<= z 1.7e-190)
t_0
(if (<= z 7.4e-146)
t_1
(if (<= z 4.5e-89)
t_0
(if (<= z 2.6e-56)
t_1
(if (<= z 0.05) t_0 (fma (- z) y (* x 0.5)))))))))
double code(double x, double y, double z) {
double t_0 = y + (y * log(z));
double t_1 = (x * 0.5) - (y * z);
double tmp;
if (z <= 1.7e-190) {
tmp = t_0;
} else if (z <= 7.4e-146) {
tmp = t_1;
} else if (z <= 4.5e-89) {
tmp = t_0;
} else if (z <= 2.6e-56) {
tmp = t_1;
} else if (z <= 0.05) {
tmp = t_0;
} else {
tmp = fma(-z, y, (x * 0.5));
}
return tmp;
}
function code(x, y, z) t_0 = Float64(y + Float64(y * log(z))) t_1 = Float64(Float64(x * 0.5) - Float64(y * z)) tmp = 0.0 if (z <= 1.7e-190) tmp = t_0; elseif (z <= 7.4e-146) tmp = t_1; elseif (z <= 4.5e-89) tmp = t_0; elseif (z <= 2.6e-56) tmp = t_1; elseif (z <= 0.05) tmp = t_0; else tmp = fma(Float64(-z), y, Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(y + N[(y * N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, 1.7e-190], t$95$0, If[LessEqual[z, 7.4e-146], t$95$1, If[LessEqual[z, 4.5e-89], t$95$0, If[LessEqual[z, 2.6e-56], t$95$1, If[LessEqual[z, 0.05], t$95$0, N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y + y \cdot \log z\\
t_1 := x \cdot 0.5 - y \cdot z\\
\mathbf{if}\;z \leq 1.7 \cdot 10^{-190}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;z \leq 7.4 \cdot 10^{-146}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;z \leq 4.5 \cdot 10^{-89}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;z \leq 2.6 \cdot 10^{-56}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;z \leq 0.05:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 1.69999999999999991e-190 or 7.39999999999999973e-146 < z < 4.4999999999999999e-89 or 2.59999999999999997e-56 < z < 0.050000000000000003Initial program 99.8%
sub-neg99.8%
associate-+l+99.7%
distribute-lft-in99.6%
*-rgt-identity99.6%
associate-+r+99.6%
fma-def99.6%
+-commutative99.6%
unsub-neg99.6%
Simplified99.6%
Taylor expanded in x around 0 72.1%
Taylor expanded in z around 0 69.5%
if 1.69999999999999991e-190 < z < 7.39999999999999973e-146 or 4.4999999999999999e-89 < z < 2.59999999999999997e-56Initial program 99.8%
Taylor expanded in z around inf 68.4%
mul-1-neg68.4%
distribute-rgt-neg-out68.4%
Simplified68.4%
if 0.050000000000000003 < 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.4%
neg-mul-199.4%
Simplified99.4%
Final simplification85.1%
(FPCore (x y z)
:precision binary64
(if (or (<= z 1.65e-190)
(and (not (<= z 2.25e-147))
(or (<= z 7.5e-89) (and (not (<= z 4e-56)) (<= z 0.05)))))
(+ y (* y (log z)))
(- (* x 0.5) (* y z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= 1.65e-190) || (!(z <= 2.25e-147) && ((z <= 7.5e-89) || (!(z <= 4e-56) && (z <= 0.05))))) {
tmp = y + (y * log(z));
} else {
tmp = (x * 0.5) - (y * z);
}
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 <= 1.65d-190) .or. (.not. (z <= 2.25d-147)) .and. (z <= 7.5d-89) .or. (.not. (z <= 4d-56)) .and. (z <= 0.05d0)) then
tmp = y + (y * log(z))
else
tmp = (x * 0.5d0) - (y * z)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z <= 1.65e-190) || (!(z <= 2.25e-147) && ((z <= 7.5e-89) || (!(z <= 4e-56) && (z <= 0.05))))) {
tmp = y + (y * Math.log(z));
} else {
tmp = (x * 0.5) - (y * z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= 1.65e-190) or (not (z <= 2.25e-147) and ((z <= 7.5e-89) or (not (z <= 4e-56) and (z <= 0.05)))): tmp = y + (y * math.log(z)) else: tmp = (x * 0.5) - (y * z) return tmp
function code(x, y, z) tmp = 0.0 if ((z <= 1.65e-190) || (!(z <= 2.25e-147) && ((z <= 7.5e-89) || (!(z <= 4e-56) && (z <= 0.05))))) tmp = Float64(y + Float64(y * log(z))); else tmp = Float64(Float64(x * 0.5) - Float64(y * z)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= 1.65e-190) || (~((z <= 2.25e-147)) && ((z <= 7.5e-89) || (~((z <= 4e-56)) && (z <= 0.05))))) tmp = y + (y * log(z)); else tmp = (x * 0.5) - (y * z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, 1.65e-190], And[N[Not[LessEqual[z, 2.25e-147]], $MachinePrecision], Or[LessEqual[z, 7.5e-89], And[N[Not[LessEqual[z, 4e-56]], $MachinePrecision], LessEqual[z, 0.05]]]]], N[(y + N[(y * N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 1.65 \cdot 10^{-190} \lor \neg \left(z \leq 2.25 \cdot 10^{-147}\right) \land \left(z \leq 7.5 \cdot 10^{-89} \lor \neg \left(z \leq 4 \cdot 10^{-56}\right) \land z \leq 0.05\right):\\
\;\;\;\;y + y \cdot \log z\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\end{array}
\end{array}
if z < 1.65000000000000009e-190 or 2.24999999999999986e-147 < z < 7.4999999999999999e-89 or 4.0000000000000002e-56 < z < 0.050000000000000003Initial program 99.8%
sub-neg99.8%
associate-+l+99.7%
distribute-lft-in99.6%
*-rgt-identity99.6%
associate-+r+99.6%
fma-def99.6%
+-commutative99.6%
unsub-neg99.6%
Simplified99.6%
Taylor expanded in x around 0 72.1%
Taylor expanded in z around 0 69.5%
if 1.65000000000000009e-190 < z < 2.24999999999999986e-147 or 7.4999999999999999e-89 < z < 4.0000000000000002e-56 or 0.050000000000000003 < z Initial program 99.9%
Taylor expanded in z around inf 92.7%
mul-1-neg92.7%
distribute-rgt-neg-out92.7%
Simplified92.7%
Final simplification85.1%
(FPCore (x y z) :precision binary64 (if (or (<= y -6.2e+30) (not (<= y 35.0))) (* y (+ (- 1.0 z) (log z))) (fma (- z) y (* x 0.5))))
double code(double x, double y, double z) {
double tmp;
if ((y <= -6.2e+30) || !(y <= 35.0)) {
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 ((y <= -6.2e+30) || !(y <= 35.0)) tmp = Float64(y * Float64(Float64(1.0 - z) + log(z))); else tmp = fma(Float64(-z), y, Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[y, -6.2e+30], N[Not[LessEqual[y, 35.0]], $MachinePrecision]], N[(y * N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -6.2 \cdot 10^{+30} \lor \neg \left(y \leq 35\right):\\
\;\;\;\;y \cdot \left(\left(1 - z\right) + \log z\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\end{array}
\end{array}
if y < -6.1999999999999995e30 or 35 < y Initial program 99.8%
+-commutative99.8%
*-commutative99.8%
fma-def99.8%
sub-neg99.8%
associate-+l+99.8%
+-commutative99.8%
sub-neg99.8%
Applied egg-rr99.8%
Taylor expanded in y around inf 90.2%
+-commutative90.2%
associate--l+90.2%
Simplified90.2%
if -6.1999999999999995e30 < y < 35Initial 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 85.8%
neg-mul-185.8%
Simplified85.8%
Final simplification87.8%
(FPCore (x y z) :precision binary64 (if (<= z 0.062) (+ (* x 0.5) (+ y (* y (log z)))) (fma (- z) y (* x 0.5))))
double code(double x, double y, double z) {
double tmp;
if (z <= 0.062) {
tmp = (x * 0.5) + (y + (y * log(z)));
} else {
tmp = fma(-z, y, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 0.062) tmp = Float64(Float64(x * 0.5) + Float64(y + Float64(y * log(z)))); else tmp = fma(Float64(-z), y, Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 0.062], N[(N[(x * 0.5), $MachinePrecision] + N[(y + N[(y * N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[((-z) * y + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 0.062:\\
\;\;\;\;x \cdot 0.5 + \left(y + y \cdot \log z\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 0.062Initial program 99.8%
Taylor expanded in z around 0 97.9%
*-commutative97.9%
distribute-lft-in97.8%
*-rgt-identity97.8%
Simplified97.8%
if 0.062 < 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.4%
neg-mul-199.4%
Simplified99.4%
Final simplification98.7%
(FPCore (x y z) :precision binary64 (if (or (<= z 2.1e+58) (and (not (<= z 6.6e+187)) (<= z 2.7e+196))) (* x 0.5) (* y (- z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= 2.1e+58) || (!(z <= 6.6e+187) && (z <= 2.7e+196))) {
tmp = x * 0.5;
} else {
tmp = y * -z;
}
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.1d+58) .or. (.not. (z <= 6.6d+187)) .and. (z <= 2.7d+196)) then
tmp = x * 0.5d0
else
tmp = y * -z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z <= 2.1e+58) || (!(z <= 6.6e+187) && (z <= 2.7e+196))) {
tmp = x * 0.5;
} else {
tmp = y * -z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= 2.1e+58) or (not (z <= 6.6e+187) and (z <= 2.7e+196)): tmp = x * 0.5 else: tmp = y * -z return tmp
function code(x, y, z) tmp = 0.0 if ((z <= 2.1e+58) || (!(z <= 6.6e+187) && (z <= 2.7e+196))) tmp = Float64(x * 0.5); else tmp = Float64(y * Float64(-z)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= 2.1e+58) || (~((z <= 6.6e+187)) && (z <= 2.7e+196))) tmp = x * 0.5; else tmp = y * -z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, 2.1e+58], And[N[Not[LessEqual[z, 6.6e+187]], $MachinePrecision], LessEqual[z, 2.7e+196]]], N[(x * 0.5), $MachinePrecision], N[(y * (-z)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 2.1 \cdot 10^{+58} \lor \neg \left(z \leq 6.6 \cdot 10^{+187}\right) \land z \leq 2.7 \cdot 10^{+196}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(-z\right)\\
\end{array}
\end{array}
if z < 2.10000000000000012e58 or 6.6000000000000003e187 < z < 2.69999999999999995e196Initial program 99.8%
Taylor expanded in x around inf 46.8%
if 2.10000000000000012e58 < z < 6.6000000000000003e187 or 2.69999999999999995e196 < 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.2%
mul-1-neg77.2%
*-commutative77.2%
distribute-rgt-neg-in77.2%
Simplified77.2%
Final simplification58.6%
(FPCore (x y z) :precision binary64 (- (* x 0.5) (* y z)))
double code(double x, double y, double z) {
return (x * 0.5) - (y * 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 * z)
end function
public static double code(double x, double y, double z) {
return (x * 0.5) - (y * z);
}
def code(x, y, z): return (x * 0.5) - (y * z)
function code(x, y, z) return Float64(Float64(x * 0.5) - Float64(y * z)) end
function tmp = code(x, y, z) tmp = (x * 0.5) - (y * z); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 - y \cdot z
\end{array}
Initial program 99.9%
Taylor expanded in z around inf 72.5%
mul-1-neg72.5%
distribute-rgt-neg-out72.5%
Simplified72.5%
Final simplification72.5%
(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 38.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 2023278
(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)))))