fractions --- 分?jǐn)?shù)?

源代碼 Lib/fractions.py


fractions 模塊支持分?jǐn)?shù)運(yùn)算。

分?jǐn)?shù)實(shí)例可以由一對(duì)整數(shù),一個(gè)分?jǐn)?shù),或者一個(gè)字符串構(gòu)建而成。

class fractions.Fraction(numerator=0, denominator=1)?
class fractions.Fraction(other_fraction)
class fractions.Fraction(float)
class fractions.Fraction(decimal)
class fractions.Fraction(string)

第一個(gè)版本要求 numeratordenominatornumbers.Rational 的實(shí)例,并返回一個(gè)新的 Fraction 實(shí)例,其值為 numerator/denominator。 如果 denominator0 將會(huì)引發(fā) ZeroDivisionError。 第二個(gè)版本要求 other_fractionnumbers.Rational 的實(shí)例,并返回一個(gè) Fraction 實(shí)例且與傳入值相等。 下兩個(gè)版本接受 floatdecimal.Decimal 的實(shí)例,并返回一個(gè) Fraction 實(shí)例且與傳入值完全相等。 請(qǐng)注意由于二進(jìn)制浮點(diǎn)數(shù)通常存在的問題 (參見 浮點(diǎn)算術(shù):爭(zhēng)議和限制),Fraction(1.1) 的參數(shù)并不會(huì)精確等于 11/10,因此 Fraction(1.1)不會(huì) 返回用戶所期望的 Fraction(11, 10)。 (請(qǐng)參閱下文中 limit_denominator() 方法的文檔。) 構(gòu)造器的最后一個(gè)版本接受一個(gè)字符串或 unicode 實(shí)例。 此實(shí)例的通常形式為:

[sign] numerator ['/' denominator]

where the optional sign may be either '+' or '-' and numerator and denominator (if present) are strings of decimal digits (underscores may be used to delimit digits as with integral literals in code). In addition, any string that represents a finite value and is accepted by the float constructor is also accepted by the Fraction constructor. 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)

Fraction 類繼承自抽象基類 numbers.Rational,并實(shí)現(xiàn)了該類的所有方法和操作。 Fraction 實(shí)例是可哈希的,并應(yīng)當(dāng)被視為不可變對(duì)象。 此外,Fraction 還具有以下屬性和方法:

在 3.2 版更改: Fraction 構(gòu)造器現(xiàn)在接受 floatdecimal.Decimal 實(shí)例。

在 3.9 版更改: 現(xiàn)在會(huì)使用 math.gcd() 函數(shù)來正規(guī)化 numeratordenominatormath.gcd() 總是返回 int 類型。 在之前版本中,GCD 的類型取決于 numeratordenominator 的類型。

在 3.11 版更改: Underscores are now permitted when creating a Fraction instance from a string, following PEP 515 rules.

在 3.11 版更改: Fraction implements __int__ now to satisfy typing.SupportsInt instance checks.

numerator?

最簡(jiǎn)分?jǐn)?shù)形式的分子。

denominator?

最簡(jiǎn)分?jǐn)?shù)形式的分母。

as_integer_ratio()?

返回由兩個(gè)整數(shù)組成的元組,兩數(shù)之比等于該分?jǐn)?shù)的值且其分母為正數(shù)。

3.8 新版功能.

classmethod from_float(flt)?

Alternative constructor which only accepts instances of float or numbers.Integral. Beware that Fraction.from_float(0.3) is not the same value as Fraction(3, 10).

備注

從 Python 3.2 開始,在構(gòu)造 Fraction 實(shí)例時(shí)可以直接使用 float

classmethod from_decimal(dec)?

Alternative constructor which only accepts instances of decimal.Decimal or numbers.Integral.

備注

從 Python 3.2 開始,在構(gòu)造 Fraction 實(shí)例時(shí)可以直接使用 decimal.Decimal 實(shí)例。

limit_denominator(max_denominator=1000000)?

找到并返回一個(gè) Fraction 使得其值最接近 self 并且分母不大于 max_denominator。 此方法適用于找出給定浮點(diǎn)數(shù)的有理數(shù)近似值:

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

或是用來恢復(fù)被表示為一個(gè)浮點(diǎn)數(shù)的有理數(shù):

>>>
>>> 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__()?

返回最大的 int <= self。 此方法也可通過 math.floor() 函數(shù)來使用:

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

返回最小的 int >= self。 此方法也可通過 math.ceil() 函數(shù)來使用。

__round__()?
__round__(ndigits)

第一個(gè)版本返回一個(gè) int 使得其值最接近 self,位值為二分之一時(shí)只對(duì)偶數(shù)舍入。第二個(gè)版本會(huì)將 self 舍入到最接近 Fraction(1, 10**ndigits) 的倍數(shù)(如果 ndigits 為負(fù)值則為邏輯運(yùn)算),位值為二分之一時(shí)同樣只對(duì)偶數(shù)舍入。 此方法也可通過 round() 函數(shù)來使用。

參見

numbers 模塊

構(gòu)成數(shù)字塔的所有抽象基類。