On 13/01/2020 6:33 p.m., Wright, Erik Scott wrote:
This struck me as incorrect:

choose(3.999999, 4)
[1] 0.9999979
choose(3.9999999, 4)
[1] 0
choose(4, 4)
[1] 1
choose(4.0000001, 4)
[1] 4
choose(4.000001, 4)
[1] 1.000002

Should base::choose(n, k) check whether n is within machine precision of k and 
return 1?

I don't think that's the solution. The current code checks whether n is within 1e-7 of an integer; if it is and n-k is smaller than k, it computes choose(n, n-k) instead. The problem in your second example is that n-k < 0 which implies the answer should be zero. In the 4th example n-k > 0 but it is not an integer; the code rounds k to an integer, but the transformation to n-k happens after that, so the code ends up working with a non-integer.

I think a solution would be to force n to be an integer if it is very close to one.

I note that the source to lchoose() seems to already do this: it handles your examples nicely.

Duncan Murdoch

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to