Update contrib.
1 /* Boost interval/interval.hpp header file
3 * Copyright 2002-2003 Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE_1_0.txt or
7 * copy at http://www.boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_NUMERIC_INTERVAL_INTERVAL_HPP
11 #define BOOST_NUMERIC_INTERVAL_INTERVAL_HPP
15 #include <boost/numeric/interval/detail/interval_prototype.hpp>
20 namespace interval_lib {
22 class comparison_error
23 : public std::runtime_error
27 : std::runtime_error("boost::interval: uncertain comparison")
31 } // namespace interval_lib
37 template<class T, class Policies>
41 struct interval_holder;
45 typedef Policies traits_type;
47 T const &lower() const;
48 T const &upper() const;
52 template<class T1> interval(T1 const &v);
53 interval(T const &l, T const &u);
54 template<class T1, class T2> interval(T1 const &l, T2 const &u);
55 interval(interval<T, Policies> const &r);
56 template<class Policies1> interval(interval<T, Policies1> const &r);
57 template<class T1, class Policies1> interval(interval<T1, Policies1> const &r);
59 interval &operator=(T const &v);
60 template<class T1> interval &operator=(T1 const &v);
61 interval &operator=(interval<T, Policies> const &r);
62 template<class Policies1> interval &operator=(interval<T, Policies1> const &r);
63 template<class T1, class Policies1> interval &operator=(interval<T1, Policies1> const &r);
65 void assign(const T& l, const T& u);
67 static interval empty();
68 static interval whole();
69 static interval hull(const T& x, const T& y);
71 interval& operator+= (const T& r);
72 interval& operator+= (const interval& r);
73 interval& operator-= (const T& r);
74 interval& operator-= (const interval& r);
75 interval& operator*= (const T& r);
76 interval& operator*= (const interval& r);
77 interval& operator/= (const T& r);
78 interval& operator/= (const interval& r);
80 bool operator< (const interval_holder& r) const;
81 bool operator> (const interval_holder& r) const;
82 bool operator<= (const interval_holder& r) const;
83 bool operator>= (const interval_holder& r) const;
84 bool operator== (const interval_holder& r) const;
85 bool operator!= (const interval_holder& r) const;
87 bool operator< (const number_holder& r) const;
88 bool operator> (const number_holder& r) const;
89 bool operator<= (const number_holder& r) const;
90 bool operator>= (const number_holder& r) const;
91 bool operator== (const number_holder& r) const;
92 bool operator!= (const number_holder& r) const;
94 // the following is for internal use only, it is not a published interface
95 // nevertheless, it's public because friends don't always work correctly.
96 interval(const T& l, const T& u, bool): low(l), up(u) {}
99 void set(const T& l, const T& u);
102 struct interval_holder {
103 template<class Policies2>
104 interval_holder(const interval<T, Policies2>& r)
105 : low(r.lower()), up(r.upper())
107 typedef typename Policies2::checking checking2;
108 if (checking2::is_empty(low, up))
109 throw interval_lib::comparison_error();
116 struct number_holder {
117 number_holder(const T& r) : val(r)
119 typedef typename Policies::checking checking;
120 if (checking::is_nan(r))
121 throw interval_lib::comparison_error();
127 typedef typename Policies::checking checking;
128 typedef typename Policies::rounding rounding;
134 template<class T, class Policies> inline
135 interval<T, Policies>::interval():
136 low(static_cast<T>(0)), up(static_cast<T>(0))
139 template<class T, class Policies> inline
140 interval<T, Policies>::interval(T const &v): low(v), up(v)
142 if (checking::is_nan(v)) set_empty();
145 template<class T, class Policies> template<class T1> inline
146 interval<T, Policies>::interval(T1 const &v)
148 if (checking::is_nan(v)) set_empty();
151 low = rnd.conv_down(v);
152 up = rnd.conv_up (v);
156 template<class T, class Policies> template<class T1, class T2> inline
157 interval<T, Policies>::interval(T1 const &l, T2 const &u)
159 if (checking::is_nan(l) || checking::is_nan(u) || !(l <= u)) set_empty();
162 low = rnd.conv_down(l);
163 up = rnd.conv_up (u);
167 template<class T, class Policies> inline
168 interval<T, Policies>::interval(T const &l, T const &u): low(l), up(u)
170 if (checking::is_nan(l) || checking::is_nan(u) || !(l <= u))
175 template<class T, class Policies> inline
176 interval<T, Policies>::interval(interval<T, Policies> const &r): low(r.lower()), up(r.upper())
179 template<class T, class Policies> template<class Policies1> inline
180 interval<T, Policies>::interval(interval<T, Policies1> const &r): low(r.lower()), up(r.upper())
182 typedef typename Policies1::checking checking1;
183 if (checking1::is_empty(r.lower(), r.upper())) set_empty();
186 template<class T, class Policies> template<class T1, class Policies1> inline
187 interval<T, Policies>::interval(interval<T1, Policies1> const &r)
189 typedef typename Policies1::checking checking1;
190 if (checking1::is_empty(r.lower(), r.upper())) set_empty();
193 low = rnd.conv_down(r.lower());
194 up = rnd.conv_up (r.upper());
198 template<class T, class Policies> inline
199 interval<T, Policies> &interval<T, Policies>::operator=(T const &v)
201 if (checking::is_nan(v)) set_empty();
206 template<class T, class Policies> template<class T1> inline
207 interval<T, Policies> &interval<T, Policies>::operator=(T1 const &v)
209 if (checking::is_nan(v)) set_empty();
212 low = rnd.conv_down(v);
213 up = rnd.conv_up (v);
218 template<class T, class Policies> inline
219 interval<T, Policies> &interval<T, Policies>::operator=(interval<T, Policies> const &r)
226 template<class T, class Policies> template<class Policies1> inline
227 interval<T, Policies> &interval<T, Policies>::operator=(interval<T, Policies1> const &r)
229 typedef typename Policies1::checking checking1;
230 if (checking1::is_empty(r.lower(), r.upper())) set_empty();
238 template<class T, class Policies> template<class T1, class Policies1> inline
239 interval<T, Policies> &interval<T, Policies>::operator=(interval<T1, Policies1> const &r)
241 typedef typename Policies1::checking checking1;
242 if (checking1::is_empty(r.lower(), r.upper())) set_empty();
245 low = rnd.conv_down(r.lower());
246 up = rnd.conv_up (r.upper());
251 template<class T, class Policies> inline
252 void interval<T, Policies>::assign(const T& l, const T& u)
254 if (checking::is_nan(l) || checking::is_nan(u) || !(l <= u))
259 template<class T, class Policies> inline
260 void interval<T, Policies>::set(const T& l, const T& u)
266 template<class T, class Policies> inline
267 void interval<T, Policies>::set_empty()
269 low = checking::empty_lower();
270 up = checking::empty_upper();
273 template<class T, class Policies> inline
274 void interval<T, Policies>::set_whole()
276 low = checking::neg_inf();
277 up = checking::pos_inf();
280 template<class T, class Policies> inline
281 interval<T, Policies> interval<T, Policies>::hull(const T& x, const T& y)
283 bool bad_x = checking::is_nan(x);
284 bool bad_y = checking::is_nan(y);
286 if (bad_y) return interval::empty();
287 else return interval(y, y, true);
289 if (bad_y) return interval(x, x, true);
290 if (x <= y) return interval(x, y, true);
291 else return interval(y, x, true);
294 template<class T, class Policies> inline
295 interval<T, Policies> interval<T, Policies>::empty()
297 return interval<T, Policies>(checking::empty_lower(),
298 checking::empty_upper(), true);
301 template<class T, class Policies> inline
302 interval<T, Policies> interval<T, Policies>::whole()
304 return interval<T, Policies>(checking::neg_inf(), checking::pos_inf(), true);
307 template<class T, class Policies> inline
308 const T& interval<T, Policies>::lower() const
313 template<class T, class Policies> inline
314 const T& interval<T, Policies>::upper() const
320 * interval/interval comparisons
323 template<class T, class Policies> inline
324 bool interval<T, Policies>::operator< (const interval_holder& r) const
326 if (!checking::is_empty(low, up)) {
327 if (up < r.low) return true;
328 else if (low >= r.up) return false;
330 throw interval_lib::comparison_error();
333 template<class T, class Policies> inline
334 bool interval<T, Policies>::operator> (const interval_holder& r) const
336 if (!checking::is_empty(low, up)) {
337 if (low > r.up) return true;
338 else if (up <= r.low) return false;
340 throw interval_lib::comparison_error();
343 template<class T, class Policies> inline
344 bool interval<T, Policies>::operator<= (const interval_holder& r) const
346 if (!checking::is_empty(low, up)) {
347 if (up <= r.low) return true;
348 else if (low > r.up) return false;
350 throw interval_lib::comparison_error();
353 template<class T, class Policies> inline
354 bool interval<T, Policies>::operator>= (const interval_holder& r) const
356 if (!checking::is_empty(low, up)) {
357 if (low >= r.up) return true;
358 else if (up < r.low) return false;
360 throw interval_lib::comparison_error();
363 template<class T, class Policies> inline
364 bool interval<T, Policies>::operator== (const interval_holder& r) const
366 if (!checking::is_empty(low, up)) {
367 if (up == r.low && low == r.up) return true;
368 else if (up < r.low || low > r.up) return false;
370 throw interval_lib::comparison_error();
373 template<class T, class Policies> inline
374 bool interval<T, Policies>::operator!= (const interval_holder& r) const
376 if (!checking::is_empty(low, up)) {
377 if (up < r.low || low > r.up) return true;
378 else if (up == r.low && low == r.up) return false;
380 throw interval_lib::comparison_error();
384 * interval/number comparisons
387 template<class T, class Policies> inline
388 bool interval<T, Policies>::operator< (const number_holder& r) const
390 if (!checking::is_empty(low, up)) {
391 if (up < r.val) return true;
392 else if (low >= r.val) return false;
394 throw interval_lib::comparison_error();
397 template<class T, class Policies> inline
398 bool interval<T, Policies>::operator> (const number_holder& r) const
400 if (!checking::is_empty(low, up)) {
401 if (low > r.val) return true;
402 else if (up <= r.val) return false;
404 throw interval_lib::comparison_error();
407 template<class T, class Policies> inline
408 bool interval<T, Policies>::operator<= (const number_holder& r) const
410 if (!checking::is_empty(low, up)) {
411 if (up <= r.val) return true;
412 else if (low > r.val) return false;
414 throw interval_lib::comparison_error();
417 template<class T, class Policies> inline
418 bool interval<T, Policies>::operator>= (const number_holder& r) const
420 if (!checking::is_empty(low, up)) {
421 if (low >= r.val) return true;
422 else if (up < r.val) return false;
424 throw interval_lib::comparison_error();
427 template<class T, class Policies> inline
428 bool interval<T, Policies>::operator== (const number_holder& r) const
430 if (!checking::is_empty(low, up)) {
431 if (up == r.val && low == r.val) return true;
432 else if (up < r.val || low > r.val) return false;
434 throw interval_lib::comparison_error();
437 template<class T, class Policies> inline
438 bool interval<T, Policies>::operator!= (const number_holder& r) const
440 if (!checking::is_empty(low, up)) {
441 if (up < r.val || low > r.val) return true;
442 else if (up == r.val && low == r.val) return false;
444 throw interval_lib::comparison_error();
447 } // namespace numeric
450 #endif // BOOST_NUMERIC_INTERVAL_INTERVAL_HPP