Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fraction literals should support underscores in numbers and whitespace around numbers #96432

Closed
rhettinger opened this issue Aug 30, 2022 · 3 comments
Labels
type-feature A feature request or enhancement

Comments

@rhettinger
Copy link
Contributor

rhettinger commented Aug 30, 2022

To make fraction literals more readable, this should be made to work: Fraction('31_415_925_535 / 10_000_000_000')

PEP 515 – Underscores in Numeric Literals are well supported in all numeric types except for fractions. This works: 1_234 and Decimal('1_234'). However, this is not accepted: Fraction('1_234').

Like decimals and ints, fractions support leading and trailing whitespace for string inputs: int(' 123 '), Decimal(' 123.4 '), and Fraction(' 123/100 '). What is still needed is support for optional whitespace around the forward slash: Fraction('355 / 113'). This particular example came up when using the Fraction module to teach math to kids and where the string literal was obtained from calling input().

Side note: If you write print(355/113) in your code, Black will reformat it to print(355 / 113). Because of this, I expect that adults will also develop a preference for writing fractions with whitespace around the slash.

@rhettinger rhettinger added the type-feature A feature request or enhancement label Aug 30, 2022
@mdickinson
Copy link
Member

See also #88512

@mdickinson
Copy link
Member

mdickinson commented Aug 30, 2022

this should be made to work: Fraction('31_415_925_535 / 10_000_000_000')

Agreed on the underscores, both for readability and for consistency with int.

+1 to allowing whitespace around the slash, too.

I don't agree with the slippery slope argument in #88512 (comment). complex has all sorts of oddities both in parsing and printing, and I don't see any need for those to limit what seem like fairly natural enhancements for Fraction.

@mdickinson
Copy link
Member

FTR, underscore support was added previously in #26422

scoder added a commit to scoder/quicktions that referenced this issue Mar 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants