Skip to content

Commit

Permalink
ParmParse::queryAsDouble: Fix bug in AMReX-Codes#4152 & AMReX-Codes#4149
Browse files Browse the repository at this point in the history
 (AMReX-Codes#4154)

std::round should only be used when the value type is an integer, not a
floating point number.
  • Loading branch information
WeiqunZhang authored Sep 17, 2024
1 parent 97fcea3 commit a0ed390
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions Src/Base/AMReX_ParmParse.H
Original file line number Diff line number Diff line change
Expand Up @@ -1470,9 +1470,11 @@ public:
double dref;
int exist = queryWithParser(name, dref);
if (exist) {
dref = std::round(dref);
if (std::is_integral_v<value_type>) {
dref = std::round(dref);
}
auto vref = static_cast<value_type>(dref);
if constexpr (!std::is_same_v<value_type,bool>) {
if constexpr (std::is_integral_v<value_type> && !std::is_same_v<value_type,bool>) {
if (static_cast<double>(vref) != dref) {
amrex::Abort("ParmParse:: queryAsDouble is not safe");
}
Expand All @@ -1498,9 +1500,11 @@ public:
int exist = queryarrWithParser(name, nvals, dref.data());
if (exist) {
for (int i = 0; i < nvals; ++i) {
dref[i] = std::round(dref[i]);
if (std::is_integral_v<value_type>) {
dref[i] = std::round(dref[i]);
}
auto vref = static_cast<value_type>(dref[i]);
if constexpr (!std::is_same_v<value_type,bool>) {
if constexpr (std::is_integral_v<value_type> && !std::is_same_v<value_type,bool>) {
if (static_cast<double>(vref) != dref[i]) {
amrex::Abort("ParmParse:: queryarrAsDouble is not safe");
}
Expand Down

0 comments on commit a0ed390

Please sign in to comment.