简体   繁体   中英

Python prevent fractions.Fraction from reducing fractions

So I have a simple example:

import fractions
f = fractions.Fraction(6, 12)

and I don't want f to become 1/2 . I want it to remain 6/12 . Is there a way to do this?

Found a solution here :

No, not really. There's an internal _normalize flag that can be set, allowing the creation of non-normalized fractions (and probably leading to much undefined behavior).

>>> f = fractions.Fraction(6, 12, _normalize=False)
>>> f
Fraction(6, 12)

But it wouldn't do anything for the results of calculations. Part of the use of the module is to allow equal fractions to be found equal, and that's done through normalization. So it looks to be a core part that can't be disabled.

>>> frac(1,2) == frac(3,6, _normalize=False)
False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM