From ef1e530b6c5b9247b07cd1ab6361295b80329a03 Mon Sep 17 00:00:00 2001 From: CarloLucibello Date: Thu, 26 Nov 2015 18:30:51 -0500 Subject: [PATCH] generalize binomial to real and complex arguments --- base/special/gamma.jl | 4 ++++ test/math.jl | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/base/special/gamma.jl b/base/special/gamma.jl index 0678287e1b064..5b23cfa879dda 100644 --- a/base/special/gamma.jl +++ b/base/special/gamma.jl @@ -482,3 +482,7 @@ eta(x::Integer) = eta(Float64(x)) eta(x::Real) = oftype(float(x),eta(Float64(x))) eta(z::Complex) = oftype(float(z),eta(Complex128(z))) @vectorize_1arg Number eta + +Base.binomial{S<:Number,T<:Number}(n::S, k::T) = ( (isinteger(n) && isinteger(k)) + ? convert(promote_type(T,S), binomial(round(Int64, n), round(Int64, k))) + : gamma(1+n) / (gamma(1+n-k) * gamma(1+k)) ) diff --git a/test/math.jl b/test/math.jl index b3039e8decf2c..713753ce98a03 100644 --- a/test/math.jl +++ b/test/math.jl @@ -712,3 +712,8 @@ end let A = [1 2; 3 4]; B = [5 6; 7 8]; C = [9 10; 11 12] @test muladd(A,B,C) == A*B + C end + +@test binomial(2.,1) == 2 +@test binomial(-2.,1) == -2 +@test typeof(binomial(-2.,1)) == Float64 +@test_approx_eq binomial(-2.1,1.1) -4.21013511374008