Fix error in converting doubles to floats

This commit is contained in:
Alexey Andreev 2015-02-26 12:16:31 +04:00
parent bab69bac3d
commit 0b7db410d3
3 changed files with 43 additions and 8 deletions

View File

@ -249,7 +249,7 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
exp = 0; exp = 0;
float digit = 1; float digit = 1;
for (int i = powersOfTen.length - 1; i >= 0; --i) { 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]; digit *= powersOfTen[i];
exp |= bit; exp |= bit;
} }
@ -290,7 +290,6 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
exp = 0; exp = 0;
} }
} }
sz += digits;
// Extend buffer to store exponent // Extend buffer to store exponent
if (exp != 0) { if (exp != 0) {
@ -303,6 +302,11 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
} }
} }
if (exp != 0 && digits == intPart) {
digits++;
}
sz += digits;
// Print mantissa // Print mantissa
insertSpace(target, target + sz); insertSpace(target, target + sz);
if (negative) { if (negative) {
@ -399,7 +403,7 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
exp = 0; exp = 0;
double digit = 1; double digit = 1;
for (int i = doublePowersOfTen.length - 1; i >= 0; --i) { 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]; digit *= doublePowersOfTen[i];
exp |= bit; exp |= bit;
} }
@ -440,7 +444,6 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
exp = 0; exp = 0;
} }
} }
sz += digits;
// Extend buffer to store exponent // Extend buffer to store exponent
if (exp != 0) { if (exp != 0) {
@ -456,6 +459,11 @@ class TAbstractStringBuilder extends TObject implements TSerializable, TCharSequ
} }
} }
if (exp != 0 && digits == intPart) {
digits++;
}
sz += digits;
// Print mantissa // Print mantissa
insertSpace(target, target + sz); insertSpace(target, target + sz);
if (negative) { if (negative) {

View File

@ -444,10 +444,6 @@ public class TeaVM implements TeaVMHost, ServiceRepository {
} else { } else {
sourceWriter.append(asyncMethod ? naming.getFullNameForAsync(ref) : naming.getFullNameFor(ref)); sourceWriter.append(asyncMethod ? naming.getFullNameForAsync(ref) : naming.getFullNameFor(ref));
} }
if (wrapAsync) {
sourceWriter.append(")");
}
sourceWriter.append(";").newLine(); sourceWriter.append(";").newLine();
} }
for (Map.Entry<String, String> entry : exportedClasses.entrySet()) { for (Map.Entry<String, String> entry : exportedClasses.entrySet()) {

View File

@ -225,6 +225,37 @@ public class StringBuilderTest {
assertEquals("1.23456789E150", sb.toString()); 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 @Test
public void negativeDoubleAppended() { public void negativeDoubleAppended() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();