Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

required implementation for long_hash #12

Open
kai-nashi opened this issue Jan 15, 2024 · 2 comments
Open

required implementation for long_hash #12

kai-nashi opened this issue Jan 15, 2024 · 2 comments

Comments

@kai-nashi
Copy link

kai-nashi commented Jan 15, 2024

Hash of integer long in python2 and python3 has different result. Must implement hash of long

Example:
Python2

hash(10000000000)  #  10000000000
hash(-10000000000)  #  -1410065410
hash(20000000000)  # 1474836476
hash(-20000000000)  # 1474836476
hash(10 ** 20)  # -824746446

Python3

hash(10000000000)  #  10000000000
hash(-10000000000)  #  -10000000000
hash(20000000000)  # 20000000000
hash(-20000000000)  # -20000000000
hash(10 ** 20)  # 848750603811160107
@kai-nashi
Copy link
Author

kai-nashi commented Jan 15, 2024

I have wrote implementation of long hash. It work for 32bit, don't test on 64bit.

INT_MAX_VALUE = 2 ** 32

def hash_long(value: int) -> int:
    sign = -1 if value < 0 else 1
    value = abs(value)

    if value >= INT_MAX_VALUE:
        value = value % (INT_MAX_VALUE - 1)

    if value > (INT_MAX_VALUE / 2 - 1):
        value -= INT_MAX_VALUE

    value = sign * value
    if value == -1:
        value = -2

    return value

@davidmezzetti
Copy link
Member

Hello. It's been a while since I've changed py27hash. It was primarily designed to help with converting Python 2 programs to Python 3. Given that Python 2.7 has now been EOL for 4 years, not sure how much need there is for this library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants