相关疑难解决方法(0)

在Python中单独测试正无穷大或负无穷大

math.isinf()测试正或负无限集中在一起.什么是pythonic方式明确地测试它们?

测试正无穷大的方法:

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

测试负无穷大的方法:

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

拆卸方式1:

>>> 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
Run Code Online (Sandbox Code Playgroud)

拆卸方式2:

>>> 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 …
Run Code Online (Sandbox Code Playgroud)

python infinity

23
推荐指数
2
解决办法
4万
查看次数

标签 统计

infinity ×1

python ×1