From 25e39acec8d671d32b57dde9b60f9eaf83dc5a6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Baru=C4=8Di=C4=87?= Date: Mon, 29 Apr 2024 15:34:08 +0200 Subject: [PATCH] Use standard `binomial` for Integer arguments 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 --- base/intfuncs.jl | 1 + test/intfuncs.jl | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/base/intfuncs.jl b/base/intfuncs.jl index 90aba9ef5564c..d37122b0494b4 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -1205,6 +1205,7 @@ Base.@assume_effects :terminates_locally function binomial(n::T, k::T) where T<: end copysign(x, sgn) end +binomial(n::Integer, k::Integer) = binomial(promote(n, k)...) """ binomial(x::Number, k::Integer) diff --git a/test/intfuncs.jl b/test/intfuncs.jl index bdfbe42bad0bd..deb1dd10681e8 100644 --- a/test/intfuncs.jl +++ b/test/intfuncs.jl @@ -593,6 +593,10 @@ end x>=0 && @test binomial(x,x-T(2)) == div(x*(x-1), 2) end @test @inferred(binomial(one(T),one(T))) isa T + + # Arguments of different Integer types do not lead to computation of + # generalized binomial coefficient (issue #54296) + @test @inferred(binomial(Int64(5), T(2))) === Int64(10) end for x in ((false,false), (false,true), (true,false), (true,true)) @test binomial(x...) == (x != (false,true))