## [Jython-dev] float___hash__()

 [Jython-dev] float___hash__() From: David Morley - 2008-11-15 06:18:28 ```If you have a float that equals an integer, but cannot be represented in 32 bits, then float___hash__() converts this number, intPart, to a PyLong/java.math.BigInteger and uses java.math.BigInteger.intValue() to extract the last 32 bits. Even though floats are rarely hashed, this seems like a lot of effort. What about something simpler, like using the modulus operator to reduce the intPart to a smaller number that has the same last 32 bits, coercing that to a long, and then coercing that to an int. Coercing to an int directly doesn't work, as coercion of large magnitude floats to ints leads to MAX_VALUE or MIN_VALUE. This seems to give the same results for quite a few boundary cases: >>> from java.lang import Float, Integer, Long >>> def check(x): print hash(float(x)), Long.intValue(Float.longValue(float(x)%(2.0**32))) ... >>> check(Integer.MIN_VALUE - 1) 2147483647 2147483647 >>> check(Integer.MIN_VALUE) -2147483648 -2147483648 >>> check(Integer.MAX_VALUE) 2147483647 2147483647 >>> check(Integer.MAX_VALUE + 1) -2147483648 -2147483648 >>> check(Integer.MAX_VALUE + 2**52) 2147483647 2147483647 >>> check(Integer.MAX_VALUE + 2**53) -2147483648 -2147483648 >>> check(Integer.MAX_VALUE + 2**83) check(Integer.MAX_VALUE + 2**83) -2147483648 -2147483648 >>> check(Integer.MAX_VALUE + 2**84) 0 0 >>> - David ```

 [Jython-dev] float___hash__() From: David Morley - 2008-11-15 06:18:28 ```If you have a float that equals an integer, but cannot be represented in 32 bits, then float___hash__() converts this number, intPart, to a PyLong/java.math.BigInteger and uses java.math.BigInteger.intValue() to extract the last 32 bits. Even though floats are rarely hashed, this seems like a lot of effort. What about something simpler, like using the modulus operator to reduce the intPart to a smaller number that has the same last 32 bits, coercing that to a long, and then coercing that to an int. Coercing to an int directly doesn't work, as coercion of large magnitude floats to ints leads to MAX_VALUE or MIN_VALUE. This seems to give the same results for quite a few boundary cases: >>> from java.lang import Float, Integer, Long >>> def check(x): print hash(float(x)), Long.intValue(Float.longValue(float(x)%(2.0**32))) ... >>> check(Integer.MIN_VALUE - 1) 2147483647 2147483647 >>> check(Integer.MIN_VALUE) -2147483648 -2147483648 >>> check(Integer.MAX_VALUE) 2147483647 2147483647 >>> check(Integer.MAX_VALUE + 1) -2147483648 -2147483648 >>> check(Integer.MAX_VALUE + 2**52) 2147483647 2147483647 >>> check(Integer.MAX_VALUE + 2**53) -2147483648 -2147483648 >>> check(Integer.MAX_VALUE + 2**83) check(Integer.MAX_VALUE + 2**83) -2147483648 -2147483648 >>> check(Integer.MAX_VALUE + 2**84) 0 0 >>> - David ```