Use math.isnan:
>>> import math
>>> x = float('nan')
>>> math.isnan(x)
True
Answer from gimel on Stack OverflowPython
docs.python.org › 3 › library › math.html
math — Mathematical functions — Python 3.15.2 documentation
math.isnan(x)¶ · Return True if x is a NaN (not a number), and False otherwise. math.ldexp(x, i)¶ · Return x * (2**i). This is essentially the inverse of function frexp(). math.nextafter(x, y, steps=1)¶ · Return the floating-point value ...
W3Schools
w3schools.com › python › ref_math_isnan.asp
Python math.isnan() Method
The math.isnan() method checks whether a value is NaN (Not a Number), or not.
Codecademy
codecademy.com › docs › python › math module › math.isnan()
Python | Math Module | math.isnan() | Codecademy
September 4, 2024 - In Python, the math.isnan() function returns True if the given value is NaN (not a number) and False if the value is a number.
Tutorialspoint
tutorialspoint.com › python › python_math_isnan_method.htm
Python math.isnan() Method
The Python math.isnan() method is used to determine whether a given number NaN (Not a Number). It returns "True" if the number is NaN, and "False" otherwise. Generally, a number "x" is considered NaN if it does not represent a valid real number and
Interactive Chaos
interactivechaos.com › en › python › function › mathisnan
math.isnan | Interactive Chaos
The math.isnan function returns the Boolean True if the number passed as an argument is a NaN (Not a Number), or False otherwise.
Python Examples
pythonexamples.org › python-math-isnan
Python math.isnan() - Check if Number is NaN
import math x = math.nan result = math.isnan(x) print('isnan(x) :', result)
Reddit
reddit.com › r/learnpython › math.isnan()
r/learnpython on Reddit: math.isnan()
December 6, 2013 -
does this EVER return true? gives error on 1/0 and arcsin(2) and '123' and 'abc' and so forth. says it needs a float. but a float is a number, right?
Top answer 1 of 2
4
I think you're confused about what NaN represents. NaN is a special value of a IEEE-754 floating point number that signifies that the result of a floating point computation does not exist (the spec also defines representations for positive and negative infinity). math.isnan will check if a value is this special NaN value, not whether the provided value is of numeric type. In most cases where a NaN would be generated, Python chooses to raise an exception instead of returning NaN (you noticed this is the case for division by zero, but it also applies to taking the square root of a negative number or providing input outside of the domain of inverse trigonometric functions). This behavior differs from some other languages. See also: CPython implementation detail about NaN , Wikipedia article on NaN
2 of 2
3
A float can basically be either a number, positive or negative infinity, or NaN. You can think of NaN and the infinities as special values that a float can have. math.isnan() takes a float and tells you whether its value is NaN. The Python math library is a wrapper around a C library. The corresponding functions in the C library return NaN for many things, but the Python wrapper checks for most (maybe all) of these and raises an exception instead of returning NaN. This is done to make the math module fit in better with the rest of Python, which prefers to raise exceptions instead of returning special values. So, most of the time there's no use for math.isnan() in pure Python code (because Python raises exceptions instead of returning NaN). However, it may be needed in cases where Python code has to process data from external sources (which might produce NaN).
GeeksforGeeks
geeksforgeeks.org › python-math-library-isnan-method
Python math library | isnan() method | GeeksforGeeks
February 20, 2023 - Syntax : math.isnan(x) Parameters : x [Required] : It is any valid python data type or any number. Returns: Return type is boolean.
IncludeHelp
includehelp.com › python › math-isnan-method-with-example.aspx
math.isnan() method with example in Python
math.isnan() method is a library method of math module, it is used to check whether a given number is "NaN" (Not a Number) or not, it accepts a number and returns True if given number is "NaN", else it returns False.
Educative
educative.io › answers › what-is-mathisnan-in-python
What is math.isnan() in python?
The math module in Python provides access to mathematical functions and constants. math.isnan() checks whether a value is a number or not.
Bomberbot
bomberbot.com › python › mastering-pythons-math-isnan-the-ultimate-guide-for-number-validation-and-beyond
Mastering Python's math.isnan(): The Ultimate Guide for Number Validation and Beyond - Bomberbot
The implementation of isnan() leverages the peculiar property of NaN that it's not equal to itself. While a simplified version might look like this: ... The actual implementation in Python's C source code is more nuanced, taking into account various floating-point representations and edge cases to ensure accuracy across different systems and architectures. The utility of math.isnan() extends far beyond simple number checking.