
(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 (log (* z E))) (* y z))))
double code(double x, double y, double z) {
return (x * 0.5) + ((y * log((z * ((double) M_E)))) - (y * z));
}
public static double code(double x, double y, double z) {
return (x * 0.5) + ((y * Math.log((z * Math.E))) - (y * z));
}
def code(x, y, z): return (x * 0.5) + ((y * math.log((z * math.e))) - (y * z))
function code(x, y, z) return Float64(Float64(x * 0.5) + Float64(Float64(y * log(Float64(z * exp(1)))) - Float64(y * z))) end
function tmp = code(x, y, z) tmp = (x * 0.5) + ((y * log((z * 2.71828182845904523536))) - (y * z)); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] + N[(N[(y * N[Log[N[(z * E), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 + \left(y \cdot \log \left(z \cdot e\right) - y \cdot z\right)
\end{array}
Initial program 99.8%
Taylor expanded in z around 0 99.9%
+-commutative99.9%
add-log-exp99.9%
exp-sum99.9%
add-exp-log99.9%
Applied egg-rr99.9%
exp-1-e99.9%
Simplified99.9%
Final simplification99.9%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (* y (+ 1.0 (log z)))))
(if (<= z 6e-177)
t_0
(if (<= z 4.8e-73)
(- (* x 0.5) (* y z))
(if (<= z 1e-41) t_0 (fma y (- z) (* x 0.5)))))))
double code(double x, double y, double z) {
double t_0 = y * (1.0 + log(z));
double tmp;
if (z <= 6e-177) {
tmp = t_0;
} else if (z <= 4.8e-73) {
tmp = (x * 0.5) - (y * z);
} else if (z <= 1e-41) {
tmp = t_0;
} else {
tmp = fma(y, -z, (x * 0.5));
}
return tmp;
}
function code(x, y, z) t_0 = Float64(y * Float64(1.0 + log(z))) tmp = 0.0 if (z <= 6e-177) tmp = t_0; elseif (z <= 4.8e-73) tmp = Float64(Float64(x * 0.5) - Float64(y * z)); elseif (z <= 1e-41) tmp = t_0; else tmp = fma(y, Float64(-z), Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(y * N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, 6e-177], t$95$0, If[LessEqual[z, 4.8e-73], N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision], If[LessEqual[z, 1e-41], t$95$0, N[(y * (-z) + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y \cdot \left(1 + \log z\right)\\
\mathbf{if}\;z \leq 6 \cdot 10^{-177}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 4.8 \cdot 10^{-73}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\mathbf{elif}\;z \leq 10^{-41}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, -z, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 6.00000000000000015e-177 or 4.80000000000000011e-73 < z < 1.00000000000000001e-41Initial program 99.7%
Taylor expanded in z around 0 99.7%
Taylor expanded in x around 0 61.0%
if 6.00000000000000015e-177 < z < 4.80000000000000011e-73Initial program 99.8%
Taylor expanded in z around inf 68.5%
associate-*r*68.5%
mul-1-neg68.5%
Simplified68.5%
Taylor expanded in x around 0 68.5%
+-commutative68.5%
associate-*r*68.5%
neg-mul-168.5%
*-commutative68.5%
*-commutative68.5%
distribute-rgt-neg-out68.5%
unsub-neg68.5%
*-commutative68.5%
*-commutative68.5%
Simplified68.5%
if 1.00000000000000001e-41 < z Initial program 99.9%
+-commutative99.9%
fma-define99.9%
Simplified99.9%
Taylor expanded in z around inf 95.2%
mul-1-neg95.2%
Simplified95.2%
Final simplification81.1%
(FPCore (x y z) :precision binary64 (if (or (<= z 8.3e-178) (and (not (<= z 1.06e-73)) (<= z 2.7e-44))) (* y (+ 1.0 (log z))) (- (* x 0.5) (* y z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= 8.3e-178) || (!(z <= 1.06e-73) && (z <= 2.7e-44))) {
tmp = y * (1.0 + 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 <= 8.3d-178) .or. (.not. (z <= 1.06d-73)) .and. (z <= 2.7d-44)) then
tmp = y * (1.0d0 + 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 <= 8.3e-178) || (!(z <= 1.06e-73) && (z <= 2.7e-44))) {
tmp = y * (1.0 + Math.log(z));
} else {
tmp = (x * 0.5) - (y * z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= 8.3e-178) or (not (z <= 1.06e-73) and (z <= 2.7e-44)): tmp = y * (1.0 + math.log(z)) else: tmp = (x * 0.5) - (y * z) return tmp
function code(x, y, z) tmp = 0.0 if ((z <= 8.3e-178) || (!(z <= 1.06e-73) && (z <= 2.7e-44))) tmp = Float64(y * Float64(1.0 + 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 <= 8.3e-178) || (~((z <= 1.06e-73)) && (z <= 2.7e-44))) tmp = y * (1.0 + log(z)); else tmp = (x * 0.5) - (y * z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, 8.3e-178], And[N[Not[LessEqual[z, 1.06e-73]], $MachinePrecision], LessEqual[z, 2.7e-44]]], N[(y * N[(1.0 + 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 8.3 \cdot 10^{-178} \lor \neg \left(z \leq 1.06 \cdot 10^{-73}\right) \land z \leq 2.7 \cdot 10^{-44}:\\
\;\;\;\;y \cdot \left(1 + \log z\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\end{array}
\end{array}
if z < 8.2999999999999999e-178 or 1.05999999999999997e-73 < z < 2.6999999999999999e-44Initial program 99.7%
Taylor expanded in z around 0 99.7%
Taylor expanded in x around 0 61.0%
if 8.2999999999999999e-178 < z < 1.05999999999999997e-73 or 2.6999999999999999e-44 < z Initial program 99.9%
Taylor expanded in z around inf 89.4%
associate-*r*89.4%
mul-1-neg89.4%
Simplified89.4%
Taylor expanded in x around 0 89.4%
+-commutative89.4%
associate-*r*89.4%
neg-mul-189.4%
*-commutative89.4%
*-commutative89.4%
distribute-rgt-neg-out89.4%
unsub-neg89.4%
*-commutative89.4%
*-commutative89.4%
Simplified89.4%
Final simplification81.1%
(FPCore (x y z) :precision binary64 (if (or (<= y -9e+33) (not (<= y 2.55e-52))) (* y (- (+ 1.0 (log z)) z)) (fma y (- z) (* x 0.5))))
double code(double x, double y, double z) {
double tmp;
if ((y <= -9e+33) || !(y <= 2.55e-52)) {
tmp = y * ((1.0 + log(z)) - z);
} else {
tmp = fma(y, -z, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((y <= -9e+33) || !(y <= 2.55e-52)) tmp = Float64(y * Float64(Float64(1.0 + log(z)) - z)); else tmp = fma(y, Float64(-z), Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[y, -9e+33], N[Not[LessEqual[y, 2.55e-52]], $MachinePrecision]], N[(y * N[(N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision]), $MachinePrecision], N[(y * (-z) + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -9 \cdot 10^{+33} \lor \neg \left(y \leq 2.55 \cdot 10^{-52}\right):\\
\;\;\;\;y \cdot \left(\left(1 + \log z\right) - z\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, -z, x \cdot 0.5\right)\\
\end{array}
\end{array}
if y < -9.0000000000000001e33 or 2.54999999999999995e-52 < y Initial program 99.7%
Taylor expanded in x around inf 81.4%
associate-/l*80.5%
+-commutative80.5%
associate--l+80.5%
Simplified80.5%
Taylor expanded in x around 0 88.5%
if -9.0000000000000001e33 < y < 2.54999999999999995e-52Initial program 99.9%
+-commutative99.9%
fma-define100.0%
Simplified100.0%
Taylor expanded in z around inf 87.9%
mul-1-neg87.9%
Simplified87.9%
Final simplification88.2%
(FPCore (x y z) :precision binary64 (if (<= z 8.4e-7) (+ (* x 0.5) (* y (log (* z E)))) (fma y (- z) (* x 0.5))))
double code(double x, double y, double z) {
double tmp;
if (z <= 8.4e-7) {
tmp = (x * 0.5) + (y * log((z * ((double) M_E))));
} else {
tmp = fma(y, -z, (x * 0.5));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 8.4e-7) tmp = Float64(Float64(x * 0.5) + Float64(y * log(Float64(z * exp(1))))); else tmp = fma(y, Float64(-z), Float64(x * 0.5)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 8.4e-7], N[(N[(x * 0.5), $MachinePrecision] + N[(y * N[Log[N[(z * E), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y * (-z) + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 8.4 \cdot 10^{-7}:\\
\;\;\;\;x \cdot 0.5 + y \cdot \log \left(z \cdot e\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(y, -z, x \cdot 0.5\right)\\
\end{array}
\end{array}
if z < 8.4e-7Initial program 99.7%
Taylor expanded in z around 0 99.4%
+-commutative99.7%
add-log-exp99.7%
exp-sum99.7%
add-exp-log99.7%
Applied egg-rr99.4%
exp-1-e99.7%
Simplified99.4%
if 8.4e-7 < z Initial program 100.0%
+-commutative100.0%
fma-define100.0%
Simplified100.0%
Taylor expanded in z around inf 98.9%
mul-1-neg98.9%
Simplified98.9%
Final simplification99.2%
(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.8%
Final simplification99.8%
(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.8%
Taylor expanded in z around inf 75.2%
associate-*r*75.2%
mul-1-neg75.2%
Simplified75.2%
Taylor expanded in x around 0 75.2%
+-commutative75.2%
associate-*r*75.2%
neg-mul-175.2%
*-commutative75.2%
*-commutative75.2%
distribute-rgt-neg-out75.2%
unsub-neg75.2%
*-commutative75.2%
*-commutative75.2%
Simplified75.2%
Final simplification75.2%
(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.8%
Taylor expanded in x around inf 40.1%
Final simplification40.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 2024071
(FPCore (x y z)
:name "System.Random.MWC.Distributions:gamma from mwc-random-0.13.3.2"
:precision binary64
:alt
(- (+ y (* 0.5 x)) (* y (- z (log z))))
(+ (* x 0.5) (* y (+ (- 1.0 z) (log z)))))