julia icon indicating copy to clipboard operation
julia copied to clipboard

`Base.binomial` returns `Float64` for `Integer` inputs of different types.

Open sgaure opened this issue 1 year ago • 2 comments

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

sgaure avatar Apr 28 '24 20:04 sgaure

Add

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

? Then

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

barucden avatar Apr 29 '24 07:04 barucden

A PR is welcome

jishnub avatar Apr 29 '24 12:04 jishnub