在没有numpy的情况下在python中分配变量NaN
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19374254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assigning a variable NaN in python without numpy
提问by zelinka
Most languages have a NaN constant you can use to assign a variable the value NaN. Can python do this without using numpy?
大多数语言都有一个 NaN 常量,您可以使用它为变量分配值 NaN。python可以在不使用numpy的情况下做到这一点吗?
采纳答案by Michael0x2a
Yes -- use math.nan
.
是的 - 使用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).
在 Python 3.5 之前,可以使用float("nan")
(不区分大小写)。
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.
请注意,检查 NaN 的两个事物是否彼此相等将始终返回 false。这部分是因为“不是数字”的两个东西不能(严格来说)说彼此相等——请参阅对于 IEEE754 NaN 值返回 false 的所有比较的基本原理是什么?了解更多详情和信息。
Instead, use math.isnan(...)
if you need to determine if a value is NaN or not.
相反,math.isnan(...)
如果您需要确定值是否为 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 containerfor more details.
此外,==
当尝试将 NaN 存储在容器类型(例如list
或)dict
(或使用自定义容器类型时)时,对 NaN 值的操作的确切语义可能会导致微妙的问题。有关更多详细信息,请参阅检查容器中是否存在 NaN。
You can also construct NaN numbers using Python's decimalmodule:
您还可以使用 Python 的小数模块构造 NaN 数:
>>> 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.
math.isnan(...)
也适用于 Decimal 对象。
However, you cannotconstruct NaN numbers in Python's fractionsmodule:
但是,您不能在 Python 的分数模块中构造 NaN 数字:
>>> 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(...)
)
顺便说一句,您还可以执行float('Inf')
、Decimal('Inf')
或math.inf
(3.5+) 来分配无限的数字。(另见math.isinf(...)
)
However doing Fraction('Inf')
or Fraction(float('inf'))
isn't permitted and will throw an exception, just like NaN.
但是,不允许Fraction('Inf')
或Fraction(float('inf'))
不允许,并且会抛出异常,就像 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+.
如果您想要一种快速简便的方法来检查数字是否既不是 NaN 也不是无穷大,您可以math.isfinite(...)
从 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
模块包含一组与该模块类似的函数和常量math
:
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'))
)
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'))
)
回答by orlp
Use float("nan")
:
使用float("nan")
:
>>> float("nan")
nan
回答by BrenBarn
You can do float('nan')
to get NaN.
你可以做得到float('nan')
NaN。
回答by abarnert
nan = float('nan')
And now you have the constant, nan
.
现在你有了常数,nan
。
You can similarly create NaN values for decimal.Decimal.:
您可以类似地为 decimal.Decimal. 创建 NaN 值:
dnan = Decimal('nan')
回答by Junior Polegato
You can get NaN from "inf - inf", and you can get "inf" from a number greater than 2e308, so, I generally used:
可以从“inf - inf”得到NaN,也可以从大于2e308的数得到“inf”,所以,我一般用:
>>> inf = 9e999
>>> inf
inf
>>> inf - inf
nan
回答by user3685621
A more consistent (and less opaque) way to generate inf and -inf is to again use float():
生成 inf 和 -inf 的一种更一致(也更不透明)的方法是再次使用 float():
>> positive_inf = float('inf')
>> positive_inf
inf
>> negative_inf = float('-inf')
>> negative_inf
-inf
Note that the size of a float varies depending on the architecture, so it probably best to avoid using magic numbers like 9e999, even if that is likely to work.
请注意,浮点数的大小因架构而异,因此最好避免使用像 9e999 这样的幻数,即使这可能有效。
import sys
sys.float_info
sys.float_info(max=1.7976931348623157e+308,
max_exp=1024, max_10_exp=308,
min=2.2250738585072014e-308, min_exp=-1021,
min_10_exp=-307, dig=15, mant_dig=53,
epsilon=2.220446049250313e-16, radix=2, rounds=1)