首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用Python单独测试正无穷大或负无穷大

用Python单独测试正无穷大或负无穷大
EN

Stack Overflow用户
提问于 2015-01-23 02:55:29
回答 3查看 77.4K关注 0票数 47

正无穷大或负无穷大的math.isinf()检验集中在一起。怎样才能清楚地检测出它们呢?

检验正无穷大的方法:

  1. x == float('+inf')
  2. math.isinf(x) and x > 0

检验负无穷大的方法:

  1. x == float('-inf')
  2. math.isinf(x) and x < 0

拆卸方式1:

代码语言:javascript
运行
复制
>>> def ispinf1(x): return x == float("inf")
...
>>> dis.dis(ispinf1)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_GLOBAL              0 (float)
              6 LOAD_CONST               1 ('inf')
              9 CALL_FUNCTION            1
             12 COMPARE_OP               2 (==)
             15 RETURN_VALUE

拆卸方式2:

代码语言:javascript
运行
复制
>>> def ispinf2(x): return isinf(x) and x > 0
...
>>> dis.dis(ispinfs)
  1           0 LOAD_GLOBAL              0 (isinf)
              3 LOAD_FAST                0 (x)
              6 CALL_FUNCTION            1
              9 JUMP_IF_FALSE_OR_POP    21
             12 LOAD_FAST                0 (x)
             15 LOAD_CONST               1 (0)
             18 COMPARE_OP               4 (>)
        >>   21 RETURN_VALUE

这个答案似乎倾向于方式2,除了x>0。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-01-23 03:11:40

"pythonic“的方法是使用可读性和可维护性。

尽管如此,对于我来说,x == float("inf")x == float("-inf")的可读性稍强一些,我更喜欢它们。math.isinf(x) and x > 0更快,但每次调用只需40纳秒左右。

所以,除非你检查大量的数字,否则运行时间不会有太大的变化。

票数 51
EN

Stack Overflow用户

发布于 2018-03-15 15:16:28

还有numpy

代码语言:javascript
运行
复制
>>> import numpy as np
>>> np.isneginf([np.inf, 0, -np.inf])
array([False, False,  True], dtype=bool)
>>> np.isposinf([np.inf, 0, -np.inf])
array([ True, False, False], dtype=bool)

然后是通用的isinf

代码语言:javascript
运行
复制
>>> np.isinf([np.inf, 0, -np.inf])
array([ True, False,  True], dtype=bool)
票数 18
EN

Stack Overflow用户

发布于 2021-12-17 08:24:43

下面是一些jupyterlab计时测试,以查看最快的方式(从最慢到最快):

准备工作:

代码语言:javascript
运行
复制
import math
import numpy as np
n = 100000000
a = list(range(n))
a.extend([np.inf, float('inf'), math.inf])

结果:

代码语言:javascript
运行
复制
%%time
def inf_to_none(x):
    if np.isinf(x):
        return None
    else:
        return x
r = list(map(inf_to_none, a))

>> CPU times: user 1min 30s, sys: 481 ms, total: 1min 31s
Wall time: 1min 31s


%%time
def inf_to_none(x):
    if x == float('inf'):
        return None
    else:
        return x
r = list(map(inf_to_none, a))

>> CPU times: user 19.6 s, sys: 494 ms, total: 20.1 s
Wall time: 20.2 s


%%time
def inf_to_none(x):
    if math.isinf(x):
        return None
    else:
        return x
r = list(map(inf_to_none_math, a))

>> CPU times: user 15 s, sys: 292 ms, total: 15.3 s
Wall time: 15.3 s


%%time
d = {np.inf: None}
l = lambda x: d.get(x,x)
r = list(map(l, a))

>> CPU times: user 11.7 s, sys: 256 ms, total: 12 s
Wall time: 12 s


%%time
def inf_to_none(x):
    if x == np.inf:
        return None
    else:
        return x
r = list(map(inf_to_none, a))

>> CPU times: user 11.2 s, sys: 280 ms, total: 11.5 s
Wall time: 11.5 s


%%time
def inf_to_none(x):
    if x == math.inf:
        return None
    else:
        return x
r = list(map(inf_to_none, a))

>> CPU times: user 11 s, sys: 276 ms, total: 11.3 s
Wall time: 11.3 s

我认为这些结果非常有趣,让它简短地使用==比较。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28102302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档