From ef0d216f662327316c26072412f591f342a7312e Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Thu, 4 Apr 2019 16:40:28 -0500 Subject: [PATCH] Fix float_endf for decimal-less numbers. Also secretly support d/D --- openmc/data/endf.c | 12 +++++++----- tests/unit_tests/test_endf.py | 11 +++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/openmc/data/endf.c b/openmc/data/endf.c index b83e130c73..fee6af179e 100644 --- a/openmc/data/endf.c +++ b/openmc/data/endf.c @@ -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 diff --git a/tests/unit_tests/test_endf.py b/tests/unit_tests/test_endf.py index b6f79291de..11a8db4432 100644 --- a/tests/unit_tests/test_endf.py +++ b/tests/unit_tests/test_endf.py @@ -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():