Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IParser: Use long long int #4046

Merged
merged 2 commits into from
Jul 26, 2024

Conversation

WeiqunZhang
Copy link
Member

  • Use long long int as the underlying integer data type in IParser.

  • Extend number format to include 3e9, 2.34e6, 1'000'000, etc.

@WeiqunZhang WeiqunZhang marked this pull request as draft July 24, 2024 04:42
@WeiqunZhang
Copy link
Member Author

For this test, https://github.com/WeiqunZhang/amrex-devtests/blob/main/iparser_long/main.cpp

    {
        std::vector<std::string> vs{"1'234'567'890'123", // good
                                    "-1000",             // good
                                    "5e2",               // good
                                    "5.400e4",           // good
                                    "-5.4e2",            // good
                                    ".123e5",            // good
                                    "0.123e5",           // good
                                    "5.43e2",            // good
                                    "1234e0",            // good         
                                    "1234.567e3",        // good
                                    "3e-4",              // bad
                                    "5.432e2",           // bad
                                    "10000e-3",          // bad
                                    "3.14"};             // bad
        for (auto const& s : vs) {
            std::cout << "s = " << std::setw(18) << s;
            try {
                IParser iparser(s);
                auto exe = iparser.compile<0>();
                auto i = exe();
                std::cout << "    i = " << i << "\n";
            } catch (std::runtime_error const& e) {
                std::cout << "    error: " << e.what() << '\n';
            }
        }
    }

produces

s =  1'234'567'890'123    i = 1234567890123
s =              -1000    i = -1000
s =                5e2    i = 500
s =            5.400e4    i = 54000
s =             -5.4e2    i = -540
s =             .123e5    i = 12300
s =            0.123e5    i = 12300
s =             5.43e2    i = 543
s =             1234e0    i = 1234
s =         1234.567e3    i = 1234567
s =               3e-4    error: syntax error in IParser expression "3e-4"
s =            5.432e2    error: 5.432e2 is not an integer in IParser expression "5.432e2"
s =           10000e-3    error: syntax error in IParser expression "10000e-3"
s =               3.14    error: 3.14 is not an integer in IParser expression "3.14"

@WeiqunZhang
Copy link
Member Author

We could allow 10000e-3, if people want it.

@WeiqunZhang WeiqunZhang force-pushed the iparser_long branch 2 times, most recently from 0841726 to 9eac969 Compare July 24, 2024 22:26
@WeiqunZhang WeiqunZhang marked this pull request as ready for review July 24, 2024 22:26
@WeiqunZhang WeiqunZhang force-pushed the iparser_long branch 2 times, most recently from 4e9fcda to af03c0f Compare July 24, 2024 22:44
Tests/Parser/main.cpp Fixed Show fixed Hide fixed
* Use long long int as the underlying integer data type in IParser.

* Extend number format to include `3e9`, `2.34e6`, `1'000'000`, etc.
as a separator for :cpp:`IParser` numbers just like C++ integer
literals. Additionally, a floating point like number with a positive
exponent may be accepted as an integer if it is reasonable to do so. For
example, it's okay to have ``1.234e3``, but ``1.23e2`` is an error.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the former reasonable but the latter not?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A typo.

@WeiqunZhang WeiqunZhang merged commit 80b0c7e into AMReX-Codes:development Jul 26, 2024
71 checks passed
@WeiqunZhang WeiqunZhang deleted the iparser_long branch July 26, 2024 18:11
@WeiqunZhang WeiqunZhang restored the iparser_long branch July 26, 2024 18:11
@WeiqunZhang WeiqunZhang deleted the iparser_long branch July 26, 2024 18:11
WeiqunZhang pushed a commit that referenced this pull request Aug 1, 2024
#4046 changed the type of IParser from int to long long, however
in pp_make_parser it was still cast to int which can cause an
overflow.  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants