Python prevent fractions.Fraction from reducing fractions

351 Views Asked by At

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?

1

There are 1 best solutions below

0
Pedro Maia On BEST ANSWER

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(6,12, _normalize=False)
False