Skip to content

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

Open

Description

@dongyikuan919

Repository commit

6e03a48579ca212a08b36be00378f887dc41a003

Python version (python --version)

Python 3.11.0, macOS

Dependencies version (pip freeze)

The reproduction uses only Python's standard library; no third-party dependencies are involved.

Expected behavior

continued_fraction(Fraction(2**53 + 3)) should return [9007199254740995], the continued fraction of that integer. Large exact fractions should not fail merely because they exceed floating-point range.

Actual behavior

Run from the repository root:

from fractions import Fraction
from maths.continued_fraction import continued_fraction

print(continued_fraction(Fraction(2**53 + 3)))
try:
    print(continued_fraction(Fraction(10**400, 3)))
except OverflowError as exc:
    print(type(exc).__name__ + ':', exc)

Output:

[9007199254740996, -1]
OverflowError: integer division result too large for a float

The first result represents the same integer numerically, but is not the regular continued fraction: the trailing term is negative because the first quotient was rounded upward.

In maths/continued_fraction.py, floor(numerator / denominator) first converts the integer ratio to a float. This loses precision for sufficiently large values and can overflow. Computing the quotient directly with integer floor division (numerator // denominator) preserves exact arithmetic and also handles negative fractions.

I searched existing issues and PRs for continued fraction and continued_fraction; I did not find an existing report of this problem.

Activity

  1. Shriraj1901 commented on Oct 28, 2026

    @Shriraj1901

    Hi, I'd like to take this issue. I'll investigate the division logic in continued_fraction.py, correct the handling of large integers without relying on floating-point conversion, and add tests for large and negative fractions. Could you please assign this issue to me if it's still available? I'll run the relevant tests before submitting a PR.

  2. dongyikuan919 commented on Oct 28, 2026

    @dongyikuan919
    Author

    Thanks for offering to investigate and add tests for large and negative fractions! A maintainer can advise on assignment.

    AI assistance was used to draft this reply.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions