diff --git a/classlib/src/main/java/org/teavm/classlib/impl/NumericClassTransformer.java b/classlib/src/main/java/org/teavm/classlib/impl/NumericClassTransformer.java index a83e06212..5dee22dc5 100644 --- a/classlib/src/main/java/org/teavm/classlib/impl/NumericClassTransformer.java +++ b/classlib/src/main/java/org/teavm/classlib/impl/NumericClassTransformer.java @@ -40,12 +40,6 @@ public class NumericClassTransformer implements ClassHolderTransformer { case "java.lang.Long": transformLong(cls); break; - case "java.lang.Float": - transformFloat(cls); - break; - case "java.lang.Double": - transformDouble(cls); - break; } } @@ -57,14 +51,6 @@ public class NumericClassTransformer implements ClassHolderTransformer { transformCompareMethod(cls, ValueType.LONG, NumericOperandType.LONG); } - private void transformFloat(ClassHolder cls) { - transformCompareMethod(cls, ValueType.FLOAT, NumericOperandType.FLOAT); - } - - private void transformDouble(ClassHolder cls) { - transformCompareMethod(cls, ValueType.DOUBLE, NumericOperandType.DOUBLE); - } - private void transformCompareMethod(ClassHolder cls, ValueType type, NumericOperandType insnType) { MethodHolder method = cls.getMethod(new MethodDescriptor("compare", type, type, ValueType.INTEGER)); Program program = new Program(); diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java index 6df111e47..f66fbcf6b 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TDouble.java @@ -215,7 +215,11 @@ public class TDouble extends TNumber implements TComparable { } @NoSideEffects - public static native int compare(double a, double b); + public static int compare(double a, double b) { + var diff = (a > b ? 1 : 0) - (b > a ? 1 : 0); + return diff != 0 ? diff : (1 / a > 1 / b ? 1 : 0) - (1 / b > 1 / a ? 1 : 0) + + (b == b ? 1 : 0) - (a == a ? 1 : 0); + } @Override public int compareTo(TDouble other) { diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java index 5a8129474..fda618bf8 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TFloat.java @@ -245,7 +245,11 @@ public class TFloat extends TNumber implements TComparable { } @NoSideEffects - public static native int compare(float f1, float f2); + public static int compare(float a, float b) { + var diff = (a > b ? 1 : 0) - (b > a ? 1 : 0); + return diff != 0 ? diff : (1 / a > 1 / b ? 1 : 0) - (1 / b > 1 / a ? 1 : 0) + + (b == b ? 1 : 0) - (a == a ? 1 : 0); + } @Override public int compareTo(TFloat other) { diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/DoubleTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/DoubleTest.java index cb6bb4f05..b7cf451a8 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/DoubleTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/DoubleTest.java @@ -17,7 +17,6 @@ package org.teavm.classlib.java.lang; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.Random; import org.junit.Test; @@ -134,8 +133,15 @@ public class DoubleTest { @Test public void compares() { - assertTrue(Double.compare(10, 5) > 0); - assertTrue(Double.compare(5, 10) < 0); - assertTrue(Double.compare(5, 5) == 0); + assertEquals(1, Double.compare(10, 5)); + assertEquals(-1, Double.compare(5, 10)); + assertEquals(0, Double.compare(5, 5)); + assertEquals(0, Double.compare(0.0f, 0.0f)); + assertEquals(1, Double.compare(Double.NaN, Double.POSITIVE_INFINITY)); + assertEquals(-1, Double.compare(Double.POSITIVE_INFINITY, Double.NaN)); + assertEquals(1, Double.compare(Double.NaN, 0.0)); + assertEquals(-1, Double.compare(-0.0, Double.NaN)); + assertEquals(1, Double.compare(0.0, -0.0)); + assertEquals(-1, Double.compare(-0.0, 0.0)); } } diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/FloatTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/FloatTest.java index b2957d962..4a8498d31 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/FloatTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/FloatTest.java @@ -17,7 +17,6 @@ package org.teavm.classlib.java.lang; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import org.junit.Test; import org.junit.runner.RunWith; @@ -115,8 +114,16 @@ public class FloatTest { @Test public void compares() { - assertTrue(Float.compare(10, 5) > 0); - assertTrue(Float.compare(5, 10) < 0); - assertTrue(Float.compare(5, 5) == 0); + assertEquals(1, Float.compare(10, 5)); + assertEquals(-1, Float.compare(5, 10)); + assertEquals(0, Float.compare(5, 5)); + assertEquals(0, Float.compare(0.0f, 0.0f)); + assertEquals(0, Float.compare(-0.0f, -0.0f)); + assertEquals(1, Float.compare(Float.NaN, Float.POSITIVE_INFINITY)); + assertEquals(-1, Float.compare(Float.POSITIVE_INFINITY, Float.NaN)); + assertEquals(1, Float.compare(Float.NaN, 0.0f)); + assertEquals(-1, Float.compare(-0.0f, Float.NaN)); + assertEquals(1, Float.compare(0.0f, -0.0f)); + assertEquals(-1, Float.compare(-0.0f, 0.0f)); } }