Fix floating point number hash code in ranges

Don't make 0.0 and -0.0 have the same hash code: they are different in Java (in
the sense that "Double.valueOf(0.0).equals(Double.valueOf(-0.0))" is false),
and they are considered different already in DoubleRange.equals, which calls
Double.compare, which differentiates them
This commit is contained in:
Alexander Udalov
2014-01-15 16:46:24 +04:00
parent d427fcc187
commit d66c5b2fbe
5 changed files with 11 additions and 17 deletions
@@ -45,9 +45,9 @@ fun areEqualNumbers(kind: ProgressionKind, v: String) = when (kind) {
fun hashLong(v: String) = "($v xor ($v ushr 32))"
fun floatToIntBits(v: String) = "(if ($v != 0.0f) java.lang.Float.floatToIntBits($v) else 0)"
fun floatToIntBits(v: String) = "java.lang.Float.floatToIntBits($v)"
fun doubleToLongBits(v: String) = "if ($v != 0.0) java.lang.Double.doubleToLongBits($v) else 0L"
fun doubleToLongBits(v: String) = "java.lang.Double.doubleToLongBits($v)"
val OUTPUT_DIR: File by Delegates.lazy {
@@ -48,9 +48,7 @@ class GenerateProgressions(val out: PrintWriter) {
BYTE, CHAR, SHORT -> " = 31 * (31 * start.toInt() + end) + increment"
INT -> " = 31 * (31 * start + end) + increment"
LONG -> " = (31 * (31 * ${hashLong("start")} + ${hashLong("end")}) + ${hashLong("increment")}).toInt()"
FLOAT -> " = (31 * (31 * ${floatToIntBits("start")} +\n" +
" ${floatToIntBits("end")}) + \n" +
" ${floatToIntBits("increment")}).toInt()"
FLOAT -> " = 31 * (31 * ${floatToIntBits("start")} + ${floatToIntBits("end")}) + ${floatToIntBits("increment")}"
DOUBLE -> ": Int {\n" +
" var temp = ${doubleToLongBits("start")}\n" +
" var result = ${hashLong("temp")}\n" +
@@ -48,8 +48,7 @@ class GenerateRanges(val out: PrintWriter) {
BYTE, CHAR, SHORT -> " = 31 * start.toInt() + end"
INT -> " = 31 * start + end"
LONG -> " = (31 * ${hashLong("start")} + ${hashLong("end")}).toInt()"
FLOAT -> " = 31 * ${floatToIntBits("start")} +\n" +
" ${floatToIntBits("end")}"
FLOAT -> " = 31 * ${floatToIntBits("start")} + ${floatToIntBits("end")}"
DOUBLE -> ": Int {\n" +
" var temp = ${doubleToLongBits("start")}\n" +
" val result = ${hashLong("temp")}\n" +