changeset 30413:181c44079f6c stable

nchhosek: Support integer type input (bug #61565). * scripts/specfun/nchoosek.m: Support integer type input. Add test.
author Markus Mützel <markus.muetzel@gmx.de>
date Wed, 01 Dec 2021 22:16:33 +0100
parents 0fee9e910d84
children 391b35ef8b24
files scripts/specfun/nchoosek.m
diffstat 1 files changed, 10 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/specfun/nchoosek.m	Tue May 11 13:26:38 2021 -0400
+++ b/scripts/specfun/nchoosek.m	Wed Dec 01 22:16:33 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,7 +140,7 @@
       numer = numer(numer > 1);
     until (isempty (denom))
 
-    C = prod (numer);
+    C = prod (numer, "native");
     if (C > flintmax)
       warning ("nchoosek: possible loss of precision");
     endif
@@ -269,6 +269,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 <Invalid call> nchoosek ()
 %!error <Invalid call> nchoosek (1)