-1

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?

Oleksandr Novik
  • 489
  • 9
  • 24
  • 1
    Perhaps you could explain your use case. Why do you want to represent a half as 6/12 ? – khelwood Dec 11 '21 at 21:47
  • @khelwood basically I want to use `fractions.Fraction` to find The least common multiple of several numbers by putting them into denominators and then summing them and extracting the denominator if this sum. I know it's a weird way of doing this, but I'm now curious if this is even possible :) – Oleksandr Novik Dec 11 '21 at 21:50
  • Perhaps see this question: https://stackoverflow.com/questions/45651029/how-to-keep-fractions-in-your-equation-output – Peter Wood Dec 11 '21 at 21:57

1 Answers1

3

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
Pedro Maia
  • 2,666
  • 1
  • 5
  • 20