熊猫指数功率没有按应有的方式计算

Pandas exponential power not computing as it should

提问人:Paris Spinelli 提问时间:4/21/2022 更新时间:4/21/2022 访问量:144

问:

我想我在 PANDA 中使用指数时发现了浮点精度的问题。我有一个数字列表,我正在通过 4 阶多项式运行,当我得到大于 2 的阶时,我注意到 Pandas 中预测的值不正确。代码如下:

import numpy as np
import pandas as pd

x = [-69500]
x_k = pd.DataFrame([i*1000 for i in x], columns=['X'])

x_k3 = x_k**3
x_k3_np = np.power(x_k['X'], 3)


x_k3_real = [pow(i*1000, 3) for i in x]



print(x_k3.iloc[0])
print(x_k3_np.iloc[0])
print(x_k3_real[0])

生产:

X   -8526346633579692032
Name: 0, dtype: int64
-8526346633579692032
-335702375000000000000000

如前所述,列表推导方法产生了我认为正确的答案 - 但不确定为什么 Pandas 不会。有没有更好的方法可以在数据帧中执行此操作?

Python Pandas DataFrame 浮点 精度

评论

0赞 Kelly Bundy 4/21/2022
你在哪里看到浮点?看起来也不像是精度问题,而像溢出。这难道不表明问题吗?int64
0赞 Paris Spinelli 4/21/2022
我在任何地方都看不到浮点 - 但我想知道当变量存储在内存中时,如果操作对于大数字不可靠?您能详细解释一下'''int64''如何表示问题吗?为什么这很能说明问题?**
0赞 Kelly Bundy 4/21/2022
因为您的数字明显超过 64 位
0赞 aka.nice 4/21/2022
如果你取 -335702375000000000000000 模 2^64,你会得到 -8526346633579692032

答: 暂无答案