fractions — rational numbers

Fraction construction from ints, strings, floats, and Decimal, plus arithmetic and limit_denominator, checked on Python 3.13.12.

モジュールページの本文は現在英語です。

The fractions module provides rational arithmetic with a Fraction that stores a numerator and a positive denominator in lowest terms. Examples were checked on Python 3.13.12.

A float argument is not a decimal literal. Official documentation notes that Fraction(1.1) is not Fraction(11, 10), because 1.1 is the usual binary floating-point approximation. Use Fraction(11, 10), Fraction('1.1'), or Fraction.from_decimal(Decimal('1.1')) when you want that decimal value. This page does not reteach math or decimal beyond that constructor difference.

Examples were checked against Python 3.13. See the official documentation for the complete API.

Python reference overview →

Constructors and float input

Fraction(numerator=0, denominator=1) also accepts another rational, a float, a Decimal, or a string such as '0.1', '1/10', or '3/7'. A zero denominator raises ZeroDivisionError. Fraction(0.1) and Fraction.from_float(0.1) are the exact binary value of the float 0.1, which is not equal to Fraction(1, 10).

Fraction('0.1') and Fraction.from_decimal(Decimal('0.1')) are both 1/10. Some floats are exact binary values: Fraction(1.5) equals Fraction(3, 2).

fractions.Fraction, Fraction.from_float, Fraction.from_decimal

class fractions.Fraction(numerator=0, denominator=1)
classmethod Fraction.from_float(f)
classmethod Fraction.from_decimal(dec)

Build a Fraction from integers, a decimal string, a float, or a Decimal.

ParameterDescription
numerator / denominatorA pair of rationals, or a single other_fraction, float, Decimal, or string when denominator is omitted in the documented single-argument forms.
f / decfrom_float accepts a float. from_decimal accepts a Decimal. Since Python 3.2 the constructor accepts both types directly.

Returns: A Fraction in lowest terms. print uses the numerator/denominator form, such as 1/10.

Raises

  • ZeroDivisionError — Raised when the denominator is zero.
  • TypeError / ValueError — Raised for inputs the constructor does not accept, including a malformed string.
  • The long fraction printed for Fraction(0.1) is the exact value of the IEEE 754 binary64 number 0.1, measured on Python 3.13.12.
python
from decimal import Decimal
from fractions import Fraction

print(Fraction(1, 10), Fraction("0.1"), Fraction("1/10"))
print(Fraction(0.1))
print(Fraction(0.1) == Fraction(1, 10))
print(Fraction(0.1) == Fraction.from_float(0.1))
print(Fraction("0.1") == Fraction.from_decimal(Decimal("0.1")))
print(Fraction(1.5) == Fraction(3, 2))

Expected output

1/10 1/10 1/10
3602879701896397/36028797018963968
False
True
True
True

Related: decimal

Official documentation →

Arithmetic, limit_denominator, and ratios

Fraction arithmetic stays exact: Fraction('0.1') + Fraction(2, 10) is 3/10, and Fraction(1, 2) * Fraction(2, 3) is 1/3. as_integer_ratio() returns the lowest-terms pair with a positive denominator. is_integer(), added in Python 3.12, is true when the value is an integer.

limit_denominator(max_denominator=1000000) returns the closest Fraction whose denominator is at most max_denominator. Official documentation uses this to recover a rational that was stored as a float, for example Fraction(0.1).limit_denominator(10) is 1/10.

Fraction.limit_denominator, Fraction.as_integer_ratio, Fraction.is_integer

Fraction.limit_denominator(max_denominator=1000000)
Fraction.as_integer_ratio()
Fraction.is_integer()

Add and multiply fractions exactly, recover a simple rational from a float, and read the integer ratio.

ParameterDescription
max_denominatorThe largest allowed denominator for the approximation. The default is 1_000_000.

Returns: limit_denominator returns a Fraction. as_integer_ratio returns a tuple of two ints. is_integer returns a bool.

Raises

  • ZeroDivisionError — Fraction(1, 0) raises ZeroDivisionError.
python
from fractions import Fraction

print(Fraction("0.1") + Fraction(2, 10))
print(Fraction(1, 2) * Fraction(2, 3))
print((Fraction(1, 2) * Fraction(2, 3)).as_integer_ratio())
print(Fraction(0.1).limit_denominator(10))
print(Fraction(1, 2).is_integer(), Fraction(2, 1).is_integer())
try:
    Fraction(1, 0)
except ZeroDivisionError as err:
    print(type(err).__name__)

Expected output

3/10
1/3
(1, 3)
1/10
False True
ZeroDivisionError

Official documentation →