Python 类型 long 与 C“long long”类型

发布于 2024-08-12 07:52:41 字数 138 浏览 1 评论 0原文

我想将一个值表示为 64 位有符号 long,这样大于 (2**63)-1 的值表示为负数,但是 Python long 具有无限精度。有没有一种“快速”的方法可以让我实现这一目标?

I would like to represent a value as a 64bit signed long, such that values larger than (2**63)-1 are represented as negative, however Python long has infinite precision. Is there a 'quick' way for me to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无法言说的痛 2024-08-19 07:52:41

您可以使用 ctypes.c_longlong

>>> from ctypes import c_longlong as ll
>>> ll(2 ** 63 - 1)
c_longlong(9223372036854775807L)
>>> ll(2 ** 63)
c_longlong(-9223372036854775808L)
>>> ll(2 ** 63).value
-9223372036854775808L

如果您确定 signed long long 在目标机器上的宽度为 64 位,那么这实际上是一个选项。

编辑:jorendorff的想法 为 64 位数字定义一个类很有吸引力。理想情况下,您希望最大程度地减少显式类创建的数量。

使用c_longlong,你可以做这样的事情(注意:仅限Python 3.x!):

from ctypes import c_longlong

class ll(int):
    def __new__(cls, n):
        return int.__new__(cls, c_longlong(n).value)

    def __add__(self, other):
        return ll(super().__add__(other))

    def __radd__(self, other):
        return ll(other.__add__(self))

    def __sub__(self, other):
        return ll(super().__sub__(other))

    def __rsub__(self, other):
        return ll(other.__sub__(self))

    ...

这样的结果是ll(2 ** 63) - 1 确实是 9223372036854775807。不过,这种构造可能会导致性能下降,因此根据您想要做什么,定义一个像上面这样的类可能不值得。如有疑问,请使用 timeit

You could use ctypes.c_longlong:

>>> from ctypes import c_longlong as ll
>>> ll(2 ** 63 - 1)
c_longlong(9223372036854775807L)
>>> ll(2 ** 63)
c_longlong(-9223372036854775808L)
>>> ll(2 ** 63).value
-9223372036854775808L

This is really only an option if you know for sure that a signed long long will be 64 bits wide on the target machine(s).

Edit: jorendorff's idea of defining a class for 64 bit numbers is appealing. Ideally you want to minimize the number of explicit class creations.

Using c_longlong, you could do something like this (note: Python 3.x only!):

from ctypes import c_longlong

class ll(int):
    def __new__(cls, n):
        return int.__new__(cls, c_longlong(n).value)

    def __add__(self, other):
        return ll(super().__add__(other))

    def __radd__(self, other):
        return ll(other.__add__(self))

    def __sub__(self, other):
        return ll(super().__sub__(other))

    def __rsub__(self, other):
        return ll(other.__sub__(self))

    ...

In this way the result of ll(2 ** 63) - 1 will indeed be 9223372036854775807. This construction may result in a performance penalty though, so depending on what you want to do exactly, defining a class such as the above may not be worth it. When in doubt, use timeit.

茶花眉 2024-08-19 07:52:41

你可以使用 numpy 吗?它有一个 int64 类型,可以完全满足您的需求。

In [1]: import numpy

In [2]: numpy.int64(2**63-1)
Out[2]: 9223372036854775807

In [3]: numpy.int64(2**63-1)+1
Out[3]: -9223372036854775808

与 ctypes 示例不同,它对用户是透明的,并且它是用 C 编写的,因此比在 Python 中滚动您自己的类更快。 Numpy 可能比其他解决方案更大,但如果您正在进行数值分析,您会很高兴拥有它。

Can you use numpy? It has an int64 type that does exactly what you want.

In [1]: import numpy

In [2]: numpy.int64(2**63-1)
Out[2]: 9223372036854775807

In [3]: numpy.int64(2**63-1)+1
Out[3]: -9223372036854775808

It's transparent to users, unlike the ctypes example, and it's coded in C so it'll be faster than rolling your own class in Python. Numpy may be bigger than the other solutions, but if you're doing numerical analysis, you will appreciate having it.

巴黎夜雨 2024-08-19 07:52:41

最快的事情可能是自己将结果截断为 64 位:

def to_int64(n):
    n = n & ((1 << 64) - 1)
    if n > (1 << 63) - 1:
        n -= 1 << 64
    return n

您当然可以定义自己的数字类型,每次执行任何类型的算术运算时都会自动执行此操作:

class Int64:
    def __init__(self, n):
        if isinstance(n, Int64):
            n = n.val
        self.val = to_int64(n)

    def __add__(self, other):
        return Int64(self.val + other)

    def __radd__(self, other):
        return Int64(other + self.val)

    def __sub__(self, other):
        return Int64(self.val - other)

    ...

但这并不是特别“快”实现。

The quickest thing is probably to truncate the result to 64 bits yourself:

def to_int64(n):
    n = n & ((1 << 64) - 1)
    if n > (1 << 63) - 1:
        n -= 1 << 64
    return n

You can of course define your own numeric type that automatically does this every time you do any sort of arithmetic operation:

class Int64:
    def __init__(self, n):
        if isinstance(n, Int64):
            n = n.val
        self.val = to_int64(n)

    def __add__(self, other):
        return Int64(self.val + other)

    def __radd__(self, other):
        return Int64(other + self.val)

    def __sub__(self, other):
        return Int64(self.val - other)

    ...

but that is not particularly "quick" to implement.

贱人配狗天长地久 2024-08-19 07:52:41

看一下 ctypes 模块,它用于从 python 调用外部 DLL/库。
有一些数据类型对应于C类型,例如

c_longlong 类

Have a look at the ctypes module, it is used to call foreign DLLs/libraries from python.
There a some data types that correspond to C types, for example

class c_longlong

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文