fractions
— Rational 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.分数实例可以由一对整数、另一个有理数或字符串构造。
-
class
fractions.
Fraction
(numerator=0, denominator=1)¶ -
class
fractions.
Fraction
(other_fraction) -
class
fractions.
Fraction
(float) -
class
fractions.
Fraction
(decimal) -
class
fractions.
Fraction
(string) The first version requires that numerator and denominator are instances of第一个版本要求分子和分母是numbers.Rational
and returns a newFraction
instance with valuenumerator/denominator
.numbers.Rational
的实例,并返回一个具有值numerator/denominator
的新分数实例。If denominator is如果denominator为0
, it raises aZeroDivisionError
.0
,则会产生ZeroDivisionError
。The second version requires that other_fraction is an instance of第二个版本要求other_fraction是numbers.Rational
and returns aFraction
instance with the same value.numbers.Rational
的实例,并返回具有相同值的Fraction
实例。The next two versions accept either a接下来的两个版本接受float
or adecimal.Decimal
instance, and return aFraction
instance with exactly the same value.float
或decimal.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 soFraction(1.1)
does not returnFraction(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 ‘-’ andnumerator
anddenominator
(if present) are strings of decimal digits.sign
可以是“+”或“-”,numerator
和denominator
(如果存在)是十进制数字串。In addition, any string that represents a finite value and is accepted by the此外,表示有限值并被float
constructor is also accepted by theFraction
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)TheFraction
class inherits from the abstract base classnumbers.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中更改:TheFraction
constructor now acceptsfloat
anddecimal.Decimal
instances.Fraction
构造函数现在接受float
实例和decimal.Decimal
实例。Changed in version 3.9:版本3.9中更改:Themath.gcd()
function is now used to normalize the numerator and denominator.math.gcd()
函数现在用于规范化numerator和denominator。math.gcd()
always return aint
type.math.gcd()
始终返回int
类型。Previously, the GCD type depended on numerator and denominator.以前,GCD类型取决于numerator和denominator。-
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该类方法构造了一个表示flt精确值的Fraction
representing the exact value of flt, which must be afloat
.Fraction
,该分数必须是float
。Beware that注意,Fraction.from_float(0.3)
is not the same value asFraction(3, 10)
.Fraction.from_float(0.3)
与Fraction(3, 10)
的值不同。
-
from_decimal
(dec)¶ This class method constructs a该类方法构造了一个Fraction
representing the exact value of dec, which must be adecimal.Decimal
instance.Fraction
,表示dec的精确值,该值必须是decimal.Decimal
实例。Note
From Python 3.2 onwards, you can also construct a从Python 3.2开始,您还可以直接从Fraction
instance directly from adecimal.Decimal
instance.decimal.Decimal
实例构造Fraction
实例。
-
limit_denominator
(max_denominator=1000000)¶ Finds and returns the closest查找并返回与Fraction
toself
that has denominator at most max_denominator.self
最接近的Fraction
,该分数的分母最多为max_denominator
。This 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
toself
, rounding half to even.self
的int
,将其四舍五入到偶数。The second version rounds第二个版本将self
to the nearest multiple ofFraction(1, 10**ndigits)
(logically, ifndigits
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.构成数字塔的抽象基类。