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

hashes/hash casts 64-bit floats to 32-bit ints on 32-bit architecture #24769

Open
pwste-dawid-k opened this issue Mar 11, 2025 · 2 comments
Open

Comments

@pwste-dawid-k
Copy link

Nim Version

2.2.2

Description

The hash proc for floats casts float to int even if these types are not of the same size.

Current Output


Expected Output

Valid hash or error

Known Workarounds

Custom hash proc:

proc hash*(x: float): Hash {.inline.} =
  let y = x + 0.0
  when nimvm:
    # workaround a JS VM bug: bug #16547
    result = hashWangYi1(cast[int64](float64(y)))
  else:
    when not defined(js):
      when sizeof(system.int) == 4:
        result = hashWangYi1(cast[int64](float64(y)))
      else:
        result = hashWangYi1(cast[Hash](y))
    else:
      result = hashWangYiJS(toBits(y))

Additional Information

No response

@Araq
Copy link
Member

Araq commented Mar 11, 2025

So? What exactly is the problem with this?

@pwste-dawid-k
Copy link
Author

assert hash(0.0) != hash(1.0) fails because both floats seem to get truncated to 0 before before hashWangYi1 is called.

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