在没有numpy的python中分配变量NaN [英] Assigning a variable NaN in python without numpy
问题描述
大多数语言都有一个NaN常量,您可以使用它来给变量赋值NaN. python可以在不使用numpy的情况下做到这一点吗?
Most languages have a NaN constant you can use to assign a variable the value NaN. Can python do this without using numpy?
推荐答案
是-使用float('nan')
.从Python 3.5开始,您还可以使用 math.nan
.
Yes -- use float('nan')
. As of Python 3.5, you can also use math.nan
.
>>> a = float('nan')
>>> print(a)
nan
>>> print(a + 2)
nan
>>> a == a
False
>>> import math
>>> math.isnan(a)
True
>>> # Python 3.5+
>>> math.isnan(math.nan)
True
float(...)
函数不区分大小写-执行float('NAN')
或float('naN')
或类似操作也可以.
The float(...)
function is case-insensitive -- doing float('NAN')
or float('naN')
or similar things will also work.
请注意,检查两个NaN是否彼此相等将始终返回false.部分原因是不能(严格地说)说两个不是数字的事物彼此相等-参见对于所有比较,对于IEEE754 NaN值返回false的理由是什么?以获取更多详细信息.
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.
如果需要,请使用 math.isnan(...)
确定值是否为NaN.
Instead, use math.isnan(...)
if you need to determine if a value is NaN or not.
此外,当尝试将NaN存储在诸如list
或dict
之类的容器类型中(或使用自定义容器类型)时,对NaN值的==
操作的确切语义可能会引起细微问题.有关更多详细信息,请参见检查容器中是否存在NaN .
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.
您还可以使用Python的十进制模块构造NaN数字:
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(...)
也可用于Decimal对象.
math.isnan(...)
will also work with Decimal objects.
但是,您不能在Python的分数模块:
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.
顺便说一句,您还可以执行float('Inf')
,Decimal('Inf')
或 math.inf
(3.5+)分配无限数. (另请参见 math.isinf(...)
)
Incidentally, you can also do float('Inf')
, Decimal('Inf')
, or math.inf
(3.5+) to assign infinite numbers. (And also see math.isinf(...)
)
但是,不允许执行Fraction('Inf')
或Fraction(float('inf'))
,并且会抛出异常,就像NaN一样.
However doing Fraction('Inf')
or Fraction(float('inf'))
isn't permitted and will throw an exception, just like NaN.
如果您想要一种快速简便的方法来检查数字是否既不是NaN也不是无限,则可以使用 math.isfinite(...)
.
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+.
如果要使用复数进行类似的检查,则cmath
模块包含与math
模块类似的一组函数和常量:
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+;等效于complex(float('nan'), 0.0)
) -
cmath.nanj
(Python 3.6+;等效于complex(0.0, float('nan'))
) -
cmath.inf
(Python 3.6+;等同于complex(float('inf'), 0.0)
) -
cmath.infj
(Python 3.6+;等同于complex(0.0, float('inf'))
)
cmath.isnan(...)
cmath.isinf(...)
cmath.isfinite(...)
(Python 3.2+)cmath.nan
(Python 3.6+; equivalent tocomplex(float('nan'), 0.0)
)cmath.nanj
(Python 3.6+; equivalent tocomplex(0.0, float('nan'))
)cmath.inf
(Python 3.6+; equivalent tocomplex(float('inf'), 0.0)
)cmath.infj
(Python 3.6+; equivalent tocomplex(0.0, float('inf'))
)
这篇关于在没有numpy的python中分配变量NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!