Skip to content

Fix continued_fraction for large integers and big fractions - #15660

Open
trleo wants to merge 1 commit into
TheAlgorithms:masterfrom
trleo:fix/continued-fraction-float
Open

trleo wants to merge 1 commit into
TheAlgorithms:masterfrom
trleo:fix/continued-fraction-float

Conversation

@trleo

@trleo trleo commented Oct 28, 2026

Copy link
Copy Markdown

Describe your change

  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests?

Fixes #15655. continued_fraction used floor(numerator / denominator), converting huge integers to float. That rounded 2**53+3 up, giving [9007199254740996, -1] instead of [9007199254740995], and raised OverflowError for Fraction(10**400, 3). This change uses exact integer floor division numerator // denominator, removes the now-unused floor import, and adds a regression doctest for Fraction(2**53 + 3).

Tested on Linux: python -m doctest maths/continued_fraction.py passes 9 tests, ruff check and ruff format pass. Ruff/pytest via uvx were not available in this environment, so CI should confirm those.

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes continued_fraction uses float division, producing invalid terms for large integers and raising OverflowError #15655".

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.

continued_fraction uses float division, producing invalid terms for large integers and raising OverflowError

1 participant