Fix float_endf for decimal-less numbers. Also secretly support d/D

This commit is contained in:
Paul Romano 2019-04-04 16:40:28 -05:00
parent ae4b0106dc
commit ef0d216f66
2 changed files with 18 additions and 5 deletions

View file

@ -4,14 +4,14 @@ double cfloat_endf(const char* buffer, int n)
{
char arr[12]; // 11 characters plus a null terminator
int j = 0; // current position in arr
int found_decimal = 0;
int found_significand = 0;
int found_exponent = 0;
for (int i = 0; i < n; ++i) {
// Skip whitespace characters
char c = buffer[i];
if (c == ' ') continue;
if (found_decimal) {
if (found_significand) {
if (!found_exponent) {
if (c == '+' || c == '-') {
// In the case that we encounter +/- and we haven't yet encountered
@ -19,12 +19,14 @@ double cfloat_endf(const char* buffer, int n)
arr[j++] = 'e';
found_exponent = 1;
} else if (c == 'e' || c == 'E') {
} else if (c == 'e' || c == 'E' || c == 'd' || c == 'D') {
arr[j++] = 'e';
found_exponent = 1;
continue;
}
}
} else if (c == '.') {
found_decimal = 1;
} else if (c == '.' || (c >= '0' && c <= '9')) {
found_significand = 1;
}
// Copy character

View file

@ -11,6 +11,17 @@ def test_float_endf():
assert endf.float_endf(' -1.01- 2') == approx(-0.0101)
assert endf.float_endf('+ 2 . 3+ 1') == approx(23.0)
assert endf.float_endf('-7 .8 -1') == approx(-0.78)
assert endf.float_endf('3.14e0') == approx(3.14)
assert endf.float_endf('3.14E0') == approx(3.14)
assert endf.float_endf('3.14e-1') == approx(0.314)
assert endf.float_endf('3.14d0') == approx(3.14)
assert endf.float_endf('3.14D0') == approx(3.14)
assert endf.float_endf('3.14d-1') == approx(0.314)
assert endf.float_endf('1+2') == approx(100.0)
assert endf.float_endf('-1+2') == approx(-100.0)
assert endf.float_endf('1.+2') == approx(100.0)
assert endf.float_endf('-1.+2') == approx(-100.0)
assert endf.float_endf(' ') == 0.0
def test_int_endf():