jerry.lewis at biogenidec.com
2008-Mar-19 13:25 UTC
[Rd] choose incorrect for fractional and some negative integer values (PR#10766)
choose(-5,-7) uses integer arguments (as specified in Help) and returns a numeric value that is incorrect. Either the function or the documentation should be fixed. If the function is not fixed, a warning or an error would be helpful. The fact that choose(n,k) usually returns choose(n,round(k,0)) is not obvious from either the output or the documentation. I suggest issuing a warning when k is not an integer; a user might easily expect that choose would coerce both arguments to integers, but only coercing k to an integer is unusual and potentially misleading. The fact that choose(n,k) always rounds .5 up in k (contrary to the round function documentation) is nowhere documented. Help for coerce would lead a reader to expect k to be truncated toward zero (as.integer), instead of rounding it. Jerry [[alternative HTML version deleted]]
Ben Bolker
2008-Mar-19 22:15 UTC
[Rd] choose incorrect for fractional and some negative integer values (PR#10766)
<jerry.lewis <at> biogenidec.com> writes:> > choose(-5,-7) uses integer arguments (as specified in Help) and returns a > numeric value that is incorrect. Either the function or the documentation > should be fixed. If the function is not fixed, a warning or an error > would be helpful. > > The fact that choose(n,k) usually returns choose(n,round(k,0)) is not > obvious from either the output or the documentation. I suggest issuing a > warning when k is not an integer; a user might easily expect that choose > would coerce both arguments to integers, but only coercing k to an integer > is unusual and potentially misleading.Although perhaps (?) expected from a close reading of the documentation, which says that n is real and k must be integer ...> > The fact that choose(n,k) always rounds .5 up in k (contrary to the round > function documentation) is nowhere documented. > > Help for coerce would lead a reader to expect k to be truncated toward > zero (as.integer), instead of rounding it. >Not being an R-core member I can't say it would be accepted, but since your arguments seem to make sense it would be helpful if you could provide patches to implement your suggested changes -- and perhaps a set of test code to demonstrate the range of behaviors you are proposing. The rounding behavior ( return R_IS_INT(n) ? floor(r + 0.5) : r; in C code) is commented as "/* might have got rounding errors */", so I think it's intended as a way to get the desired answer when a little bit of numeric fuzz is present -- but I agree it seems like overkill. I don't know whether R-core members have on the top of their heads the standard ways of dealing with this situation ... Ben Bolker
Possibly Parallel Threads
- choose incorrect for fractional and some negative integer values (PR#10766)
- [R] choose(n, k) as n approaches k
- choose fails a fundamental property of binomial coefficients (PR#11035)
- [R] choose(n, k) as n approaches k
- For integer vectors, `as(x, "numeric")` has no effect.