Numpy float64 与 Python 浮点数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27098529/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:26:10  来源:igfitidea点击:

Numpy float64 vs Python float

pythonnumpyfloating-point

提问by mchangun

I'm battling some floating point problems in Pandas read_csv function. In my investigation, I found this:

我正在与 Pandas read_csv 函数中的一些浮点问题作斗争。在我的调查中,我发现了这一点:

In [15]: a = 5.9975

In [16]: a
Out[16]: 5.9975

In [17]: np.float64(a)
Out[17]: 5.9974999999999996

Why is builtin floatof Python and the np.float64type from Python giving different results? I thought they were both C++ doubles?

为什么内置float的 Python 和np.float64来自 Python的类型给出不同的结果?我以为他们都是C++双打?

采纳答案by Ignacio Vazquez-Abrams

>>> numpy.float64(5.9975).hex()
'0x1.7fd70a3d70a3dp+2'
>>> (5.9975).hex()
'0x1.7fd70a3d70a3dp+2'

They are the same number. What differs is their representation; the Python native type uses a "sane" representation, and the NumPy type uses an accurate representation.

他们是同一个数字。不同的是他们的代表;Python 本机类型使用“理智”表示,而 NumPy 类型使用准确表示。