我如何在python中表示一个无限的数字?无论你在程序中输入哪个数字,都不应该大于这个表示无穷大的数字。
当前回答
如果你使用SymPy,你也可以使用symphony .oo
>>> from sympy import oo
>>> oo + 1
oo
>>> oo - oo
nan
etc.
其他回答
在Python中,你可以这样做:
test = float("inf")
在Python 3.5中,你可以:
import math
test = math.inf
然后:
test > 1
test > 10000
test > x
永远是真的。当然,如前所述,除非x也是无穷大或“nan”(“不是一个数字”)。
此外(Python 2。x ONLY),与Ellipsis相比,float(inf)较小,例如:
float('inf') < Ellipsis
返回true。
NumPy库中有一个无穷大:from NumPy import inf。要得到负无穷大,可以简单地写入-inf。
总的来说,无穷有两种定义。
对于正无穷
posVal1 = math.inf
posVal2 = float("inf")
对于负无穷
negVal1 = -math.inf
negVal2 = float("-inf")
∞
1. 使用float('inf')和float('-inf)
positive_infinity = float('inf')
negative_infinity = float('-inf')
2. 使用Python的数学模块
import math
positive_infinity = math.inf
negative_infinity = -math.inf
3.整数的最大尺寸
import sys
maxSize = sys.maxsize # 9223372036854775807
minSize = -sys.maxsize # -9223372036854775807
4. 使用Python的十进制模块
from decimal import Decimal
positive_infinity = Decimal('Infinity')
negative_infinity = Decimal('-Infinity')
我不知道你到底在做什么,但float("inf")给你一个浮点无穷大,这比任何其他数字都大。
推荐文章
- 插入一行到熊猫数据框架
- 要列出Pandas DataFrame列
- 在Django模型中存储电话号码的最佳方法是什么?
- 从导入的模块中模拟函数
- 滚动或滑动窗口迭代器?
- python的方法找到最大值和它的索引在一个列表?
- 如何读取文件的前N行?
- 如何删除matplotlib中的顶部和右侧轴?
- 解析.py文件,读取AST,修改它,然后写回修改后的源代码
- Visual Studio Code:如何调试Python脚本的参数
- 使用元组/列表等等。从输入vs直接引用类型如list/tuple/etc
- 结合conda环境。Yml和PIP requirements.txt
- 将命名元组转换为字典
- 如何使x轴和y轴的刻度相等呢?
- Numpy在这里函数多个条件