-
Notifications
You must be signed in to change notification settings - Fork 241
Open
Description
When checking input arguments, it's common to want min <= max
. If this statement is not true, the program needs to error. One way to make sure the statement is true is to assert that it is true. E.g., with:
BOOST_MATH_ASSERT(min <= max);
A common way of checking the assertion that min <= max
in the Boost codebase is to use the construction:
if (min > max) {
return policies::raise_evaluation_error(function, "assertion min <= max failed (first arg=%1%)", min, boost::math::policies::policy<>());
}
The first approach has three advantages over the second approach:
- It is cleaner and clearer (imo)
- It makes disagreement between the error message and performed check impossible (e.g., update beta error messages and checks #1003)
- It gives the right answer when one of both arguments is
nan
. E.g.,
const auto fn = [](double x) { return std::make_pair(x, 1.0); };
const double inf = std::numeric_limits<double>::infinity();
const double nan = std::numeric_limits<double>::quiet_NaN();
const double x0 = boost::math::tools::newton_raphson_iterate(fn, -inf, /* initial guess */
inf, /* lower bound */
nan, /* upper bound */
52);
gives x0 = nan
, even though the lower bound is inf
.
NOTE:
The six comparison operators are: ==
, <
, <=
, >
, >=
, and !=
. For all comparison operators besides !=
, if one or both of the arguments are nan
, the result is false
according to the IEEE standard.
Metadata
Metadata
Assignees
Labels
No labels