Testing for positive infinity, or negative infinity, individually in Python

前端 未结 2 1231
花落未央
花落未央 2021-02-02 08:02

math.isinf() tests for positive or negative infinity lumped together. What\'s the pythonic way to test for them distinctly?

Ways to test for positive infinity:

2条回答
  •  我在风中等你
    2021-02-02 08:25

    there is also numpy

    >>> 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)
    

    and then there is general isinf

    >>> np.isinf([np.inf, 0, -np.inf])
    array([ True, False,  True], dtype=bool)
    

提交回复
热议问题