Fractional module in Python

Python Methods and Functions

Fraction instances. An instance of a fraction can be created from a pair of integers, another rational number, or a string. Fraction instances are hashable and should be treated as immutable.

  1. fractions of a class. Fraction (Numerator = 0, Denominator = 1): This requires both the numerator and denominator to be instances of numbers. Rationally and an instance of the fraction with value = (numerator / denominator) is returned. A zero split error occurs if the denominator = 0.

    from fractions import Fraction

     

    print Fraction ( 11 , 35 )

    # returns the fraction (11, 35)

     

    print Fraction ( 10 , 18 )

    # returns the fraction (5, 9)

      

    print  Fraction ()

    # returns fraction (0, 1)

    Output:

     11/35 5 / 9 0 
  2. class fractions.Fraction (other_fraction): This requires other_fraction to be a numbers.Rational instance and a faction instance with the same value is returned.
  3. class fractions.Fraction (float): this requires an instance of type float and returns a fraction instance with the same value.

    from fractions import Fraction

      

    print Fraction ( 1.13 < code class = "plain">)

    # returns a fraction (1272266894732165, 1125899906842624)

    Output:

     1272266894732165/1125899906842624 
  4. class fractions.Fraction (decimal): this requires a decimal instance and returns a fraction instance with the same value.

    from fractions import Fraction

     

    print Fraction ( '1.13' )

    # returns the fraction (113, 100)

    Output:

     1 13/100 
  5. class fractions.Fraction (string): this requires an instance of a string or unicode instance and returns an instance of fractions with that same value.

    The form for this instance is: [sign] numerator [& # 39; / & # 39; denominator]
    Here, the sign represents "+" or "-", and the numerator and denominator are single-digit strings.

    from fractions import Fraction

     

    print Fraction ( ' 8 / 25' )

    # returns the fraction (8, 25)

     

    print Fraction ( '1.13' )

    # returns fraction (113, 100)

     

    print Frac tion ( '3 / 7' )

    # returns the fraction (3, 7)

     

    print Fraction ( '1.414213' )

    # returns the fraction (1414213, 1000000)

    Output:

     8/25 113/100 3/7 1414213/1000000 
  6. limit_denominator ( max_denominator = 1000000):
    • This method is useful for finding rational approximations to a given floating point number.
    • This module finds and returns the nearest fraction with a denominator of at most max_denominator.
    • This module can also be used to return the numerator of a given fraction in the smallest term using the numerator property and the denominator using the  denominator .

    from fractions import Fraction

     

    print Fraction ( '3.14159265358979323846' )

    # returns the fraction (157079632679489661923, 50000000000000000000)

      

    print Fraction ( '3.14159265358979323846' ). limit_denominator ( 10000 )

    # returns the fraction (355, 113)

     

    print Fraction ( '3.14159265358979323846' ). limit_denominator ( 100 )

    # returns the fraction (311, 99)

     

    print Fraction ( '3.14159265358979323846' ). limit_denominator ( 10 )

    # returns the fraction (22, 7)

     

    print Fraction ( 125 , 50 ). numerator

    # returns 5

     

     

    print Fraction ( 125 , 50 ). denominator

    # returns 2

    Output:

     157079632679489661923/50000000000000000000 355/113 311/99 22/7 5 2 

Performing mathematical operations on fractions

from fractions import Fraction

 

print Fraction ( 113 , 100 ) + Fraction ( 25 , 18 )

# returns the fraction (2267, 900 )

 

print Fraction ( 18 , 5 ) / Fraction ( 18 , 10 )

# returns the fraction (2, 1)

 

print Fraction ( 18 , 5 ) * Fraction ( 16 , 19 )

# returns the fraction (288, 95)

 

print Fraction ( 18 , 5 ) * Fraction ( 15 , 36 )

# returns the fraction (3, 2)

 

print Fraction ( 12 , 5 ) * * Fraction ( 12 , 10 )

# returns 2.8592589556010197

Output:

 2267/900 2 288/95 3/2 2.8592589556 

Fractional calculations using various functions of the math module

import math

from fractions import Fraction

  

print math.sqrt (Fraction ( 25 ,  4 ))

# returns 2.5

 

print math.sqrt (Fraction ( 28 , 3 ))

# returns 3.0550504633038935

 

print math.floor (Fraction ( 3558 , 1213 ))

# returns 2

 

print Fraction (math.sin (math.pi / 3 ))

# returns the fraction ( 3900231685776981, 4503599627370496)

 

print Fraction (math.sin (math.pi / 3 )). limit_denominator ( 10 )

# returns the fraction (6, 7)

Output:

 2.5 3.0550504633 2.0 3900231685776981/4503599627370496 6/7 

This article courtesy of Aditi Gupta . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.





Tutorials