Before looking at the hashing logic, lets see few tidbits.
Hash value of the python integers will be the numbers themselves.
Only exception to point 1 is, -1
. Because -1
is internally used to indicate error cases. So, hash value of -1
will be -2
. (For -2 also, it will be -2 only.)
With this understanding, we can answer your examples 2, 3 and 5.
Example 2: Hash value of -1 == Hash value of -2. So, it is true.
Example 3: Same as the reason for Example 2
Example 5: Hash value of -1 and 1 are different (-2 and 1 respectively). That's why the result is False
.
The case, Example 4, hash value of floating point numbers are not the same as the numbers themselves, because hash values should be integers. And the numbers are not represented as they are in the memory. (2.01 is NOT stored as 2.01 itself in the memory). So, hash values of them being different is acceptable.
To answer the Example 1, lets see some code. As per the tuple hashing implementation, lets calculate the hash value using a Python program
x, hash_mult = int("0x345678", 16), 1000003
x, hash_mult = (x ^ 2) * hash_mult, hash_mult + 82522
x = (x ^ -2) * hash_mult
print(x + 97531) # -3713082714466793269
x, hash_mult = int("0x345678", 16), 1000003
x, hash_mult = (x ^ -2) * hash_mult, hash_mult + 82522
x = (x ^ 2) * hash_mult
print(x + 97531) # -3713082714466793269
print(hash((-2, 2))) # -3713082714466793269
Note: This is not only true for -2, 2
, it is true for all integers, except the above seen case and all the multiples of 8.