在没有numpy的python中分配一个变量NaN

2022-01-23 00:00:00 python nan constants

问题描述

大多数语言都有一个 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?


解决方案

是的 -- 使用 math.nan.

Yes -- use math.nan.

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

在 Python 3.5 之前,可以使用 float("nan")(不区分大小写).

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

请注意,检查是否为 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 存储在 listdict 等容器类型中时,对 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 的 decimal 模块构造 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:Python35libfractions.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:Python35libfractions.py", line 130, in __new__
    value = Fraction.from_float(numerator)
  File "C:Python35libfractions.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(...))

但是,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(...) 自 Python 3.2+ 起.

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')))

相关文章