python的整数哈希

发布于 2025-02-09 04:36:04 字数 839 浏览 0 评论 0原文

我知道,一个不变的对象的哈希是该对象的整数表示,这是该对象在过程的寿命中是独一无二的。

整数对象的哈希与整数持有的值相同。例如,

>>> int(1000).__hash__()
1000

但是当整数成长足够大时,原则上的一定阈值似乎是在原则上突破的。它的价值似乎在一定的范围内。

>>> int(10000000000000000).__hash__()
10000000000000000
>>> int(100000000000000000).__hash__()
100000000000000000
>>> int(1000000000000000000).__hash__()
1000000000000000000
>>> int(10000000000000000000).__hash__()
776627963145224196

两个问题:

  1. 限制是什么?哈希桌子覆盖的整数空间是什么?
  2. 整数如何计算的哈希值超过上述限制?

系统信息:

Linux lap-0179 5.13.0-44-generic #49~20.04.1-Ubuntu SMP Wed May 18 18:44:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Python解释器:

Python 3.8.10 (default, Mar 15 2022, 12:22:08) 
[GCC 9.4.0] on linux

I understand that hash of an immutable object is an integer representation of that object which is unique within the process's lifetime.

Hash of an integer object is the same as the value held by the integer. For example,

>>> int(1000).__hash__()
1000

But when the integer grows big enough, above principle breaks after a certain threshold as it seems. Its value seems to be rolled with in some limit.

>>> int(10000000000000000).__hash__()
10000000000000000
>>> int(100000000000000000).__hash__()
100000000000000000
>>> int(1000000000000000000).__hash__()
1000000000000000000
>>> int(10000000000000000000).__hash__()
776627963145224196

Two questions:

  1. What is the limit? What is the integer-space that hash table covers?
  2. How is the hash value calculated for an integer exceeding the above limit?

System information:

Linux lap-0179 5.13.0-44-generic #49~20.04.1-Ubuntu SMP Wed May 18 18:44:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Python interpreter:

Python 3.8.10 (default, Mar 15 2022, 12:22:08) 
[GCC 9.4.0] on linux

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

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

发布评论

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

评论(1

因为看清所以看轻 2025-02-16 04:36:06

虽然这是机器和实现依赖的,但对于64位计算机上的Cpython,对于非负整数nhash()被计算为n%k带有k =(2 ** 61-1)= 2305843009213693951)因此,0 k-- 1 留下来。

这是从经验上证明的:

k = 2 ** 61 - 1
for i in range(k - 2, k + 2):
    print(i, hash(i), i % k)
# 2305843009213693949 2305843009213693949
# 2305843009213693950 2305843009213693950
# 2305843009213693951 0
# 2305843009213693952 1

有关完整的规则集,请参见文档

While this is machine and implementation dependent, for CPython on 64-bit machines, the hash() for a non-negative integer n is computed as n % k with k = (2 ** 61 - 1) (= 2305843009213693951) hence values between 0 and k - 1 are left as is.

This is empirically evidenced here:

k = 2 ** 61 - 1
for i in range(k - 2, k + 2):
    print(i, hash(i), i % k)
# 2305843009213693949 2305843009213693949
# 2305843009213693950 2305843009213693950
# 2305843009213693951 0
# 2305843009213693952 1

For the complete ruleset, see the documentation.

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