Skip to content

Binomial for BigInt does not work with k > typemax(UInt64) #48072

@originalsouth

Description

@originalsouth
  1. The output of versioninfo()
Julia Version 1.8.4
Commit 00177ebc4fc (2022-12-23 21:32 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 16 × AMD Ryzen 7 5800U with Radeon Graphics
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, znver3)
  Threads: 1 on 16 virtual cores
  1. How you installed Julia
    See https://aur.archlinux.org/packages/julia-bin

  2. A minimal working example (MWE), also known as a minimum reproducible example

julia> binomial(big(1), big(typemax(UInt64))+1)
ERROR: InexactError: UInt64(18446744073709551616)
Stacktrace:
 [1] Type
   @ ./gmp.jl:354 [inlined]
 [2] binomial(n::BigInt, k::BigInt)
   @ Base.GMP ./gmp.jl:685
 [3] top-level scope
   @ REPL[3]:1

Culprit base/gmp.jl:685:

binomial(n::BigInt, k::Integer) = k < 0 ? BigInt(0) : binomial(n, UInt(k))

This is probably a limitation due to GMP, but in that case

  1. Probably binomial(n::BigInt, k::Integer) = k < 0 ? BigInt(0) : binomial(n, UInt(min(k, n-k))) will cover more cases,
  2. Limitations are not currently documented.

Please advise on how to proceed — if at all — thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bignumsBigInt and BigFloat

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions