Support '.<digits>' floating literals in Double.parseDouble

See #327
This commit is contained in:
Alexey Andreev 2018-02-16 23:17:17 +03:00
parent 0485930c1f
commit 52a23fcadd
4 changed files with 57 additions and 46 deletions

View File

@ -83,27 +83,30 @@ public class TDouble extends TNumber implements TComparable<TDouble> {
++index;
}
char c = string.charAt(index);
if (c < '0' || c > '9') {
throw new TNumberFormatException();
}
long mantissa = 0;
int exp = 0;
while (string.charAt(index) == '0') {
if (++index == string.length()) {
return 0;
}
}
while (index < string.length()) {
c = string.charAt(index);
if (c != '.') {
if (c < '0' || c > '9') {
break;
throw new TNumberFormatException();
}
if (mantissa < 1E17) {
mantissa = mantissa * 10 + (c - '0');
} else {
++exp;
while (string.charAt(index) == '0') {
if (++index == string.length()) {
return 0;
}
}
while (index < string.length()) {
c = string.charAt(index);
if (c < '0' || c > '9') {
break;
}
if (mantissa < 1E17) {
mantissa = mantissa * 10 + (c - '0');
} else {
++exp;
}
++index;
}
++index;
}
if (index < string.length() && string.charAt(index) == '.') {
++index;

View File

@ -117,28 +117,33 @@ public class TFloat extends TNumber implements TComparable<TFloat> {
++index;
}
char c = string.charAt(index);
if (c < '0' || c > '9') {
throw new TNumberFormatException();
}
int mantissa = 0;
int exp = 0;
while (string.charAt(index) == '0') {
if (++index == string.length()) {
return 0;
}
}
while (index < string.length()) {
c = string.charAt(index);
if (c != '.') {
if (c < '0' || c > '9') {
break;
throw new TNumberFormatException();
}
if (mantissa < 1E8) {
mantissa = mantissa * 10 + (c - '0');
} else {
++exp;
while (string.charAt(index) == '0') {
if (++index == string.length()) {
return 0;
}
}
while (index < string.length()) {
c = string.charAt(index);
if (c < '0' || c > '9') {
break;
}
if (mantissa < 1E8) {
mantissa = mantissa * 10 + (c - '0');
} else {
++exp;
}
++index;
}
++index;
}
if (index < string.length() && string.charAt(index) == '.') {
++index;
boolean hasOneDigit = false;

View File

@ -34,6 +34,7 @@ public class DoubleTest {
assertEquals(23, Double.parseDouble("00230000e-4"), 1E-12);
assertEquals(23, Double.parseDouble("2300000000000000000000e-20"), 1E-12);
assertEquals(23, Double.parseDouble("2300000000000000000000e-20"), 1E-12);
assertEquals(0.1, Double.parseDouble(".1"), 0.001);
}
@Test

View File

@ -25,28 +25,30 @@ import org.teavm.junit.TeaVMTestRunner;
public class FloatTest {
@Test
public void parsed() {
assertEquals(23, Double.parseDouble("23"), 1E-12);
assertEquals(23, Double.parseDouble("23.0"), 1E-12);
assertEquals(23, Double.parseDouble("23E0"), 1E-12);
assertEquals(23, Double.parseDouble("2.30000E1"), 1E-12);
assertEquals(23, Double.parseDouble("0.23E2"), 1E-12);
assertEquals(23, Double.parseDouble("0.000023E6"), 1E-12);
assertEquals(23, Double.parseDouble("00230000e-4"), 1E-12);
assertEquals(23, Double.parseDouble("2300000000000000000000e-20"), 1E-12);
assertEquals(23, Double.parseDouble("2300000000000000000000e-20"), 1E-12);
assertEquals(23, Float.parseFloat("23"), 1E-12F);
assertEquals(23, Float.parseFloat("23.0"), 1E-12F);
assertEquals(23, Float.parseFloat("23E0"), 1E-12F);
assertEquals(23, Float.parseFloat("2.30000E1"), 1E-12F);
assertEquals(23, Float.parseFloat("0.23E2"), 1E-12F);
assertEquals(23, Float.parseFloat("0.000023E6"), 1E-12F);
assertEquals(23, Float.parseFloat("00230000e-4"), 1E-12F);
assertEquals(23, Float.parseFloat("2300000000000000000000e-20"), 1E-12F);
assertEquals(23, Float.parseFloat("2300000000000000000000e-20"), 1E-12F);
assertEquals(23, Float.parseFloat("2300000000000000000000e-20"), 1E-12F);
assertEquals(0.1F, Float.parseFloat("0.1"), 0.001F);
}
@Test
public void negativeParsed() {
assertEquals(-23, Double.parseDouble("-23"), 1E-12);
assertEquals(-23, Float.parseFloat("-23"), 1E-12F);
}
@Test
public void zeroParsed() {
assertEquals(0, Double.parseDouble("0.0"), 1E-12);
assertEquals(0, Double.parseDouble("23E-8000"), 1E-12);
assertEquals(0, Double.parseDouble("00000"), 1E-12);
assertEquals(0, Double.parseDouble("00000.0000"), 1E-12);
assertEquals(0, Float.parseFloat("0.0"), 1E-12F);
assertEquals(0, Float.parseFloat("23E-8000"), 1E-12F);
assertEquals(0, Float.parseFloat("00000"), 1E-12F);
assertEquals(0, Float.parseFloat("00000.0000"), 1E-12F);
}
@Test