Skip to content

fix(maths): compute finite roots of large integer products - #15807

Open
lanqing431 wants to merge 1 commit into
TheAlgorithms:masterfrom
lanqing431:fix/geometric-mean-integer-overflow
Open

lanqing431 wants to merge 1 commit into
TheAlgorithms:masterfrom
lanqing431:fix/geometric-mean-integer-overflow

Conversation

@lanqing431

Copy link
Copy Markdown

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

The integer product is converted to float before taking a fractional power. A product outside float range raises OverflowError even when its root is finite. The later rounding check can also overflow by raising a float candidate to a power.

Keep the existing calculation for ordinary values and fall back to exp(log(abs(product)) / count) only after an overflow. Use integer exponentiation for the rounded-candidate verification, converting an exact candidate back to float only when it matches.

Reproduction / verification

import math
from maths.geometric_mean import compute_geometric_mean

assert math.isclose(compute_geometric_mean(10**200, 10**200), 1e200, rel_tol=1e-12)
assert math.isclose(compute_geometric_mean(-10**150, 10**150, 10**150), -1e150, rel_tol=1e-12)
assert math.isclose(compute_geometric_mean(2**600, 2**600), float(2**600), rel_tol=1e-12)

Validated on Windows with CPython 3.15.2:

  • python -m doctest -v maths/geometric_mean.py
  • python -m pytest maths/geometric_mean.py --doctest-modules -q
  • python -m ruff check maths/geometric_mean.py (Ruff 0.17.3)
  • python -m ruff format --check maths/geometric_mean.py
  • Independent local regression checks: 53 integer-input cases checked against a 70-digit Decimal root calculation, including large products with finite positive and negative odd roots and all ordinary doctest examples.

The regression harness found 4 failing cases on the unchanged upstream file and none after the fix (53 checks). It is kept outside this source-only diff to follow the repository's instruction not to change implementation and doctests together. The original doctests remain unchanged. The complete repository suite and free-threaded CI were not run locally.

Checklist

  • I have read CONTRIBUTING.md and AGENTS.md.
  • This PR only changes one algorithm file.
  • Existing doctests pass and are unchanged.
  • The modified file passes Ruff lint and formatting checks.
  • CI checks have completed. (Not run until this PR is opened.)

AI assistance: OpenAI Codex assisted with source analysis, implementation, reproductions, local verification, and drafting this description.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant