JS: fix double compareTo behaviour for NaN and +-0 (KT-22723)

This commit is contained in:
Anton Bannykh
2017-12-27 21:18:05 +03:00
parent 12c01ef80a
commit ed80252ba8
49 changed files with 260 additions and 307 deletions
+1 -1
View File
@@ -130,5 +130,5 @@ Kotlin.arrayDeepHashCode = function (arr) {
};
Kotlin.primitiveArraySort = function (array) {
array.sort(Kotlin.primitiveCompareTo)
array.sort(Kotlin.doubleCompareTo)
};
+4
View File
@@ -31,6 +31,10 @@ Kotlin.equals = function (obj1, obj2) {
return obj1.equals(obj2);
}
if (typeof obj1 === "number" && typeof obj2 === "number") {
return obj1 === obj2 && (obj1 !== 0 || 1 / obj1 === 1 / obj2)
}
return obj1 === obj2;
};
+20 -7
View File
@@ -16,14 +16,13 @@
Kotlin.compareTo = function (a, b) {
var typeA = typeof a;
var typeB = typeof a;
if (Kotlin.isChar(a) && typeB === "number") {
return Kotlin.primitiveCompareTo(a.charCodeAt(0), b);
if (typeA === "number") {
if (typeof b === "number") {
return Kotlin.doubleCompareTo(a, b);
}
return Kotlin.primitiveCompareTo(a, b);
}
if (typeA === "number" && Kotlin.isChar(b)) {
return Kotlin.primitiveCompareTo(a, b.charCodeAt(0));
}
if (typeA === "number" || typeA === "string" || typeA === "boolean") {
if (typeA === "string" || typeA === "boolean") {
return Kotlin.primitiveCompareTo(a, b);
}
return a.compareTo_11rb$(b);
@@ -33,6 +32,20 @@ Kotlin.primitiveCompareTo = function (a, b) {
return a < b ? -1 : a > b ? 1 : 0;
};
Kotlin.doubleCompareTo = function (a, b) {
if (a < b) return -1;
if (a > b) return 1;
if (a === b) {
if (a !== 0) return 0;
var ia = 1 / a;
return ia === 1 / b ? 0 : (ia < 0 ? -1 : 1);
}
return a !== a ? (b !== b ? 0 : 1) : -1
};
Kotlin.charInc = function (value) {
return Kotlin.toChar(value+1);
};