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

Fix arbitrary precision rationals with negative denominator #41

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/boost/rational.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ BOOST_CXX14_CONSTEXPR void rational<IntType>::normalize()
num /= g;
den /= g;

if (den < -(std::numeric_limits<IntType>::max)()) {
if (std::numeric_limits<IntType>::is_bounded && den < -(std::numeric_limits<IntType>::max)()) {
BOOST_THROW_EXCEPTION(bad_rational("bad rational: non-zero singular denominator"));
}

Expand Down
13 changes: 13 additions & 0 deletions test/rational_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <boost/config.hpp>
#include <boost/limits.hpp>
#include <boost/mpl/list.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/operators.hpp>
#include <boost/preprocessor/stringize.hpp>
#include <boost/integer/common_factor_rt.hpp>
Expand Down Expand Up @@ -1605,4 +1606,16 @@ BOOST_AUTO_TEST_CASE( ticket_9067_test )
#endif
}

// arbitrary precision rational with negative denominator throws exception
BOOST_AUTO_TEST_CASE( issue_27_test )
{
using namespace boost::multiprecision;

// Verify that the check to ensure that the denominator is positive works
// with an arbitrary precision rational.
cpp_rational rational(1, -2);
BOOST_CHECK_EQUAL(numerator(rational), -1);
BOOST_CHECK_EQUAL(denominator(rational), 2);
}

BOOST_AUTO_TEST_SUITE_END()