From 0b7db410d3d65232db0ba9dc2055059f65c4f8a0 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 26 Feb 2015 12:16:31 +0400 Subject: [PATCH] Fix error in converting doubles to floats --- .../java/lang/TAbstractStringBuilder.java | 16 +++++++--- .../src/main/java/org/teavm/vm/TeaVM.java | 4 --- .../classlib/java/lang/StringBuilderTest.java | 31 +++++++++++++++++++ 3 files changed, 43 insertions(+), 8 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 498b5a17b..ee3d6e04c 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 @@ -249,7 +249,7 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ exp = 0; float digit = 1; for (int i = powersOfTen.length - 1; i >= 0; --i) { - if ((exp | bit) <= FLOAT_MAX_EXPONENT && powersOfTen[i] * digit < value) { + if ((exp | bit) <= FLOAT_MAX_EXPONENT && powersOfTen[i] * digit <= value) { digit *= powersOfTen[i]; exp |= bit; } @@ -290,7 +290,6 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ exp = 0; } } - sz += digits; // Extend buffer to store exponent if (exp != 0) { @@ -303,6 +302,11 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ } } + if (exp != 0 && digits == intPart) { + digits++; + } + sz += digits; + // Print mantissa insertSpace(target, target + sz); if (negative) { @@ -399,7 +403,7 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ exp = 0; double digit = 1; for (int i = doublePowersOfTen.length - 1; i >= 0; --i) { - if ((exp | bit) <= DOUBLE_MAX_EXPONENT && doublePowersOfTen[i] * digit < value) { + if ((exp | bit) <= DOUBLE_MAX_EXPONENT && doublePowersOfTen[i] * digit <= value) { digit *= doublePowersOfTen[i]; exp |= bit; } @@ -440,7 +444,6 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ exp = 0; } } - sz += digits; // Extend buffer to store exponent if (exp != 0) { @@ -456,6 +459,11 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ } } + if (exp != 0 && digits == intPart) { + digits++; + } + sz += digits; + // Print mantissa insertSpace(target, target + sz); if (negative) { diff --git a/teavm-core/src/main/java/org/teavm/vm/TeaVM.java b/teavm-core/src/main/java/org/teavm/vm/TeaVM.java index 409841019..53d5efb6f 100644 --- a/teavm-core/src/main/java/org/teavm/vm/TeaVM.java +++ b/teavm-core/src/main/java/org/teavm/vm/TeaVM.java @@ -444,10 +444,6 @@ public class TeaVM implements TeaVMHost, ServiceRepository { } else { sourceWriter.append(asyncMethod ? naming.getFullNameForAsync(ref) : naming.getFullNameFor(ref)); } - - if (wrapAsync) { - sourceWriter.append(")"); - } sourceWriter.append(";").newLine(); } for (Map.Entry entry : exportedClasses.entrySet()) { diff --git a/teavm-tests/src/test/java/org/teavm/classlib/java/lang/StringBuilderTest.java b/teavm-tests/src/test/java/org/teavm/classlib/java/lang/StringBuilderTest.java index df50934c8..3f88af2aa 100644 --- a/teavm-tests/src/test/java/org/teavm/classlib/java/lang/StringBuilderTest.java +++ b/teavm-tests/src/test/java/org/teavm/classlib/java/lang/StringBuilderTest.java @@ -225,6 +225,37 @@ public class StringBuilderTest { assertEquals("1.23456789E150", sb.toString()); } + @Test + public void powTenDoubleAppended() { + StringBuilder sb = new StringBuilder(); + sb.append(10.0); + assertEquals("10.0", sb.toString()); + sb.setLength(0); + sb.append(20.0); + assertEquals("20.0", sb.toString()); + sb.setLength(0); + sb.append(100.0); + assertEquals("100.0", sb.toString()); + sb.setLength(0); + sb.append(1000.0); + assertEquals("1000.0", sb.toString()); + sb.setLength(0); + sb.append(0.1); + assertEquals("0.1", sb.toString()); + sb.setLength(0); + sb.append(0.01); + assertEquals("0.01", sb.toString()); + sb.setLength(0); + sb.append(1e20); + assertEquals("1.0E20", sb.toString()); + sb.setLength(0); + sb.append(2e20); + assertEquals("2.0E20", sb.toString()); + sb.setLength(0); + sb.append(1e-12); + assertEquals("1.0E-12", sb.toString()); + } + @Test public void negativeDoubleAppended() { StringBuilder sb = new StringBuilder();