Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a variable NaN in python without numpy

Most languages have a NaN constant you can use to assign a variable the value NaN. Can python do this without using numpy?

like image 718
zelinka Avatar asked Oct 15 '13 06:10

zelinka


People also ask

Does Python have NaN?

The math. nan value is new in Python 3.5. NaN values can also be created in any Python version using float('nan') .


2 Answers

Yes -- use math.nan.

>>> from math import nan >>> print(nan) nan >>> print(nan + 2) nan >>> nan == nan False >>> import math >>> math.isnan(nan) True 

Before Python 3.5, one could use float("nan") (case insensitive).

Note that checking to see if two things that are NaN are equal to one another will always return false. This is in part because two things that are "not a number" cannot (strictly speaking) be said to be equal to one another -- see What is the rationale for all comparisons returning false for IEEE754 NaN values? for more details and information.

Instead, use math.isnan(...) if you need to determine if a value is NaN or not.

Furthermore, the exact semantics of the == operation on NaN value may cause subtle issues when trying to store NaN inside container types such as list or dict (or when using custom container types). See Checking for NaN presence in a container for more details.


You can also construct NaN numbers using Python's decimal module:

>>> from decimal import Decimal >>> b = Decimal('nan') >>> print(b) NaN >>> print(repr(b)) Decimal('NaN') >>> >>> Decimal(float('nan')) Decimal('NaN') >>>  >>> import math >>> math.isnan(b) True 

math.isnan(...) will also work with Decimal objects.


However, you cannot construct NaN numbers in Python's fractions module:

>>> from fractions import Fraction >>> Fraction('nan') Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "C:\Python35\lib\fractions.py", line 146, in __new__     numerator) ValueError: Invalid literal for Fraction: 'nan' >>> >>> Fraction(float('nan')) Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "C:\Python35\lib\fractions.py", line 130, in __new__     value = Fraction.from_float(numerator)   File "C:\Python35\lib\fractions.py", line 214, in from_float     raise ValueError("Cannot convert %r to %s." % (f, cls.__name__)) ValueError: Cannot convert nan to Fraction. 

Incidentally, you can also do float('Inf'), Decimal('Inf'), or math.inf (3.5+) to assign infinite numbers. (And also see math.isinf(...))

However doing Fraction('Inf') or Fraction(float('inf')) isn't permitted and will throw an exception, just like NaN.

If you want a quick and easy way to check if a number is neither NaN nor infinite, you can use math.isfinite(...) as of Python 3.2+.


If you want to do similar checks with complex numbers, the cmath module contains a similar set of functions and constants as the math module:

  • cmath.isnan(...)
  • cmath.isinf(...)
  • cmath.isfinite(...) (Python 3.2+)
  • cmath.nan (Python 3.6+; equivalent to complex(float('nan'), 0.0))
  • cmath.nanj (Python 3.6+; equivalent to complex(0.0, float('nan')))
  • cmath.inf (Python 3.6+; equivalent to complex(float('inf'), 0.0))
  • cmath.infj (Python 3.6+; equivalent to complex(0.0, float('inf')))
like image 103
Michael0x2a Avatar answered Oct 12 '22 03:10

Michael0x2a


nan = float('nan') 

And now you have the constant, nan.

You can similarly create NaN values for decimal.Decimal.:

dnan = Decimal('nan') 
like image 44
abarnert Avatar answered Oct 12 '22 01:10

abarnert