rtr*_*trt 5 python math numpy nan python-3.x
为什么math.nan不同于numpy.nan?
>>> import math
>>> import numpy as np
>>> math.nan is math.nan
True
>>> np.nan is np.nan
True
>>> np.nan is math.nan
False
Run Code Online (Sandbox Code Playgroud)
有两个不同的 NaN 有什么意义?
有两个不同的 NaN 有什么意义?
免责声明:我假设您想知道为什么存在math.nan和numpy.nan,如果这不成立,请完全忽略这个答案。
这是由于历史原因,数学文档math.nan告诉我们
3.5 版本中的新功能。
NumPy 比这更早,PyPI NumPy 发布历史显示版本 1.0 于2006 年 10 月发布,而根据PEP 478, Python 3.5.0 的第一个最终版本计划于2015 年 9 月发布,因此 NumPy 的创建者不可能使用math.nan. 这导致了一个问题,nan如果math它已经在numpy. 这可以简单地解释:math是内置模块,而numpy不是。
作为旁注,您也可以在 python 中NaN不使用任何s 来进行制作,只需执行.importfloat("NaN")