Skip to content

denominator of Fraction is positive, which should be documented #122450

Open
@privet-kitty

Description

@privet-kitty

Documentation

This suggestion is to clarify the current behavior of the fractions module.

The current implementation of Fraction reduces an input numerator and a denominator so that the latter is positive, which is implemented in the following places:

cpython/Lib/fractions.py

Lines 299 to 303 in d27a53f

g = math.gcd(numerator, denominator)
if denominator < 0:
g = -g
numerator //= g
denominator //= g

This behavior is partially documented in https://docs.python.org/3.14/library/fractions.html:

  • numerator
    • Numerator of the Fraction in lowest term.
  • denominator
    • Denominator of the Fraction in lowest term.
  • as_integer_ratio()
    • Return a tuple of two integers, whose ratio is equal to the original Fraction. The ratio is in lowest terms and has a positive denominator.

It says that a denominator returned by as_integer_ratio is positive, but doesn't mention to the sign of the denominator property. I think there should be no problem with making that explicit also in the explanation of denominator.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc direasy

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions