新聞中心
fractions —- 分數(shù)
源代碼 Lib/fractions.py

成都創(chuàng)新互聯(lián)公司主營武陵網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,APP應(yīng)用開發(fā),武陵h5小程序開發(fā)搭建,武陵網(wǎng)站營銷推廣歡迎武陵等地區(qū)企業(yè)咨詢
fractions 模塊支持分數(shù)運算。
分數(shù)實例可以由一對整數(shù),一個分數(shù),或者一個字符串構(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)
第一個版本要求 numerator 和 denominator 是 numbers.Rational 的實例,并返回一個新的 Fraction 實例,其值為 numerator/denominator。 如果 denominator 為 0 將會引發(fā) ZeroDivisionError。 第二個版本要求 other_fraction 是 numbers.Rational 的實例,并返回一個 Fraction 實例且與傳入值相等。 下兩個版本接受 float 或 decimal.Decimal 的實例,并返回一個 Fraction 實例且與傳入值完全相等。 請注意由于二進制浮點數(shù)通常存在的問題 (參見 浮點算術(shù):爭議和限制),Fraction(1.1) 的參數(shù)并不會精確等于 11/10,因此 Fraction(1.1) 也 不會 返回用戶所期望的 Fraction(11, 10)。 (請參閱下文中 limit_denominator() 方法的文檔。) 構(gòu)造器的最后一個版本接受一個字符串或 unicode 實例。 此實例的通常形式為:
[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,并實現(xiàn)了該類的所有方法和操作。 Fraction 實例是可哈希的,并應(yīng)當被視為不可變對象。 此外,F(xiàn)raction 還具有以下屬性和方法:
在 3.2 版更改: Fraction 構(gòu)造器現(xiàn)在接受 float 和 decimal.Decimal 實例。
在 3.9 版更改: 現(xiàn)在會使用 math.gcd() 函數(shù)來正規(guī)化 numerator 和 denominator。 math.gcd() 總是返回 int 類型。 在之前版本中,GCD 的類型取決于 numerator 和 denominator 的類型。
在 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
最簡分數(shù)形式的分子。
-
denominator
最簡分數(shù)形式的分母。
-
as_integer_ratio()
返回由兩個整數(shù)組成的元組,兩數(shù)之比等于該分數(shù)的值且其分母為正數(shù)。
3.8 新版功能.
-
classmethod from_float(flt)
只接受 float 或 numbers.Integral 實例的替代性構(gòu)造器。 請注意
Fraction.from_float(0.3)與Fraction(3, 10)的值是不同的。備注
從 python 3.2 開始,在構(gòu)造 Fraction 實例時可以直接使用 float。
-
classmethod from_decimal(dec)
只接受 decimal.Decimal 或 numbers.Integral 實例的替代性構(gòu)造器。
備注
從 Python 3.2 開始,在構(gòu)造 Fraction 實例時可以直接使用 decimal.Decimal 實例。
-
limit_denominator(max_denominator=1000000)
找到并返回一個 Fraction 使得其值最接近
self并且分母不大于 max_denominator。 此方法適用于找出給定浮點數(shù)的有理數(shù)近似值:>>> from fractions import Fraction>>> Fraction('3.1415926535897932').limit_denominator(1000)Fraction(355, 113)
或是用來恢復(fù)被表示為一個浮點數(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)
第一個版本返回一個 int 使得其值最接近
self,位值為二分之一時只對偶數(shù)舍入。第二個版本會將self舍入到最接近Fraction(1, 10**ndigits)的倍數(shù)(如果ndigits為負值則為邏輯運算),位值為二分之一時同樣只對偶數(shù)舍入。 此方法也可通過 round() 函數(shù)來使用。
參見
numbers 模塊
構(gòu)成數(shù)字塔的所有抽象基類。
分享標題:創(chuàng)新互聯(lián)Python教程:fractions—-分數(shù)
當前路徑:http://www.fisionsoft.com.cn/article/cogjhde.html


咨詢
建站咨詢
