python math模块的isfinite()方法,可以用于判断一个值是否为无穷大或者为NaN,如果都不是,则返回True,否则返回False。
isfinite(x, /)
python源码中对isfinite()方法的介绍:
Return True if x is neither an infinity nor a NaN, and False otherwise.
参数 | 描述 |
---|---|
x | real number,如int、float类型的值。 |
提示:如果参数x不是real number,python将抛出TypeError。
Python布尔类型True or False。
>>> import math
>>> math.isfinite(math.inf)
False
>>> math.isfinite(1)
True
>>> math.isfinite(-float('inf'))
False
>>> math.isfinite('2')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be real number, not str
全栈编程 / Python库(可向下滚动):
Copyright © 2022-2023 笨鸟工具 x1y1z1.com All Rights Reserved.