From 154bf7abd918177613c31b9db395e4f6ea1f28d1 Mon Sep 17 00:00:00 2001 From: Ivan Hetman Date: Tue, 19 Sep 2023 14:21:32 +0300 Subject: [PATCH] classlib: more fixes to Math.nextUp/nextDown (#746) --- .../main/java/org/teavm/classlib/java/lang/TMath.java | 10 ++++++++-- .../java/org/teavm/classlib/java/lang/MathTest.java | 4 ++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java index 09111543f..95c7b5977 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java @@ -387,6 +387,9 @@ public final class TMath extends TObject { if (TDouble.isNaN(d) || d == TDouble.POSITIVE_INFINITY) { return d; } + if (d == 0.0d) { + return Double.MIN_VALUE; + } long bits = TDouble.doubleToLongBits(d); if (d < 0) { bits--; @@ -400,6 +403,9 @@ public final class TMath extends TObject { if (TFloat.isNaN(d) || d == TFloat.POSITIVE_INFINITY) { return d; } + if (d == 0) { + return Float.MIN_VALUE; + } int bits = TFloat.floatToIntBits(d); if (d < 0) { bits--; @@ -417,7 +423,7 @@ public final class TMath extends TObject { return -Double.MIN_VALUE; } long bits = TDouble.doubleToLongBits(d); - if (d <= 0) { + if (d < 0) { bits++; } else { bits--; @@ -433,7 +439,7 @@ public final class TMath extends TObject { return -Float.MIN_VALUE; } int bits = TFloat.floatToIntBits(d); - if (d <= 0) { + if (d < 0) { bits++; } else { bits--; diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/MathTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/MathTest.java index e2dcebdf4..0c15c914f 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/MathTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/MathTest.java @@ -106,8 +106,12 @@ public class MathTest { public void nextWorks() { assertEquals(Double.valueOf(-Double.MIN_VALUE), Double.valueOf(Math.nextDown(0.0))); assertEquals(Double.valueOf(Double.MIN_VALUE), Double.valueOf(Math.nextUp(0.0))); + assertEquals(Double.valueOf(-Double.MIN_VALUE), Double.valueOf(Math.nextDown(-0.0))); + assertEquals(Double.valueOf(Double.MIN_VALUE), Double.valueOf(Math.nextUp(-0.0))); assertEquals(Float.valueOf(-Float.MIN_VALUE), Float.valueOf(Math.nextDown(0.0f))); assertEquals(Float.valueOf(Float.MIN_VALUE), Float.valueOf(Math.nextUp(0.0f))); + assertEquals(Float.valueOf(-Float.MIN_VALUE), Float.valueOf(Math.nextDown(-0.0f))); + assertEquals(Float.valueOf(Float.MIN_VALUE), Float.valueOf(Math.nextUp(-0.0f))); assertEquals(Double.valueOf(0.10000000000000002), Double.valueOf(Math.nextUp(0.1))); assertEquals(Double.valueOf(0.9999999999999999), Double.valueOf(Math.nextDown(1.0))); assertEquals(Double.valueOf(-0.09999999999999999), Double.valueOf(Math.nextUp(-0.1)));