Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

binomial_distribution overflows at 171 trials even when the result is representable

Open Beginner friendly
#15,656 1 comment 0 reactions 0 assignees View on GitHub

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
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
data

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

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from TheAlgorithms/Python

All issues in TheAlgorithms/Python

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.