Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base.binomial returns Float64 for Integer inputs of different types. #54296

Closed
sgaure opened this issue Apr 28, 2024 · 2 comments · Fixed by #54307
Closed

Base.binomial returns Float64 for Integer inputs of different types. #54296

sgaure opened this issue Apr 28, 2024 · 2 comments · Fixed by #54307
Labels
maths Mathematical functions

Comments

@sgaure
Copy link

sgaure commented Apr 28, 2024

The docs say

binomial(n::Integer, k::Integer)

  The binomial coefficient \binom{n}{k}, being the coefficient of the kth term in the polynomial
  expansion of (1+x)^n.

which isn't strictly true. When n and k have different integer types, the generalized binomial coefficient is returned, i.e. from binomial(x::Number, k::Integer).

julia> binomial(4, 2)
6

julia> binomial(Int32(4), 2)
6.0

julia> binomial(Int32(4), Int32(2))
6
@nsajko nsajko added the maths Mathematical functions label Apr 28, 2024
@barucden
Copy link
Contributor

Add

binomial(n::Integer, k::Integer) = binomial(promote(n, k)...)

?
Then

julia> binomial(Int32(4), Int64(2)) 
6 # typeof(6) == Int64

@jishnub
Copy link
Contributor

jishnub commented Apr 29, 2024

A PR is welcome

barucden added a commit to barucden/julia that referenced this issue Apr 29, 2024
Before this commit, `binomial(Int64(10), Int32(5))` meant the
generalized binomial coefficient due to the arguments being of different
types, which was in contradiction with the documentation.

Fixes JuliaLang#54296
jishnub pushed a commit that referenced this issue Apr 29, 2024
Before this commit, `binomial(Int64(10), Int32(5))` meant the
generalized binomial coefficient due to the arguments being of different
types, which was in contradiction with the documentation.

Fixes #54296
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maths Mathematical functions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants