Skip to content

binomial: return 0 for k < 0 - #84

Open
Marsman1996 wants to merge 1 commit into
rust-num:mainfrom
Marsman1996:fix-binomial
Open

Marsman1996 wants to merge 1 commit into
rust-num:mainfrom
Marsman1996:fix-binomial

Conversation

@Marsman1996

Copy link
Copy Markdown

binomial(n, k) explicitly returns 0 for the out-of-range case k > n, but for k < 0 no branch covers it.

num-integer/src/lib.rs

Lines 1150 to 1154 in 765fd9b

pub fn binomial<T: Integer + Clone>(mut n: T, k: T) -> T {
// See http://blog.plover.com/math/choose.html for the idea.
if k > n {
return T::zero();
}

binomial(5i64, -3) == 1
binomial(0i64, -1) == 1

The result is also inconsistent within negative k, depending on how k compares to n:

binomial(5i64, -3)  == 1   // k < 0 && k <= n: silent loop seed
binomial(-3i64, -1) == 0   // k < 0 && k >  n: hits the explicit branch

So this PR mirrors the existing k > n branch for the other end of the range.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant