From daf5ada5c61bec0bfc368c70e43d85934bccf39a Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 4 Nov 2016 14:12:51 +0300 Subject: [PATCH] Fix converting JS floating-point numbers to long (fix #228). --- .../main/resources/org/teavm/backend/javascript/runtime.js | 2 +- tests/src/test/java/org/teavm/vm/LongTest.java | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/core/src/main/resources/org/teavm/backend/javascript/runtime.js b/core/src/main/resources/org/teavm/backend/javascript/runtime.js index 25067354c..4288b39eb 100644 --- a/core/src/main/resources/org/teavm/backend/javascript/runtime.js +++ b/core/src/main/resources/org/teavm/backend/javascript/runtime.js @@ -621,7 +621,7 @@ function Long_fromNumber(val) { if (val >= 0) { return new Long(val | 0, (val / 0x100000000) | 0); } else { - return new Long(val | 0, (-(Math.abs(val) / 0x100000000) - 1) | 0); + return Long_neg(new Long(-val | 0, (-val / 0x100000000) | 0)); } } function Long_toNumber(val) { diff --git a/tests/src/test/java/org/teavm/vm/LongTest.java b/tests/src/test/java/org/teavm/vm/LongTest.java index 397be4a8b..e3bb3bc5f 100644 --- a/tests/src/test/java/org/teavm/vm/LongTest.java +++ b/tests/src/test/java/org/teavm/vm/LongTest.java @@ -49,6 +49,13 @@ public class LongTest { assertEquals(2971215073L, a + b); } + @Test + public void smallLongDivision() { + long a = id(-1); + long b = 3; + assertEquals(0, a / b); + } + private static long id(long value) { return value; }