Skip to content

Fraction scientific formatting fails for near-unit values with large numerators and denominators #158716

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: “Fraction instances support float-style formatting, with presentation types 'e', 'E', 'f', 'F', 'g', 'G' and '%'.” — Python standard library documentation, fractions module, Fraction formatting.

Expected: 1.00e+00

Actual: ValueError: Exceeds the limit (4300 digits) for integer string conversion; use sys.set_int_max_str_digits() to increase the limit

import fractions
import sys

if sys.version_info < (3, 12):
    print("REFUTATION REJECTED: float-style Fraction formatting requires Python 3.12+")
    sys.exit(0)

sys.set_int_max_str_digits(4300)
k = 4300
d = 10**k
n = d + 1
x = fractions.Fraction(n, d)
spec = ".2e"  # Valid Fraction and documented float-style presentation.

# Independent exact rounding to two decimal places; x has exponent zero.
q, r = divmod(100 * n, d)
q += 2 * r > d or (2 * r == d and q % 2)
expected = f"{q // 100}.{q % 100:02d}e+00"

try:
    actual = format(x, spec)
except Exception as e:
    actual = ("exception", type(e).__name__, str(e))

if actual != expected:
    print("REFUTATION CONFIRMED:",
          {"x": "Fraction(10**4300 + 1, 10**4300)",
           "int_max_str_digits": sys.get_int_max_str_digits(), "format": spec},
          "actual=", repr(actual), "expected=", repr(expected))
else:
    print("REFUTATION REJECTED: actual matches the exact rounding reference")

Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library fractions:

REFUTATION CONFIRMED: {'x': 'Fraction(10**4300 + 1, 10**4300)', 'int_max_str_digits': 4300, 'format': '.2e'} actual= ('exception', 'ValueError', 'Exceeds the limit (4300 digits) for integer string conversion; use sys.set_int_max_str_digits() to increase the limit') expected= '1.00e+00'

This report was found and written by an automated property-testing tool I run (bugforge). The reproducer above was executed and its output is pasted unedited; no person reviewed the report before it was filed. The search script is in https://github.com/augusto-rehfeldt/bugforge-results/tree/main/fractions-20261003-233134-c1

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Activity

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions