# HG changeset patch # User Markus Mützel # Date 1638452860 -3600 # Node ID e22183b892c6b85bdd8c2858b309b5e5f86028e7 # Parent 1fda8d2229d79bfe90a6db046d13492b63473c57# Parent 391b35ef8b24fb811533ea360900cf1b6e2a27d0 maint: merge stable to default. diff -r 1fda8d2229d7 -r e22183b892c6 scripts/specfun/nchoosek.m --- a/scripts/specfun/nchoosek.m Wed Dec 01 21:49:21 2021 +0100 +++ b/scripts/specfun/nchoosek.m Thu Dec 02 14:47:40 2021 +0100 @@ -121,11 +121,11 @@ ## Since Odd*Even is guaranteed to be Even, also take out a factor ## of 2 from numerator and denominator. if (rem (k, 2)) # k is odd - numer = [(v-k+1:v-(k+1)/2) .* (v-1:-1:v-(k-1)/2) / 2, v]; - denom = [(1:k/2) .* (k-1:-1:(k+1)/2) / 2, k]; + numer = [((v-k+1:v-(k+1)/2) .* (v-1:-1:v-(k-1)/2)) / 2, v]; + denom = [((1:(k-1)/2) .* (k-1:-1:(k+1)/2)) / 2, k]; else # k is even - numer = (v-k+1:v-k/2) .* (v:-1:v-k/2+1) / 2; - denom = (1:k/2) .* (k:-1:k/2+1) / 2; + numer = ((v-k+1:v-k/2) .* (v:-1:v-k/2+1)) / 2; + denom = ((1:k/2) .* (k:-1:k/2+1)) / 2; endif ## Remove common factors from numerator and denominator @@ -140,9 +140,13 @@ numer = numer(numer > 1); until (isempty (denom)) - C = prod (numer); - if (C > flintmax) - warning ("nchoosek: possible loss of precision"); + C = prod (numer, "native"); + if (isfloat (C) && C > flintmax (C)) + warning ("Octave:nchoosek:large-output-float", ... + "nchoosek: possible loss of precision"); + elseif (isinteger (C) && C == intmax (C)) + warning ("Octave:nchoosek:large-output-integer", ... + "nchoosek: result may have saturated at intmax"); endif elseif (k == 0) C = v(zeros (1, 0)); # Return 1x0 object for Matlab compatibility @@ -269,6 +273,11 @@ %! assert (isstruct (x)); %! assert (fieldnames (x), {"a"; "b"}); +%!test <61565> +%! x = nchoosek (uint8 (10), uint8 (5)); +%! assert (x, uint8 (252)); +%! assert (class (x), "uint8"); + ## Test input validation %!error nchoosek () %!error nchoosek (1) @@ -282,3 +291,4 @@ %!error nchoosek (-100, 45) %!error nchoosek (100.5, 45) %!warning nchoosek (100, 45); +%!warning nchoosek (uint64 (80), uint64 (40));