" OverflowError: Python int too large to convert to C long " on windows but not mac。

时间:2022-06-24 18:20:12

I am running the exact same code on both windows and mac, with python 3.5 64 bit.

我正在windows和mac上运行完全相同的代码,使用python 3.5 64位。

On windows, it looks like this:

在windows上,它是这样的:

>>> import numpy as np
>>> preds = np.zeros((1, 3), dtype=int)
>>> p = [6802256107, 5017549029, 3745804973]
>>> preds[0] = p
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    preds[0] = p
OverflowError: Python int too large to convert to C long

However, this code works fine on my mac. Could anyone help explain why or give a solution for the code on windows? Thanks so much!

但是,这段代码在我的mac上运行得很好。谁能解释一下为什么或者给windows上的代码一个解决方案吗?非常感谢!

1 个解决方案

#1


11  

You'll get that error once your numbers are greater than sys.maxsize:

当您的数字大于sys.m时,您将会得到这个错误。

>>> p = [sys.maxsize]
>>> preds[0] = p
>>> p = [sys.maxsize+1]
>>> preds[0] = p
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long

You can confirm this by checking:

您可以通过以下方式确认:

>>> import sys
>>> sys.maxsize
2147483647

To take numbers with larger precision, don't pass an int type which uses a bounded C integer behind the scenes. Use the default float:

要获得更精确的数字,请不要在后台传递使用有界C整数的int类型。使用默认的浮动:

>>> preds = np.zeros((1, 3))

#1


11  

You'll get that error once your numbers are greater than sys.maxsize:

当您的数字大于sys.m时,您将会得到这个错误。

>>> p = [sys.maxsize]
>>> preds[0] = p
>>> p = [sys.maxsize+1]
>>> preds[0] = p
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long

You can confirm this by checking:

您可以通过以下方式确认:

>>> import sys
>>> sys.maxsize
2147483647

To take numbers with larger precision, don't pass an int type which uses a bounded C integer behind the scenes. Use the default float:

要获得更精确的数字,请不要在后台传递使用有界C整数的int类型。使用默认的浮动:

>>> preds = np.zeros((1, 3))