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; ++index;
} }
char c = string.charAt(index); char c = string.charAt(index);
if (c < '0' || c > '9') {
throw new TNumberFormatException();
}
long mantissa = 0; long mantissa = 0;
int exp = 0; int exp = 0;
while (string.charAt(index) == '0') { if (c != '.') {
if (++index == string.length()) {
return 0;
}
}
while (index < string.length()) {
c = string.charAt(index);
if (c < '0' || c > '9') { if (c < '0' || c > '9') {
break; throw new TNumberFormatException();
} }
if (mantissa < 1E17) { while (string.charAt(index) == '0') {
mantissa = mantissa * 10 + (c - '0'); if (++index == string.length()) {
} else { return 0;
++exp; }
}
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) == '.') { if (index < string.length() && string.charAt(index) == '.') {
++index; ++index;

View File

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

View File

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

View File

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