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

C++ User Literal for amrex::Real (_rt) #577

Merged
merged 2 commits into from
Sep 26, 2019

Conversation

ax3l
Copy link
Member

@ax3l ax3l commented Sep 26, 2019

This adds a user literal for amrex::Real. This is a C++11 feature and allows to type constants properly instead of relying on implicit conversation.

A typical fallacy is the following user code:

auto const mypi = 3.14;
Real const sphere_volume = 4 / 3 * mypi * pow(r,3);

The first term 4/3 makes the whole expression an int until the result is casted to amrex::Real. Urgh, 4/3 == 1!

We can do better, with amrex::Real(4) / amrex::Real(3) but who wants to write such verbose code, right? Or 4./3. which is always a double operation? No. So C++11 user literals to the rescue!

auto const mypi = 3.14_rt; // that's an amrex:Real, too!
Real const sphere_volume = 4_rt / 3_rt * mypi * pow(r,3);

Note: the naming rt is borrowed from the Fortran equivalent usage in AMReX.

This adds a user literal for `amrex::Real`. This is a C++11
feature and allows to type constants properly instead of relying
on implicit conversation.

A typical fallacy is the following user code:
```C++
auto const mypi = 3.14;
Real const sphere_volume = 4/3 * mypi * pow(r,3);
```

The first term `4/3` makes the whole expression an `int` until
the result is casted to `amrex::Real`. Urgh!

We can do better, with `amrex::Real(4)/::amrex:Real(3)` but who
wants to write such verbose code, right? So C++11 user literals
to the rescue!
```C++
auto const mypi = 3.14_rt; // that's a real, too!
Real const sphere_volume 4_rt / 3_rt * mypi * pow(r,3);
```
@@ -769,7 +769,7 @@ FABio_8bit::write (std::ostream& os,
{
BL_ASSERT(comp >= 0 && num_comp >= 1 && (comp+num_comp) <= f.nComp());

const Real eps = Real(1.0e-8); // FIXME - whats a better value?
const Real eps = 1.0e-8_rt; // FIXME - whats a better value?
Copy link
Member Author

Choose a reason for hiding this comment

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

Btw, a better value might be std::numeric_limits<Real>::epsilon, without checking what this function actually does.

@WeiqunZhang WeiqunZhang merged commit 601a9b9 into AMReX-Codes:development Sep 26, 2019
@ax3l ax3l deleted the topic-userLiteralForReal branch September 26, 2019 23:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants