Skip to content

Fix unsound numeric_minimum bounds and add symmetric numeric_maximum#59

Open
gl84 wants to merge 1 commit into
boostorg:developfrom
gl84:fix/numeric_minimum
Open

Fix unsound numeric_minimum bounds and add symmetric numeric_maximum#59
gl84 wants to merge 1 commit into
boostorg:developfrom
gl84:fix/numeric_minimum

Conversation

@gl84

@gl84 gl84 commented Jun 27, 2026

Copy link
Copy Markdown

numeric_minimum relied solely on is_numeric::value to decide whether to query std::numeric_limits. For types where is_numeric is true but numeric_limits is not specialized (e.g. boost::rational), min()/max() silently fall back to a default-constructed T(), producing wrong bound checks (#58). It also used min() for the lower extreme, which for non-integral types is the smallest positive value rather than the most negative one.

Changes:

  • Guard all bound checks on std::numeric_limits::is_specialized; unspecialized numeric domains now impose no constraint instead of comparing against a bogus default-constructed bound.
  • Use lowest() (not min()) for the lower extreme.
  • Add a numeric_maximum trait mirroring numeric_minimum to detect domain_next overflow, and assert it wherever an interval boundary is built via domain_next (singleton, unit_trail, hull) and in first(), making first()/last() symmetric.
  • Add regression tests for the bound traits (int, double, rational) and for first()/last() across all bound flavors.

Fixes #58

…guards

numeric_minimum relied solely on is_numeric<T>::value to decide whether to
query std::numeric_limits<T>. For types where is_numeric is true but
numeric_limits is not specialized (e.g. boost::rational), min()/max() silently
fall back to a default-constructed T(), producing wrong bound checks
(boostorg#58). It also used min() for the lower extreme, which for non-integral types
is the smallest *positive* value rather than the most negative one.

Changes:
  - Guard all bound checks on std::numeric_limits<T>::is_specialized; unspecialized
    numeric domains now impose no constraint instead of comparing against a bogus
    default-constructed bound.
  - Use lowest() (not min()) for the lower extreme.
  - Add a numeric_maximum trait mirroring numeric_minimum to detect domain_next
    overflow, and assert it wherever an interval boundary is built via domain_next
    (singleton, unit_trail, hull) and in first(), making first()/last() symmetric.
  - Add regression tests for the bound traits (int, double, rational) and for
    first()/last() across all bound flavors.

Fixes boostorg#58
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.

numeric_minimum is unsound for boost::rational: is_numeric is true but numeric_limits is unspecialized

2 participants