fractions --- 分數?

源代碼 Lib/fractions.py


fractions 模塊支持分數運算。

分數實(shí)例可以由一對整數,一個(gè)分數,或者一個(gè)字符串構建而成。

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í)例且與傳入值完全相等。 請注意由于二進(jìn)制浮點(diǎn)數通常存在的問(wèn)題 (參見(jiàn) 浮點(diǎn)算術(shù):爭議和限制),Fraction(1.1) 的參數并不會(huì )精確等于 11/10,因此 Fraction(1.1)不會(huì ) 返回用戶(hù)所期望的 Fraction(11, 10)。 (請參閱下文中 limit_denominator() 方法的文檔。) 構造器的最后一個(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 類(lèi)繼承自抽象基類(lèi) numbers.Rational,并實(shí)現了該類(lèi)的所有方法和操作。 Fraction 實(shí)例是可哈希的,并應當被視為不可變對象。 此外,Fraction 還具有以下屬性和方法:

在 3.2 版更改: Fraction 構造器現在接受 floatdecimal.Decimal 實(shí)例。

在 3.9 版更改: 現在會(huì )使用 math.gcd() 函數來(lái)正規化 numeratordenominator。 math.gcd() 總是返回 int 類(lèi)型。 在之前版本中,GCD 的類(lèi)型取決于 numeratordenominator 的類(lèi)型。

在 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)分數形式的分子。

denominator?

最簡(jiǎn)分數形式的分母。

as_integer_ratio()?

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

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 開(kāi)始,在構造 Fraction 實(shí)例時(shí)可以直接使用 float。

classmethod from_decimal(dec)?

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

備注

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

limit_denominator(max_denominator=1000000)?

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

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

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

>>>
>>> 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。 此方法也可通過(guò) math.floor() 函數來(lái)使用:

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

返回最小的 int >= self。 此方法也可通過(guò) math.ceil() 函數來(lái)使用。

__round__()?
__round__(ndigits)

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

參見(jiàn)

numbers 模塊

構成數字塔的所有抽象基類(lèi)。