Python 关于正负无穷float(‘inf’)的一些用法
Python 关于正负无穷float(‘inf’)的一些用法
Python中可以用如下方式表示正负无穷:
float("inf"), float("-inf")
利用 inf 做简单加、乘算术运算仍会得到 inf
1 + float('inf')
inf
2 * float('inf')
inf
但是利用 inf 乘以0会得到 not-a-number(NaN):
0 * float("inf")
nan
除了inf外的其他数除以inf,会得到0
889 / float('inf')
0.0
float('inf')/float('inf')
nan
通常的运算是不会得到 inf值的
复制代码
2.02
4.0
_2
16.0
_2
256.0
_2
65536.0
_2
4294967296.0
_2
1.8446744073709552e+19
_2
3.4028236692093846e+38
_2
1.157920892373162e+77
_2
1.3407807929942597e+154
_2
Traceback (most recent call last):
File "", line 1, in ?
OverflowError: (34, 'Numerical result out of range')
复制代码
inf的运算规则遵从 IEEE-754 standard
不等式:
当涉及 > 和 < 运算时,
所有数都比-inf大
所有数都比+inf小
等式:
+inf 和 +inf相等
-inf 和 -inf相等
Refer to:
http://stackoverflow.com/questions/1628026/python-infinity-any-caveats
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步