binomial_distribution overflows at 171 trials even when the result is representable
Maintainers usually reply within 2 days
@landfvit is already working on this.
Since Oct 28, 2026.
Assessment
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Newbie friendliness
- 85/100
Research direction
Read maths/binomial_distribution.py and run the reproduction from the issue: binomial_distribution(85, 171, 0.5) should match 0.060749764626165485. Replace the float(factorial(trials))-based intermediate with a math.comb(trials, successes) form so the large factorial is never converted to a float, then re-run the reproduction. Done means no OverflowError for this input and the expected probability is returned.
Written by the indexing model from the issue text.
Description
Repository commit
6e03a48579ca212a08b36be00378f887dc41a003
Python version (python --version)
Python 3.11.0, macOS
Dependencies version (pip freeze)
No third-party dependencies are needed for this reproduction; both the implementation and reference use the Python standard library.
Expected behavior
The probability mass function should return a finite probability for valid inputs such as 85 successes in 171 fair trials. The expected value for this case is approximately 0.060749764626165485.
Actual behavior
Run from the repository root:
from math import comb
from maths.binomial_distribution import binomial_distribution
k, n, p = 85, 171, 0.5
print('reference:', comb(n, k) * p**k * (1-p)**(n-k))
print('actual:', binomial_distribution(k, n, p))
Output:
reference: 0.060749764626165485
Traceback (most recent call last):
...
OverflowError: int too large to convert to float
In maths/binomial_distribution.py, float(factorial(trials)) overflows for trials >= 171, even though the final probability for this input is representable. Using math.comb(trials, successes) avoids converting the much larger intermediate factorial to a float. For substantially larger inputs, an implementation should also consider underflow when multiplying the probability terms; the reference expression above is intended only for the concrete reproduction.
Searched existing issues and pull requests for binomial_distribution and binomial overflow; I did not find an existing report of this failure.
- Dominant language
- Python
- Stars
- 225k
- Forks
- 51.2k
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 32
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from TheAlgorithms/Python
-
continued_fraction uses float division, producing invalid terms for large integers and raising OverflowErrorPossibly taken @trleo claimed this 2 days ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 88/100
TheAlgorithms/Python#15655 · 2 comments ·
Maintainers usually reply within 2 days
-
ciphers/base32.py and ciphers/base85.py encode Unicode code points instead of bytes: output diverges from the spec for non-ASCII, round-trips corrupt chars >= U+0400, arbitrary bytes crashPossibly taken @Nate-br claimed this 4 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
TheAlgorithms/Python#15636 · 1 comment ·
Maintainers usually reply within 2 days
-
ciphers/des_ecb.py: encrypt/decrypt silently corrupts any character above U+00FF (round-trip fails)Possibly taken @mohammedanasnakhuda claimed this 5 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
TheAlgorithms/Python#15634 ·
Maintainers usually reply within 2 days
-
other/password.py: alternative_password_generator ignores the requested length when the required characters exceed itPossibly taken @raaj-2222 claimed this 5 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
TheAlgorithms/Python#15633 · 1 comment ·
Maintainers usually reply within 2 days
-
ciphers/base58.py is a copy of base64_cipher.py — no Base58 implementation exists in the filePossibly taken @SakshiYeole claimed this 6 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
TheAlgorithms/Python#15637 ·
Maintainers usually reply within 2 days
All issues in TheAlgorithms/Python
Similar issues
-
getchaintips leaves out the active tip once a header is staged on it, where Core always lists itOpen
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
btclib-org/btclib-node#2158 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
dbt-msft/dbt-sqlserver#897 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Gathin23/cleanyourtv#2770 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
vllm-project/vllm#64845 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
dekobon/big-code-analysis#1729 ·
Maintainers usually reply within 1 day