fractions
index
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/fractions.py
Module Docs

Rational, infinite-precision, real numbers.

 
Modules
       
math
numbers
operator
re

 
Classes
       
numbers.Rational(numbers.Real)
Fraction

 
class Fraction(numbers.Rational)
    This class implements rational numbers.
 
In the two-argument form of the constructor, Fraction(8, 6) will
produce a rational number equivalent to 4/3. Both arguments must
be Rational. The numerator defaults to 0 and the denominator
defaults to 1 so that Fraction(3) == 3 and Fraction() == 0.
 
Fractions can also be constructed from:
 
  - numeric strings similar to those accepted by the
    float constructor (for example, '-2.3' or '1e10')
 
  - strings of the form '123/456'
 
  - float and Decimal instances
 
  - other Rational instances (including integers)
 
 
Method resolution order:
Fraction
numbers.Rational
numbers.Real
numbers.Complex
numbers.Number
__builtin__.object

Methods defined here:
__abs__(a)
abs(a)
__add__(a, b)
a + b
__copy__(self)
__deepcopy__(self, memo)
__div__(a, b)
a / b
__eq__(a, b)
a == b
__floordiv__(a, b)
a // b
__ge__(a, b)
a >= b
__gt__(a, b)
a > b
__hash__(self)
hash(self)
 
Tricky because values that are exactly representable as a
float must have the same hash as that float.
__le__(a, b)
a <= b
__lt__(a, b)
a < b
__mod__(a, b)
a % b
__mul__(a, b)
a * b
__neg__(a)
-a
__nonzero__(a)
a != 0
__pos__(a)
+a: Coerces a subclass instance to Fraction
__pow__(a, b)
a ** b
 
If b is not an integer, the result will be a float or complex
since roots are generally irrational. If b is an integer, the
result will be rational.
__radd__(b, a)
a + b
__rdiv__(b, a)
a / b
__reduce__(self)
__repr__(self)
repr(self)
__rfloordiv__(b, a)
a // b
__rmod__(b, a)
a % b
__rmul__(b, a)
a * b
__rpow__(b, a)
a ** b
__rsub__(b, a)
a - b
__rtruediv__(b, a)
a / b
__str__(self)
str(self)
__sub__(a, b)
a - b
__truediv__(a, b)
a / b
__trunc__(a)
trunc(a)
limit_denominator(self, max_denominator=1000000)
Closest Fraction to self with denominator at most max_denominator.
 
>>> Fraction('3.141592653589793').limit_denominator(10)
Fraction(22, 7)
>>> Fraction('3.141592653589793').limit_denominator(100)
Fraction(311, 99)
>>> Fraction(4321, 8765).limit_denominator(10000)
Fraction(4321, 8765)

Class methods defined here:
from_decimal(cls, dec) from abc.ABCMeta
Converts a finite Decimal instance to a rational number, exactly.
from_float(cls, f) from abc.ABCMeta
Converts a finite float to a rational number, exactly.
 
Beware that Fraction.from_float(0.3) != Fraction(3, 10).

Static methods defined here:
__new__(cls, numerator=0, denominator=None)
Constructs a Fraction.
 
Takes a string like '3/2' or '1.5', another Rational instance, a
numerator/denominator pair, or a float.
 
Examples
--------
 
>>> Fraction(10, -8)
Fraction(-5, 4)
>>> Fraction(Fraction(1, 7), 5)
Fraction(1, 35)
>>> Fraction(Fraction(1, 7), Fraction(2, 3))
Fraction(3, 14)
>>> Fraction('314')
Fraction(314, 1)
>>> Fraction('-35/4')
Fraction(-35, 4)
>>> Fraction('3.1415') # conversion from numeric string
Fraction(6283, 2000)
>>> Fraction('-47e-2') # string may include a decimal exponent
Fraction(-47, 100)
>>> Fraction(1.47)  # direct construction from float (exact conversion)
Fraction(6620291452234629, 4503599627370496)
>>> Fraction(2.25)
Fraction(9, 4)
>>> Fraction(Decimal('1.47'))
Fraction(147, 100)

Data descriptors defined here:
denominator
numerator

Data and other attributes defined here:
__abstractmethods__ = frozenset([])

Methods inherited from numbers.Rational:
__float__(self)
float(self) = self.numerator / self.denominator
 
It's important that this conversion use the integer's "true"
division rather than casting one side to float before dividing
so that ratios of huge integers convert without overflowing.

Methods inherited from numbers.Real:
__complex__(self)
complex(self) == complex(float(self), 0)
__divmod__(self, other)
divmod(self, other): The pair (self // other, self % other).
 
Sometimes this can be computed faster than the pair of
operations.
__rdivmod__(self, other)
divmod(other, self): The pair (self // other, self % other).
 
Sometimes this can be computed faster than the pair of
operations.
conjugate(self)
Conjugate is a no-op for Reals.

Data descriptors inherited from numbers.Real:
imag
Real numbers have no imaginary component.
real
Real numbers are their real component.

Methods inherited from numbers.Complex:
__ne__(self, other)
self != other

Data and other attributes inherited from numbers.Number:
__metaclass__ = <class 'abc.ABCMeta'>
Metaclass for defining Abstract Base Classes (ABCs).
 
Use this metaclass to create an ABC.  An ABC can be subclassed
directly, and then acts as a mix-in class.  You can also register
unrelated concrete classes (even built-in classes) and unrelated
ABCs as 'virtual subclasses' -- these and their descendants will
be considered subclasses of the registering ABC by the built-in
issubclass() function, but the registering ABC won't show up in
their MRO (Method Resolution Order) nor will method
implementations defined by the registering ABC be callable (not
even via super()).

 
Functions
       
gcd(a, b)
Calculate the Greatest Common Divisor of a and b.
 
Unless b==0, the result will have the same sign as b (so that when
b is divided by it, the result comes out positive).

 
Data
        __all__ = ['Fraction', 'gcd']