Skip to main content
GameDev.net gamedev.net
🔒 Locked

boost::numeric::interval giving trouble

Started by discman1028 Sep 25, 2006 at 12:26 PM 1 replies 1.5k views
Original Post
discman1028
discman1028
Hey all, I'm using boost::numeric::interval for its interval intersection routine. When I have two intervals that do not intersect, and I call the intersection routine, it throws an exception about "making an empty interval".

boost::numeric::interval<int> inter1( 10, 100 );
boost::numeric::interval<int> inter2( 101, 109 );
boost::numeric::interval<int> inter3;
inter3 = boost::numeric::intersect( inter1, inter2 ); // throws boost "empty interval creation" exception

But I would like it to make an empty interval! It even has a function to check if an interval is empty: template bool empty(const interval& b); I commented out the exception thrown and continued, but the empty() function above still returned false. What.. the heck?
--== discman1028 ==--
Enigma
Enigma
The interval class has a template policy argument which determines the behaviour under certain conditions, such as creating empty intervals. The default policy is "strict checking", which throws an exception rather than create an empty interval. You need to create interval objects with an alternative policy. Boost.Interval contains a number of policy classes. I suspect what you want is:
typedef boost::numeric::interval_lib::rounded_math< int > rounding_policy;typedef boost::numeric::interval_lib::checking_no_nan< int > checking_policy;typedef boost::numeric::interval_lib::policies< rounding_policy, checking_policy > interval_policies;typedef boost::numeric::interval< int, interval_policies > interval;interval inter1(10, 100);interval inter2(101, 109);interval inter3 = boost::numeric::intersect(inter1, inter2);
EDIT: As to why the empty function returns false for you, the policy for the objects you are using prevents empty intervals from being created, so the implementation of empty for that policy is just to return false. An interval under that policy cannot be empty. Unless you hack the source code!

Σnigma
discman1028
discman1028
Thanks Enigma!
--== discman1028 ==--

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.