fractionsRational numbers有理数

Source code: Lib/fractions.py


The fractions module provides support for rational number arithmetic.fractions模块支持有理数算术。

A Fraction instance can be constructed from a pair of integers, from another rational number, or from a string.分数实例可以由一对整数、另一个有理数或字符串构造。

classfractions.Fraction(numerator=0, denominator=1)
classfractions.Fraction(other_fraction)
classfractions.Fraction(float)
classfractions.Fraction(decimal)
classfractions.Fraction(string)

The first version requires that numerator and denominator are instances of numbers.Rational and returns a new Fraction instance with value numerator/denominator. 第一个版本要求分子分母numbers.Rational的实例,并返回一个具有值numerator/denominator的新分数实例。If denominator is 0, it raises a ZeroDivisionError. 如果denominator0,则会产生ZeroDivisionErrorThe second version requires that other_fraction is an instance of numbers.Rational and returns a Fraction instance with the same value. 第二个版本要求other_fractionnumbers.Rational的实例,并返回具有相同值的Fraction实例。The next two versions accept either a float or a decimal.Decimal instance, and return a Fraction instance with exactly the same value. 接下来的两个版本接受floatdecimal.Decimal实例,并返回值完全相同的Fraction实例。Note that due to the usual issues with binary floating-point (see Floating Point Arithmetic: Issues and Limitations), the argument to Fraction(1.1) is not exactly equal to 11/10, and so Fraction(1.1) does not return Fraction(11, 10) as one might expect. 请注意,由于二进制浮点的常见问题(请参阅浮点算术:问题和限制),Fraction(1.1)的参数并不完全等于11/10,因此Fraction(1.1)不会像人们预期的那样返回Fraction(11, 10)(But see the documentation for the limit_denominator() method below.)(但请参阅以下limit_denominator()方法的文档。)The last version of the constructor expects a string or unicode instance. 构造函数的最新版本需要字符串或unicode实例。The usual form for this instance is:此实例的常见形式为:

[sign] numerator ['/' denominator]

where the optional sign may be either ‘+’ or ‘-’ and numerator and denominator (if present) are strings of decimal digits. 其中可选sign可以是“+”或“-”,numeratordenominator(如果存在)是十进制数字串。In addition, any string that represents a finite value and is accepted by the float constructor is also accepted by the Fraction constructor. 此外,表示有限值并被float构造函数接受的任何字符串也被Fraction构造函数接受。In either form the input string may also have leading and/or trailing whitespace. 在这两种形式中,输入字符串也可能有前导和/或尾随空格。Here are some examples:以下是一些例子:

>>> from fractions import Fraction
>>> Fraction(16, -10)
Fraction(-8, 5)
>>> Fraction(123)
Fraction(123, 1)
>>> Fraction()
Fraction(0, 1)
>>> Fraction('3/7')
Fraction(3, 7)
>>> Fraction(' -3/7 ')
Fraction(-3, 7)
>>> Fraction('1.414213 \t\n')
Fraction(1414213, 1000000)
>>> Fraction('-.125')
Fraction(-1, 8)
>>> Fraction('7e-6')
Fraction(7, 1000000)
>>> Fraction(2.25)
Fraction(9, 4)
>>> Fraction(1.1)
Fraction(2476979795053773, 2251799813685248)
>>> from decimal import Decimal
>>> Fraction(Decimal('1.1'))
Fraction(11, 10)

The Fraction class inherits from the abstract base class numbers.Rational, and implements all of the methods and operations from that class. Fraction类继承自抽象基类numbers.Rational,并实现该类的所有方法和操作。Fraction instances are hashable, and should be treated as immutable. 实例是可散列的,应该视为不可变的。In addition, Fraction has the following properties and methods:此外,Fraction具有以下属性和方法:

Changed in version 3.2:版本3.2中更改: The Fraction constructor now accepts float and decimal.Decimal instances.Fraction构造函数现在接受float实例和decimal.Decimal实例。

Changed in version 3.9:版本3.9中更改: The math.gcd() function is now used to normalize the numerator and denominator. math.gcd()函数现在用于规范化numeratordenominatormath.gcd() always return a int type. math.gcd()始终返回int类型。Previously, the GCD type depended on numerator and denominator.以前,GCD类型取决于numeratordenominator

numerator

Numerator of the Fraction in lowest term.最低项中分数的分子。

denominator

Denominator of the Fraction in lowest term.最低项中分数的分母。

as_integer_ratio()

Return a tuple of two integers, whose ratio is equal to the Fraction and with a positive denominator.返回由两个整数组成的元组,其比率等于分数并具有正分母。

New in version 3.8.版本3.8中新增。

from_float(flt)

This class method constructs a Fraction representing the exact value of flt, which must be a float. 该类方法构造了一个表示flt精确值的Fraction,该分数必须是floatBeware that Fraction.from_float(0.3) is not the same value as Fraction(3, 10).注意,Fraction.from_float(0.3)Fraction(3, 10)的值不同。

Note

From Python 3.2 onwards, you can also construct a Fraction instance directly from a float.从Python 3.2开始,您还可以直接从float构造Fraction实例。

from_decimal(dec)

This class method constructs a Fraction representing the exact value of dec, which must be a decimal.Decimal instance.该类方法构造了一个Fraction,表示dec的精确值,该值必须是decimal.Decimal实例。

Note

From Python 3.2 onwards, you can also construct a Fraction instance directly from a decimal.Decimal instance.从Python 3.2开始,您还可以直接从decimal.Decimal实例构造Fraction实例。

limit_denominator(max_denominator=1000000)

Finds and returns the closest Fraction to self that has denominator at most max_denominator. 查找并返回与self最接近的Fraction,该分数的分母最多为max_denominatorThis method is useful for finding rational approximations to a given floating-point number:该方法有助于找到给定浮点数的有理近似:

>>> from fractions import Fraction
>>> Fraction('3.1415926535897932').limit_denominator(1000)
Fraction(355, 113)

or for recovering a rational number that’s represented as a float:或者用于恢复表示为浮点的有理数:

>>> from math import pi, cos
>>> Fraction(cos(pi/3))
Fraction(4503599627370497, 9007199254740992)
>>> Fraction(cos(pi/3)).limit_denominator()
Fraction(1, 2)
>>> Fraction(1.1).limit_denominator()
Fraction(11, 10)
__floor__()

Returns the greatest int <= self. 返回<= self的最大整数This method can also be accessed through the math.floor() function:也可以通过math.floor()函数访问此方法:

>>> from math import floor
>>> floor(Fraction(355, 113))
3
__ceil__()

Returns the least int >= self. 返回>= self的最小整数This method can also be accessed through the math.ceil() function.也可以通过math.ceil()函数访问此方法。

__round__()
__round__(ndigits)

The first version returns the nearest int to self, rounding half to even. 第一个版本返回最接近selfint,将其四舍五入到偶数。The second version rounds self to the nearest multiple of Fraction(1, 10**ndigits) (logically, if ndigits is negative), again rounding half toward even. 第二个版本将self四舍五入到Fraction(1, 10**ndigits)的最近倍数(逻辑上,如果ndigits为负),再次将一半四舍五入到偶数。This method can also be accessed through the round() function.也可以通过round()函数访问此方法。

See also

Module 模块numbers

The abstract base classes making up the numeric tower.构成数字塔的抽象基类。