如何在Python中表示无限数?无论您在程序中输入哪个数字,任何数字都不应大于这个无穷大的表示形式。
在Python中,你可以这样做:
test = float("inf")
在Python 3.5中,你可以这样做:
import math
test = math.inf
然后:
test > 1
test > 10000
test > x
永远都是真的。当然,除非如所指出的那样,x 也是无穷大或“nan”(“不是数字”)。
此外(仅限 Python 2.x),与
Ellipsis
相比,float(inf)
较小,例如:
float('inf') < Ellipsis
将返回true。
从Python 3.5开始你可以使用
math.inf
:
>>> import math
>>> math.inf
inf
似乎没有人明确提到负无穷大,所以我想我应该添加它。
对于负无穷大:
-math.inf
对于正无穷大(只是为了完整性):
math.inf
我不知道你到底在做什么,但是
float("inf")
给你一个浮点无穷大,它比任何其他数字都大。
NumPy 库中有无穷大:
from numpy import inf
。要获得负无穷大,只需写 -inf
。
Decimal
类:
from decimal import Decimal
pos_inf = Decimal('Infinity')
neg_inf = Decimal('-Infinity')
在 python2.x 中,有一个肮脏的 hack 可以达到此目的(除非绝对必要,否则不要使用它):
None < any integer < any string
因此,对于任何整数
i < ''
,检查 True
都保持 i
。
它在 python3 中已被合理弃用。现在这样的比较最终是
TypeError: unorderable types: str() < int()
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.整数
maxsize
import sys
maxSize = sys.maxsize
minSize = -sys.maxsize
4。使用Python的decimal模块
from decimal import Decimal
positive_infinity = Decimal('Infinity')
negative_infinity = Decimal('-Infinity')
5。使用 Numpy 库
from numpy import inf
positive_infinity = inf
negative_infinity = -inf
如果您使用 SymPy,您也可以使用
sympy.oo
>>> from sympy import oo
>>> oo + 1
oo
>>> oo - oo
nan
等
对于正无穷
pos_inf_val = float("infinity")
对于负无穷
neg_inf_val = float("-infinity")
代表 python
中的∞
float("inf")
或 float("INF")
或 float("Inf")
或 float("inF")
或 float("infinity")
或 float("Infinity")
创建一个持有 ∞的
float
对象
您还可以在 python
中表示-∞
float("-inf")
或 float("-INF")
或 float("-Inf")
或 float("-infinity")
创建一个持有 -∞ 的浮动对象
您可以执行算术运算:
infinity = float("inf")
ninfinity = float("-inf")
nan = float("nan")
print(infinity*infinity)#inf
print(ninfinity+infinity)#not a number
print(1/-infinity)#is -0.0
print(nan*nan)# is not a number
print(1/infinity) # is 0.0 since 1/∞ is 0
$ python3 floating.py
inf
nan
-0.0
nan
0.0
总而言之,无穷大有两种定义。
对于正无穷
posVal1 = math.inf
posVal2 = float("inf")
对于负无穷
negVal1 = -math.inf
negVal2 = float("-inf")
用途:
float('inf')
或者数学模块:
import math
math.inf
但是如果你打印它,它们都会返回
inf
,这证明math
也使用float('inf')
。
对于
float
类型:
import sys
import math
import numpy as np
import torch
print(float('inf')) # inf
print(float('-inf')) # -inf
print(float('infinity')) # inf
print(float('-infinity')) # -inf
print(1.7976931348623157e+309) # inf
print(-1.7976931348623157e+309) # -inf
print(sys.float_info.max * 2) # inf
print(-sys.float_info.max * 2) # -inf
print(sys.float_info.max * -2) # -inf
print(math.inf) # inf
print(-math.inf) # -inf
print(np.inf) # inf
print(-np.inf) # -inf
print(torch.inf) # inf
print(-torch.inf) # -inf
对于
complex
类型:
print(complex('inf+infj')) # (inf+infj)
print(complex('inf-infj')) # (inf-infj)
print(complex('-inf+infj')) # (-inf+infj)
print(complex('-inf-infj')) # (-inf-infj)
print(complex('infinity+infinityj')) # (inf+infj)
print(complex('infinity-infinityj')) # (inf-infj)
print(complex('-infinity+infinityj')) # (-inf+infj)
print(complex('-infinity-infinityj')) # (-inf-infj)
print(complex('inf')) # (inf+0j)
print(complex('inf+0j')) # (inf+0j)
print(complex('infinity')) # (inf+0j)
print(complex('infinity+0j')) # (inf+0j)
print(complex('-inf')) # (-inf+0j)
print(complex('-inf+0j')) # (-inf+0j)
print(complex('-infinity')) # (-inf+0j)
print(complex('-infinity+0j')) # (-inf+0j)
print(1.7976931348623157e+309+1.7976931348623157e+309j) # (inf+infj)
print(1.7976931348623157e+309-1.7976931348623157e+309j) # (inf-infj)
print(-1.7976931348623157e+309+1.7976931348623157e+309j) # (-inf+infj)
print(-1.7976931348623157e+309-1.7976931348623157e+309j) # (-inf-infj)
对于
decimal.Decimal
类型:
from decimal import Decimal
print(Decimal('inf')) # Infinity
print(Decimal('infinity')) # Infinity
print(Decimal('-inf')) # -Infinity
print(Decimal('-infinity')) # -Infinity