-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
denominator of Fraction is positive, which should be documented #122450
Copy link
Copy link
Open
Labels
Description
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:
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
- gh-122450: Fix docs to state denominator positivity of Fraction #122464
- gh-122450: Indicate that
Fractiondenominators are always positive #136789 - [3.14] gh-122450: Indicate that
Fractiondenominators are always positive (GH-136789) #136792 - gh-122450: Expand documentation for
RationalandFraction#136800 - [3.14] gh-122450: Expand documentation for
RationalandFraction(GH-136800) #137363 - [3.13] gh-122450: Expand documentation for
RationalandFraction(#136800) #137367
Reactions are currently unavailable