mirror of
https://github.com/boostorg/safe_numerics.git
synced 2026-02-10 23:52:27 +00:00
fixed modulus and test_modulus
This commit is contained in:
@@ -40,8 +40,15 @@ class safe_base {
|
||||
template<class T>
|
||||
SAFE_NUMERIC_CONSTEXPR bool validate(const T & t) const {
|
||||
return ! (
|
||||
boost::numeric::checked::greater_than(t, base_value(Derived::max()))
|
||||
&& boost::numeric::checked::less_than(t, base_value(Derived::min()))
|
||||
boost::numeric::checked::greater_than(
|
||||
base_value(t),
|
||||
base_value(Derived::max())
|
||||
)
|
||||
&&
|
||||
boost::numeric::checked::less_than(
|
||||
base_value(t),
|
||||
base_value(Derived::min())
|
||||
)
|
||||
);
|
||||
}
|
||||
Stored m_t;
|
||||
@@ -92,7 +99,7 @@ public:
|
||||
// modification binary operators
|
||||
template<class T>
|
||||
Derived & operator=(const T & rhs){
|
||||
if(! derived().validate(rhs)){
|
||||
if(! validate(rhs)){
|
||||
E::range_error(
|
||||
"Invalid value passed on assignment"
|
||||
);
|
||||
|
||||
@@ -402,8 +402,9 @@ typename boost::lazy_enable_if<
|
||||
inline operator%(const T & t, const U & u){
|
||||
// argument dependent lookup should guarentee that we only get here
|
||||
// only if one of the types is a safe type. Verify this here
|
||||
typedef modulus_result<T, U> ar;
|
||||
typedef typename ar::type result_type;
|
||||
typedef modulus_result<T, U> mr;
|
||||
typedef typename mr::P::exception_policy exception_policy;
|
||||
typedef typename mr::type result_type;
|
||||
static_assert(
|
||||
boost::numeric::is_safe<result_type>::value,
|
||||
"Promotion failed to return safe type"
|
||||
@@ -438,9 +439,9 @@ inline operator%(const T & t, const U & u){
|
||||
base_value(u)
|
||||
);
|
||||
|
||||
r.template dispatch<typename ar::P::exception_policy>();
|
||||
|
||||
return static_cast<result_type>(r);
|
||||
r.template dispatch<exception_policy>();
|
||||
|
||||
return result_type(static_cast<result_base_type>(r));
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -24,7 +24,7 @@ bool test_modulus(
|
||||
{
|
||||
boost::numeric::safe<T1> t1 = v1;
|
||||
// presuming native policy
|
||||
boost::numeric::safe<decltype(v1 + v2)> result;
|
||||
boost::numeric::safe<decltype(v1 % v2)> result;
|
||||
|
||||
try{
|
||||
result = t1 % v2;
|
||||
@@ -41,7 +41,7 @@ bool test_modulus(
|
||||
<< " ! = "<< av1 << " % " << av2
|
||||
<< std::endl;
|
||||
try{
|
||||
result = t1 % v2;
|
||||
t1 % v2;
|
||||
}
|
||||
catch(std::exception){}
|
||||
return false;
|
||||
@@ -67,7 +67,7 @@ bool test_modulus(
|
||||
boost::numeric::safe<T2> t2 = v2;
|
||||
|
||||
// presuming native policy
|
||||
boost::numeric::safe<decltype(v1 + v2)> result;
|
||||
boost::numeric::safe<decltype(v1 % v2)> result;
|
||||
|
||||
try{
|
||||
result = t1 % t2;
|
||||
|
||||
Reference in New Issue
Block a user