Python3 math.isnan() 方法 -判斷一個(gè)數(shù)是否是NAN

2023-04-26 14:01 更新

Python math 模塊 math 模塊


描述

math.isnan() 方法判斷數(shù)字是否為 NaN(非數(shù)字),如果數(shù)字是 NaN(不是數(shù)字),則返回 True ,否則返回 False 。

該函數(shù)要求python版本至少為Python3.5!


語法

math.isnan() 方法語法如下:

math.isnan(x)

參數(shù)說明:

  • x -- 必需,數(shù)字。如果 x 不是一個(gè)數(shù)字,返回 TypeError。

返回值

返回一個(gè)布爾值,如果數(shù)字是 NaN(不是數(shù)字),則返回 True ,否則返回 False 。


實(shí)例

以下實(shí)例檢查數(shù)字是否是 NaN(不是數(shù)字):

# 導(dǎo)入 math 包
import math

# 檢查數(shù)字是否為 NaN
print (math.isnan (56))
print (math.isnan (-45.34))
print (math.isnan (+45.34))
print (math.isnan (math.inf))
print (math.isnan (float("nan")))
print (math.isnan (float("inf")))
print (math.isnan (float("-inf")))
print (math.isnan (math.nan))

輸出結(jié)果:

False
False
False
False
True
False
False
True

Python math 模塊 math 模塊


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)