首页 > 学院 > 开发设计 > 正文

Python关于正负无穷float(‘inf’)的一些用法

2019-11-14 17:33:54
字体:
来源:转载
供稿:网友

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.0**24.0>>> _**216.0>>> _**2256.0>>> _**265536.0>>> _**24294967296.0>>> _**21.8446744073709552e+19>>> _**23.4028236692093846e+38>>> _**21.157920892373162e+77>>> _**21.3407807929942597e+154>>> _**2Traceback (most recent call last):  File "<stdin>", 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

 


上一篇:Python连接Oracle

下一篇:Python之正则

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表