Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python cmath.isnan() Function



The Pythoncmath.isnan() function verifies whether a value is NaN(Not a Number) or not. This function returns the Boolean value, i.e., True if the value is NaN, otherwise False.

A number "x" is said to be NaN if it doesn't represent a real number and can't be expressed as a finite value, i.e., positive or negative infinity. NaN often arises a result of undefined operations, such as taking the square root of a negative number.

Syntax

Following is the basic syntax of the Pythoncmath.isnan() function −

cmath.isnan(x)

Parameters

This function accepts a numeric value as a parameter and redirects the value to be NaN.

Return Value

This function returns a boolean value which is True is the given number is Nan else it returns False.

Example 1

In the below example, we are verifying if the floating point number "20.5" is "NaN" usingcmath.isnan() function −

import cmathx = cmath.isnan(20.5)print(x)

Output

The output obtained is as follows −

False

Example 2

Here, we are rectifying if positive infinity is NaN usingcmath.isnan() function −

import cmathresult = cmath.isnan(float('inf'))print("The result is:",result)

Output

Following is the result for the above code −

The result is: False

Example 3

Now, when we use variable "x" to store NaN. Then thiscmath.isnan() function gives positive output.

import cmathx = float('nan')y = cmath.isnan(x)print(y)

Output

We will get the output as follows −

True

Example 4

In the following example, if we are passing string as the input, then thiscmath.isnan() function gives TypeError.

import cmathres = cmath.isnan("Welcome to Tutorialspoint")print(res)

Output

The result produced is as follows −

Traceback (most recent call last):  File "/home/cg/root/86486/main.py", line 2, in    res = cmath.isnan("Welcome to Tutorialspoint")TypeError: must be real number, not str
python_modules.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp