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

前端 未结 2 1233
花落未央
花落未央 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:38

    The "pythonic" way is to go with what's readable and maintainable.

    That said, x == float("inf") and x == float("-inf") are slightly more readable to me, and I'd prefer them. math.isinf(x) and x > 0 is faster, but only on the order of about 40 nanoseconds per call.

    So unless you're checking a whole lot of numbers, it isn't going to make much of a difference in running time.

提交回复
热议问题