Python fractions模块 —— 分数相关函数

简介: Python fractions模块 —— 分数相关函数

这是一简单的模块,搞不懂python为什么不把它并入math模块?

>>> import fractions
>>> fractions.__all__
['Fraction', 'gcd']
>>> fractions.gcd(12,18)
Warning (from warnings module):
  File "<pyshell#2>", line 1
DeprecationWarning: fractions.gcd() is deprecated. Use math.gcd() instead.
6
>>> fractions.gcd(14,6)
2
>>> import math
>>> math.gcd(14,6)
2
>>> 

一共就俩,其中一个最大公约数函数gcd()还不推荐使用,用math中的gcd替代。




fractions模块中的类Fraction


class Fraction(numbers.Rational)
 |  Fraction(numerator=0, denominator=None, *, _normalize=True)
 |  
 |  This class implements rational numbers.
 |  
 |  In the two-argument form of the constructor, Fraction(8, 6) will
 |  produce a rational number equivalent to 4/3. Both arguments must
 |  be Rational. The numerator defaults to 0 and the denominator
 |  defaults to 1 so that Fraction(3) == 3 and Fraction() == 0.
 |  
 |  Fractions can also be constructed from:
 |  
 |    - numeric strings similar to those accepted by the
 |      float constructor (for example, '-2.3' or '1e10')
 |  
 |    - strings of the form '123/456'
 |  
 |    - float and Decimal instances
 |  
 |    - other Rational instances (including integers)
 |  
 |  Method resolution order:
 |      Fraction
 |      numbers.Rational
 |      numbers.Real
 |      numbers.Complex
 |      numbers.Number
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  __abs__(a)
 |      abs(a)
 |  
 |  __add__(a, b)
 |      a + b
 |  
 |  __bool__(a)
 |      a != 0
 |  
 |  __ceil__(a)
 |      math.ceil(a)
 |  
 |  __copy__(self)
 |  
 |  __deepcopy__(self, memo)
 |  
 |  __divmod__(a, b)
 |      (a // b, a % b)
 |  
 |  __eq__(a, b)
 |      a == b
 |  
 |  __floor__(a)
 |      math.floor(a)
 |  
 |  __floordiv__(a, b)
 |      a // b
 |  
 |  __ge__(a, b)
 |      a >= b
 |  
 |  __gt__(a, b)
 |      a > b
 |  
 |  __hash__(self)
 |      hash(self)
 |  
 |  __le__(a, b)
 |      a <= b
 |  
 |  __lt__(a, b)
 |      a < b
 |  
 |  __mod__(a, b)
 |      a % b
 |  
 |  __mul__(a, b)
 |      a * b
 |  
 |  __neg__(a)
 |      -a
 |  
 |  __pos__(a)
 |      +a: Coerces a subclass instance to Fraction
 |  
 |  __pow__(a, b)
 |      a ** b
 |      
 |      If b is not an integer, the result will be a float or complex
 |      since roots are generally irrational. If b is an integer, the
 |      result will be rational.
 |  
 |  __radd__(b, a)
 |      a + b
 |  
 |  __rdivmod__(b, a)
 |      (a // b, a % b)
 |  
 |  __reduce__(self)
 |      Helper for pickle.
 |  
 |  __repr__(self)
 |      repr(self)
 |  
 |  __rfloordiv__(b, a)
 |      a // b
 |  
 |  __rmod__(b, a)
 |      a % b
 |  
 |  __rmul__(b, a)
 |      a * b
 |  
 |  __round__(self, ndigits=None)
 |      round(self, ndigits)
 |      
 |      Rounds half toward even.
 |  
 |  __rpow__(b, a)
 |      a ** b
 |  
 |  __rsub__(b, a)
 |      a - b
 |  
 |  __rtruediv__(b, a)
 |      a / b
 |  
 |  __str__(self)
 |      str(self)
 |  
 |  __sub__(a, b)
 |      a - b
 |  
 |  __truediv__(a, b)
 |      a / b
 |  
 |  __trunc__(a)
 |      trunc(a)
 |  
 |  as_integer_ratio(self)
 |      Return the integer ratio as a tuple.
 |      
 |      Return a tuple of two integers, whose ratio is equal to the
 |      Fraction and with a positive denominator.
 |  
 |  limit_denominator(self, max_denominator=1000000)
 |      Closest Fraction to self with denominator at most max_denominator.
 |      
 |      >>> Fraction('3.141592653589793').limit_denominator(10)
 |      Fraction(22, 7)
 |      >>> Fraction('3.141592653589793').limit_denominator(100)
 |      Fraction(311, 99)
 |      >>> Fraction(4321, 8765).limit_denominator(10000)
 |      Fraction(4321, 8765)
 |  
 |  ----------------------------------------------------------------------
 |  Class methods defined here:
 |  
 |  from_decimal(dec) from abc.ABCMeta
 |      Converts a finite Decimal instance to a rational number, exactly.
 |  
 |  from_float(f) from abc.ABCMeta
 |      Converts a finite float to a rational number, exactly.
 |      
 |      Beware that Fraction.from_float(0.3) != Fraction(3, 10).
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(cls, numerator=0, denominator=None, *, _normalize=True)
 |      Constructs a Rational.
 |      
 |      Takes a string like '3/2' or '1.5', another Rational instance, a
 |      numerator/denominator pair, or a float.
 |      
 |      Examples
 |      --------
 |      
 |      >>> Fraction(10, -8)
 |      Fraction(-5, 4)
 |      >>> Fraction(Fraction(1, 7), 5)
 |      Fraction(1, 35)
 |      >>> Fraction(Fraction(1, 7), Fraction(2, 3))
 |      Fraction(3, 14)
 |      >>> Fraction('314')
 |      Fraction(314, 1)
 |      >>> Fraction('-35/4')
 |      Fraction(-35, 4)
 |      >>> Fraction('3.1415') # conversion from numeric string
 |      Fraction(6283, 2000)
 |      >>> Fraction('-47e-2') # string may include a decimal exponent
 |      Fraction(-47, 100)
 |      >>> Fraction(1.47)  # direct construction from float (exact conversion)
 |      Fraction(6620291452234629, 4503599627370496)
 |      >>> Fraction(2.25)
 |      Fraction(9, 4)
 |      >>> Fraction(Decimal('1.47'))
 |      Fraction(147, 100)
 |  
 |  ----------------------------------------------------------------------
 |  Readonly properties defined here:
 |  
 |  denominator
 |  
 |  numerator
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __abstractmethods__ = frozenset()
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from numbers.Rational:
 |  
 |  __float__(self)
 |      float(self) = self.numerator / self.denominator
 |      
 |      It's important that this conversion use the integer's "true"
 |      division rather than casting one side to float before dividing
 |      so that ratios of huge integers convert without overflowing.
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from numbers.Real:
 |  
 |  __complex__(self)
 |      complex(self) == complex(float(self), 0)
 |  
 |  conjugate(self)
 |      Conjugate is a no-op for Reals.
 |  
 |  ----------------------------------------------------------------------
 |  Readonly properties inherited from numbers.Real:
 |  
 |  imag
 |      Real numbers have no imaginary component.
 |  
 |  real
 |      Real numbers are their real component.


函数实例在上述帮助中有......混一篇凑凑数 ^_^


目录
相关文章
|
2月前
|
开发者 Python
如何在Python中管理模块和包的依赖关系?
在实际开发中,通常会结合多种方法来管理模块和包的依赖关系,以确保项目的顺利进行和可维护性。同时,要及时更新和解决依赖冲突等问题,以保证代码的稳定性和可靠性
59 4
|
23天前
|
Python
Python Internet 模块
Python Internet 模块。
121 74
|
2月前
|
搜索推荐 Python
利用Python内置函数实现的冒泡排序算法
在上述代码中,`bubble_sort` 函数接受一个列表 `arr` 作为输入。通过两层循环,外层循环控制排序的轮数,内层循环用于比较相邻的元素并进行交换。如果前一个元素大于后一个元素,就将它们交换位置。
146 67
|
2月前
|
算法 数据安全/隐私保护 开发者
马特赛特旋转算法:Python的随机模块背后的力量
马特赛特旋转算法是Python `random`模块的核心,由松本真和西村拓士于1997年提出。它基于线性反馈移位寄存器,具有超长周期和高维均匀性,适用于模拟、密码学等领域。Python中通过设置种子值初始化状态数组,经状态更新和输出提取生成随机数,代码简单高效。
123 63
|
2月前
|
测试技术 Python
手动解决Python模块和包依赖冲突的具体步骤是什么?
需要注意的是,手动解决依赖冲突可能需要一定的时间和经验,并且需要谨慎操作,避免引入新的问题。在实际操作中,还可以结合使用其他方法,如虚拟环境等,来更好地管理和解决依赖冲突😉。
|
2天前
|
Python
[oeasy]python057_如何删除print函数_dunder_builtins_系统内建模块
本文介绍了如何删除Python中的`print`函数,并探讨了系统内建模块`__builtins__`的作用。主要内容包括: 1. **回忆上次内容**:上次提到使用下划线避免命名冲突。 2. **双下划线变量**:解释了双下划线(如`__name__`、`__doc__`、`__builtins__`)是系统定义的标识符,具有特殊含义。
16 3
|
2月前
|
持续交付 Python
如何在Python中自动解决模块和包的依赖冲突?
完全自动解决所有依赖冲突可能并不总是可行,特别是在复杂的项目中。有时候仍然需要人工干预和判断。自动解决的方法主要是提供辅助和便捷,但不能完全替代人工的分析和决策😉。
|
6天前
|
JSON 监控 安全
深入理解 Python 的 eval() 函数与空全局字典 {}
`eval()` 函数在 Python 中能将字符串解析为代码并执行,但伴随安全风险,尤其在处理不受信任的输入时。传递空全局字典 {} 可限制其访问内置对象,但仍存隐患。建议通过限制函数和变量、使用沙箱环境、避免复杂表达式、验证输入等提高安全性。更推荐使用 `ast.literal_eval()`、自定义解析器或 JSON 解析等替代方案,以确保代码安全性和可靠性。
19 2
|
1月前
|
Python
Python中的函数是**一种命名的代码块,用于执行特定任务或计算
Python中的函数是**一种命名的代码块,用于执行特定任务或计算
50 18
|
25天前
|
数据可视化 DataX Python
Seaborn 教程-绘图函数
Seaborn 教程-绘图函数
49 8