溢出错误:Python int 太大而无法转换为 C long

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

OverflowError: Python int too large to convert to C long

python

提问by Maxime Chéramy

I have this class:

我有这门课:

class MetricInt(int):
    """Int wrapper that adds only during the observation window."""
    def __new__(cls, _, initial):
        return int.__new__(cls, initial)

    def __init__(self, sim, initial):
        int.__init__(initial)
        self.sim = sim

    def __add__(self, val):
        if self.sim.in_observe_window():
            self = MetricInt(self.sim, super(MetricInt, self).__add__(int(val)))
        return self

Which basically overwrite the __add__method in order to only to the addition if self.sim.in_observe_window()returns True.

这基本上覆盖了该__add__方法,以便仅添加 ifself.sim.in_observe_window()返回True

However, if the initial value is too big, I have :

但是,如果初始值太大,我有:

OverflowError: Python int too large to convert to C long.

溢出错误:Python int 太大,无法转换为 C long。

What is the correct way to do what I'm trying to do and also handle big numbers?

做我想做的事情并处理大数字的正确方法是什么?

采纳答案by Eevee

Are you on Python 2.6? You could try subclassing longinstead.

您使用的是 Python 2.6 吗?你可以尝试子类化long

But in general I strongly suggest not subclassing Python built-in types; CPython reserves the right to skip calls to special methods on such types, and for example will not call __str__on a subclass of str. Your example here works, but you might be asking for bugs.

但总的来说,我强烈建议不要对 Python 内置类型进行子类化;CPython的保留跳到这种类型的特殊方法的调用正确的,例如不会调用__str__上的一个子类str。您的示例在这里有效,但您可能会要求提供错误。

Consider delegating instead, and delegating the operators you want. (You may also want __int__, of course.)

考虑委托,并委托您想要的操作员。(__int__当然,您可能还需要。)

回答by Andoni Diaz

I solved a similar problem casting it to int with int(bigNumber) but in think that is trivial in your case. You can try with the numpy:

我解决了一个类似的问题,用 int(bigNumber) 将它转换为 int,但我认为这在你的情况下是微不足道的。您可以尝试使用 numpy:

numpy.int32(Your big number)

And these that I found somewhere that now I can't remember:

我在某处找到的这些现在我不记得了:

def int_overflow(val):
  if not -sys.maxint-1 <= val <= sys.maxint:
    val = (val + (sys.maxint + 1)) % (2 * (sys.maxint + 1)) - sys.maxint - 1
  return val

Credits to the author.

感谢作者。

You can pass the overflowed value throught this function and get it normalized.

您可以通过此函数传递溢出的值并对其进行规范化。

Best regards

此致

回答by Maxime Chéramy

I like Eevee's answer about delegating instead. He has not provided any code so I'm doing it:

我喜欢 Eevee 关于委派的回答。他没有提供任何代码,所以我正在这样做:

class MetricInt(object):
    """Int wrapper that adds only during the observation window."""
    def __init__(self, sim, initial):
        self.sim = sim
        self.val = int(initial)

    def __add__(self, val):
        if self.sim.in_observe_window():
            self.val += int(val)
        return self

    def __int__(self):
        return self.val

    def __float__(self):
        return float(self.val)

This way, the problem is solved. When I decided to subclass the inttype, it was because I already had a few intvariables in my code and did not wanted to change my code too much. However, if I define __int__and __float__, I only need to add some casts to int. It's not that bad I guess if it avoids weird bugs.

这样,问题就解决了。当我决定对int类型进行子类化时,是因为int我的代码中已经有一些变量并且不想对我的代码进行太多更改。但是,如果我定义了__int__and __float__,我只需要向int. 如果它避免了奇怪的错误,我想这并没有那么糟糕。