From 7277696870fee58594ee39982485b264e2c46d27 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 12 May 2015 19:27:11 +0300 Subject: [PATCH] Fix https://github.com/konsoletyper/teavm/issues/108 Fix incorrect rounding of negative numbers --- .../teavm/classlib/java/lang/TAbstractStringBuilder.java | 2 +- .../src/main/java/org/teavm/classlib/java/lang/TMath.java | 4 ++-- .../java/org/teavm/classlib/java/lang/IntegerTest.java | 5 +++++ .../test/java/org/teavm/classlib/java/lang/MathTest.java | 8 ++++++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java index fdd03076b..1d2f664ca 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TAbstractStringBuilder.java @@ -115,7 +115,7 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ } else { insertSpace(target, target + 1); } - buffer[target++] = (char)('0' + value); + buffer[target++] = TCharacter.forDigit(value, radix); } else { int pos = 1; int sz = 1; diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java index b64d4305b..dcbc6cefb 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java @@ -93,11 +93,11 @@ public final class TMath extends TObject { public static native double atan2(double y, double x); public static int round(float a) { - return (int)(a + 0.5f); + return (int)(a + signum(a) * 0.5f); } public static long round(double a) { - return (long)(a + 0.5); + return (long)(a + signum(a) * 0.5); } @GeneratedBy(MathNativeGenerator.class) diff --git a/teavm-tests/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java b/teavm-tests/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java index 485663cf2..d0f84f2ba 100644 --- a/teavm-tests/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java +++ b/teavm-tests/src/test/java/org/teavm/classlib/java/lang/IntegerTest.java @@ -58,6 +58,11 @@ public class IntegerTest { assertEquals("kona", Integer.toString(411787, 27)); } + @Test + public void writesSingleDigitInteger() { + assertEquals("a", Integer.toString(10, 16)); + } + @Test public void decodes() { assertEquals(Integer.valueOf(123), Integer.decode("123")); diff --git a/teavm-tests/src/test/java/org/teavm/classlib/java/lang/MathTest.java b/teavm-tests/src/test/java/org/teavm/classlib/java/lang/MathTest.java index 471523b8c..58249bcb9 100644 --- a/teavm-tests/src/test/java/org/teavm/classlib/java/lang/MathTest.java +++ b/teavm-tests/src/test/java/org/teavm/classlib/java/lang/MathTest.java @@ -54,4 +54,12 @@ public class MathTest { public void getExponentComputed() { assertEquals(6, Math.getExponent(123.456)); } + + @Test + public void roundWorks() { + assertEquals(1, Math.round(1.3)); + assertEquals(2, Math.round(1.8)); + assertEquals(-1, Math.round(-1.3)); + assertEquals(-2, Math.round(-1.8)); + } }