Fix error converting doubles to strings

This commit is contained in:
Alexey Andreev 2015-02-26 12:28:32 +04:00
parent 103fc0f9cc
commit 98210d2528
2 changed files with 28 additions and 6 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

@ -1,9 +1,24 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.impl.unicode; package org.teavm.classlib.impl.unicode;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import org.junit.Test;
public class UnicodeSupportTest { public class UnicodeSupportTest {
private static boolean pairsEqual(final int[] pairs, final int index1, final int index2) { private static boolean pairsEqual(final int[] pairs, final int index1, final int index2) {
return pairs[index1] == pairs[index2] && pairs[index1 + 1] == pairs[index2 + 1]; return pairs[index1] == pairs[index2] && pairs[index1 + 1] == pairs[index2 + 1];
} }
@ -16,5 +31,4 @@ public class UnicodeSupportTest {
assertFalse(pairsEqual(digitValues, digitValues.length - 4, digitValues.length - 2)); assertFalse(pairsEqual(digitValues, digitValues.length - 4, digitValues.length - 2));
} }
} }
} }