Python Forum
fraction module: can you stop the reducing?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
fraction module: can you stop the reducing?
#1
when you import the fraction module, you can instantiate a Fraction instance:

from fractions import Fraction as frac
a = frac(1,3)
b = frac(1,6)
c = a+b
print('sum is {0}'.format©)

1/2

But I would like have the reducible form of 3/6

Is there a switch I'm missing in rationals, fractions, etc? that would cause the fraction to NOT be put in irreducible form?

Tnx
Reply
#2
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).  

>>> frac(3, 6, _normalize=False)
Fraction(3, 6)
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  method to remove zero digits from fraction in decimal Skaperen 17 2,573 Oct-23-2022, 04:02 AM
Last Post: Skaperen
  Reducing runtime memory usage in Cpython interpreter david_the_graower 2 2,175 Oct-18-2021, 09:56 PM
Last Post: david_the_graower
  Reducing JSON character count in Python for a Twitter Bot johnmitchell85 2 45,798 Apr-28-2021, 06:08 PM
Last Post: johnmitchell85
  Fraction Calculation with Limitations TINMAN01 13 5,290 Dec-22-2020, 04:45 AM
Last Post: bowlofred
  Need help reducing some code around a subprocess anistorian 4 2,886 Jun-12-2019, 03:32 PM
Last Post: snippsat
  output list reducing each time through loop 3Pinter 6 3,446 Mar-19-2019, 01:31 PM
Last Post: perfringo
  whole number and fraction Skaperen 11 5,462 Mar-17-2019, 11:33 PM
Last Post: Skaperen
  Stop execution of a module catosp 4 3,452 Sep-11-2018, 06:55 PM
Last Post: catosp
  Reducing code length xavier992 4 4,302 Jul-22-2017, 12:45 AM
Last Post: xavier992

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020