Python‎ > ‎

fractions

http://www.adilwaseem.com/

Introduction
Fraction is values which is less than one and value is denoted in this format such as 1/4, 1/2, 5/6, 3/26, 3/4.

Syntax
Fractions(x,y)

Parameters
x, y=number

Examples
The returns are:
 >>> from fractions import Fraction
>>> a=Fractions(1,3)
>>> a=Fraction(1,4)
>>> b=Fraction(3,4)
>>> a+b
Fraction(1, 1)
>>> a-b
Fraction(-1, 2)
>>> c=Fraction(1,4)
>>> c+a
Fraction(1, 2)
>>> 

fractions.Fraction()
The equal to:
from fractions import Fraction
a=Fraction(5,16)
b=Fraction(9,16)
c=a+b
print(c)

>>> 
7/8
>>>


 fractions.gcd
The equal to:
 >>> import fractions
>>> fractions.gcd(2,4)
2
>>> fractions.gcd(110,331)
1
>>> fractions.gcd(5,20)
5
>>>


fractions.Fraction(decimal)
The return is Fraction will produce a rational number from decimal.
 >>>from fractions import Fraction
>>>fractions.Fraction(1.1)
Fraction(2476979795053773, 2251799813685248)


fractions.Fraction(string)
The Fraction is Fraction produces rational number from string.
 >>> fractions.Fraction("111.11")
Fraction(11111, 100)
>>> fractions.Fraction("23.44")
Fraction(586, 25)
>>> 


fractions.Fraction().limit_denominator()
The return value is equal to closest Fraction to self with denominator at most max_denominator.
 >>>from fractions import Fraction
>> fractions.Fraction(1.1).limit_denominator(1000)
Fraction(11, 10)

Comments