fractions – Rational Numbers

Purpose:Implements a class for working with rational numbers.
Available In:2.6 and later

The Fraction class implements numerical operations for rational numbers based on the API defined by Rational in numbers.

Creating Fraction Instances

As with decimal, new values can be created in several ways. One easy way is to create them from separate numerator and denominator values:

import fractions

for n, d in [ (1, 2), (2, 4), (3, 6) ]:
    f = fractions.Fraction(n, d)
    print '%s/%s = %s' % (n, d, f)

The lowest common denominator is maintained as new values are computed.

$ python fractions_create_integers.py

1/2 = 1/2
2/4 = 1/2
3/6 = 1/2

Another way to create a Fraction is using a string representation of <numerator> / <denominator>:

import fractions

for s in [ '1/2', '2/4', '3/6' ]:
    f = fractions.Fraction(s)
    print '%s = %s' % (s, f)
$ python fractions_create_strings.py

1/2 = 1/2
2/4 = 1/2
3/6 = 1/2

Strings can also use the more usual decimal or floating point notation of [<digits>].[<digits>].

import fractions

for s in [ '0.5', '1.5', '2.0' ]:
    f = fractions.Fraction(s)
    print '%s = %s' % (s, f)
$ python fractions_create_strings_floats.py

0.5 = 1/2
1.5 = 3/2
2.0 = 2

There are class methods for creating Fraction instances directly from other representations of rational values such as float or decimal.

import fractions

for v in [ 0.1, 0.5, 1.5, 2.0 ]:
    print '%s = %s' % (v, fractions.Fraction.from_float(v))
    

Notice that for floating point values that cannot be expressed exactly the rational representation may yield unexpected results.

$ python fractions_from_float.py

0.1 = 3602879701896397/36028797018963968
0.5 = 1/2
1.5 = 3/2
2.0 = 2

Using decimal representations of the values gives the expected results.

import decimal
import fractions

for v in [ decimal.Decimal('0.1'), 
           decimal.Decimal('0.5'), 
           decimal.Decimal('1.5'), 
           decimal.Decimal('2.0'),
           ]:
    print '%s = %s' % (v, fractions.Fraction.from_decimal(v))
$ python fractions_from_decimal.py

0.1 = 1/10
0.5 = 1/2
1.5 = 3/2
2.0 = 2

Arithmetic

Once the fractions are instantiated, they can be used in mathematical expressions as you would expect.

import fractions

f1 = fractions.Fraction(1, 2)
f2 = fractions.Fraction(3, 4)

print '%s + %s = %s' % (f1, f2, f1 + f2)
print '%s - %s = %s' % (f1, f2, f1 - f2)
print '%s * %s = %s' % (f1, f2, f1 * f2)
print '%s / %s = %s' % (f1, f2, f1 / f2)
$ python fractions_arithmetic.py

1/2 + 3/4 = 5/4
1/2 - 3/4 = -1/4
1/2 * 3/4 = 3/8
1/2 / 3/4 = 2/3

Approximating Values

A useful feature of Fraction is the ability to convert a floating point number to an approximate rational value by limiting the size of the denominator.

import fractions
import math

print 'PI       =', math.pi

f_pi = fractions.Fraction(str(math.pi))
print 'No limit =', f_pi

for i in range(1, 100, 5):
    limited = f_pi.limit_denominator(i)
    print '{0:8} = {1}'.format(i, limited)
$ python fractions_limit_denominator.py

PI       = 3.14159265359
No limit = 314159265359/100000000000
       1 = 3
       6 = 19/6
      11 = 22/7
      16 = 22/7
      21 = 22/7
      26 = 22/7
      31 = 22/7
      36 = 22/7
      41 = 22/7
      46 = 22/7
      51 = 22/7
      56 = 22/7
      61 = 179/57
      66 = 201/64
      71 = 223/71
      76 = 223/71
      81 = 245/78
      86 = 267/85
      91 = 267/85
      96 = 289/92

See also

fractions
The standard library documentation for this module.
decimal
The decimal module provides an API for fixed and floating point math.
numbers
Numeric abstract base classes.