interval.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /* Boost interval/interval.hpp header file
  2. *
  3. * Copyright 2002-2003 Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion
  4. *
  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)
  8. */
  9. #ifndef BOOST_NUMERIC_INTERVAL_INTERVAL_HPP
  10. #define BOOST_NUMERIC_INTERVAL_INTERVAL_HPP
  11. #include <stdexcept>
  12. #include <string>
  13. #include <boost/numeric/interval/detail/interval_prototype.hpp>
  14. namespace boost {
  15. namespace numeric {
  16. namespace interval_lib {
  17. class comparison_error
  18. : public std::runtime_error
  19. {
  20. public:
  21. comparison_error()
  22. : std::runtime_error("boost::interval: uncertain comparison")
  23. { }
  24. };
  25. } // namespace interval_lib
  26. /*
  27. * interval class
  28. */
  29. template<class T, class Policies>
  30. class interval
  31. {
  32. private:
  33. struct interval_holder;
  34. struct number_holder;
  35. public:
  36. typedef T base_type;
  37. typedef Policies traits_type;
  38. T const &lower() const;
  39. T const &upper() const;
  40. interval();
  41. interval(T const &v);
  42. template<class T1> interval(T1 const &v);
  43. interval(T const &l, T const &u);
  44. template<class T1, class T2> interval(T1 const &l, T2 const &u);
  45. interval(interval<T, Policies> const &r);
  46. template<class Policies1> interval(interval<T, Policies1> const &r);
  47. template<class T1, class Policies1> interval(interval<T1, Policies1> const &r);
  48. interval &operator=(T const &v);
  49. template<class T1> interval &operator=(T1 const &v);
  50. interval &operator=(interval<T, Policies> const &r);
  51. template<class Policies1> interval &operator=(interval<T, Policies1> const &r);
  52. template<class T1, class Policies1> interval &operator=(interval<T1, Policies1> const &r);
  53. void assign(const T& l, const T& u);
  54. static interval empty();
  55. static interval whole();
  56. static interval hull(const T& x, const T& y);
  57. interval& operator+= (const T& r);
  58. interval& operator+= (const interval& r);
  59. interval& operator-= (const T& r);
  60. interval& operator-= (const interval& r);
  61. interval& operator*= (const T& r);
  62. interval& operator*= (const interval& r);
  63. interval& operator/= (const T& r);
  64. interval& operator/= (const interval& r);
  65. bool operator< (const interval_holder& r) const;
  66. bool operator> (const interval_holder& r) const;
  67. bool operator<= (const interval_holder& r) const;
  68. bool operator>= (const interval_holder& r) const;
  69. bool operator== (const interval_holder& r) const;
  70. bool operator!= (const interval_holder& r) const;
  71. bool operator< (const number_holder& r) const;
  72. bool operator> (const number_holder& r) const;
  73. bool operator<= (const number_holder& r) const;
  74. bool operator>= (const number_holder& r) const;
  75. bool operator== (const number_holder& r) const;
  76. bool operator!= (const number_holder& r) const;
  77. // the following is for internal use only, it is not a published interface
  78. // nevertheless, it's public because friends don't always work correctly.
  79. interval(const T& l, const T& u, bool): low(l), up(u) {}
  80. void set_empty();
  81. void set_whole();
  82. void set(const T& l, const T& u);
  83. private:
  84. struct interval_holder {
  85. template<class Policies2>
  86. interval_holder(const interval<T, Policies2>& r)
  87. : low(r.lower()), up(r.upper())
  88. {
  89. typedef typename Policies2::checking checking2;
  90. if (checking2::is_empty(low, up))
  91. throw interval_lib::comparison_error();
  92. }
  93. const T& low;
  94. const T& up;
  95. };
  96. struct number_holder {
  97. number_holder(const T& r) : val(r)
  98. {
  99. typedef typename Policies::checking checking;
  100. if (checking::is_nan(r))
  101. throw interval_lib::comparison_error();
  102. }
  103. const T& val;
  104. };
  105. typedef typename Policies::checking checking;
  106. typedef typename Policies::rounding rounding;
  107. T low;
  108. T up;
  109. };
  110. template<class T, class Policies> inline
  111. interval<T, Policies>::interval():
  112. low(static_cast<T>(0)), up(static_cast<T>(0))
  113. {}
  114. template<class T, class Policies> inline
  115. interval<T, Policies>::interval(T const &v): low(v), up(v)
  116. {
  117. if (checking::is_nan(v)) set_empty();
  118. }
  119. template<class T, class Policies> template<class T1> inline
  120. interval<T, Policies>::interval(T1 const &v)
  121. {
  122. if (checking::is_nan(v)) set_empty();
  123. else {
  124. rounding rnd;
  125. low = rnd.conv_down(v);
  126. up = rnd.conv_up (v);
  127. }
  128. }
  129. template<class T, class Policies> template<class T1, class T2> inline
  130. interval<T, Policies>::interval(T1 const &l, T2 const &u)
  131. {
  132. if (checking::is_nan(l) || checking::is_nan(u) || !(l <= u)) set_empty();
  133. else {
  134. rounding rnd;
  135. low = rnd.conv_down(l);
  136. up = rnd.conv_up (u);
  137. }
  138. }
  139. template<class T, class Policies> inline
  140. interval<T, Policies>::interval(T const &l, T const &u): low(l), up(u)
  141. {
  142. if (checking::is_nan(l) || checking::is_nan(u) || !(l <= u))
  143. set_empty();
  144. }
  145. template<class T, class Policies> inline
  146. interval<T, Policies>::interval(interval<T, Policies> const &r): low(r.lower()), up(r.upper())
  147. {}
  148. template<class T, class Policies> template<class Policies1> inline
  149. interval<T, Policies>::interval(interval<T, Policies1> const &r): low(r.lower()), up(r.upper())
  150. {
  151. typedef typename Policies1::checking checking1;
  152. if (checking1::is_empty(r.lower(), r.upper())) set_empty();
  153. }
  154. template<class T, class Policies> template<class T1, class Policies1> inline
  155. interval<T, Policies>::interval(interval<T1, Policies1> const &r)
  156. {
  157. typedef typename Policies1::checking checking1;
  158. if (checking1::is_empty(r.lower(), r.upper())) set_empty();
  159. else {
  160. rounding rnd;
  161. low = rnd.conv_down(r.lower());
  162. up = rnd.conv_up (r.upper());
  163. }
  164. }
  165. template<class T, class Policies> inline
  166. interval<T, Policies> &interval<T, Policies>::operator=(T const &v)
  167. {
  168. if (checking::is_nan(v)) set_empty();
  169. else low = up = v;
  170. return *this;
  171. }
  172. template<class T, class Policies> template<class T1> inline
  173. interval<T, Policies> &interval<T, Policies>::operator=(T1 const &v)
  174. {
  175. if (checking::is_nan(v)) set_empty();
  176. else {
  177. rounding rnd;
  178. low = rnd.conv_down(v);
  179. up = rnd.conv_up (v);
  180. }
  181. return *this;
  182. }
  183. template<class T, class Policies> inline
  184. interval<T, Policies> &interval<T, Policies>::operator=(interval<T, Policies> const &r)
  185. {
  186. low = r.lower();
  187. up = r.upper();
  188. return *this;
  189. }
  190. template<class T, class Policies> template<class Policies1> inline
  191. interval<T, Policies> &interval<T, Policies>::operator=(interval<T, Policies1> const &r)
  192. {
  193. typedef typename Policies1::checking checking1;
  194. if (checking1::is_empty(r.lower(), r.upper())) set_empty();
  195. else {
  196. low = r.lower();
  197. up = r.upper();
  198. }
  199. return *this;
  200. }
  201. template<class T, class Policies> template<class T1, class Policies1> inline
  202. interval<T, Policies> &interval<T, Policies>::operator=(interval<T1, Policies1> const &r)
  203. {
  204. typedef typename Policies1::checking checking1;
  205. if (checking1::is_empty(r.lower(), r.upper())) set_empty();
  206. else {
  207. rounding rnd;
  208. low = rnd.conv_down(r.lower());
  209. up = rnd.conv_up (r.upper());
  210. }
  211. return *this;
  212. }
  213. template<class T, class Policies> inline
  214. void interval<T, Policies>::assign(const T& l, const T& u)
  215. {
  216. if (checking::is_nan(l) || checking::is_nan(u) || !(l <= u))
  217. set_empty();
  218. else set(l, u);
  219. }
  220. template<class T, class Policies> inline
  221. void interval<T, Policies>::set(const T& l, const T& u)
  222. {
  223. low = l;
  224. up = u;
  225. }
  226. template<class T, class Policies> inline
  227. void interval<T, Policies>::set_empty()
  228. {
  229. low = checking::empty_lower();
  230. up = checking::empty_upper();
  231. }
  232. template<class T, class Policies> inline
  233. void interval<T, Policies>::set_whole()
  234. {
  235. low = checking::neg_inf();
  236. up = checking::pos_inf();
  237. }
  238. template<class T, class Policies> inline
  239. interval<T, Policies> interval<T, Policies>::hull(const T& x, const T& y)
  240. {
  241. bool bad_x = checking::is_nan(x);
  242. bool bad_y = checking::is_nan(y);
  243. if (bad_x)
  244. if (bad_y) return interval::empty();
  245. else return interval(y, y, true);
  246. else
  247. if (bad_y) return interval(x, x, true);
  248. if (x <= y) return interval(x, y, true);
  249. else return interval(y, x, true);
  250. }
  251. template<class T, class Policies> inline
  252. interval<T, Policies> interval<T, Policies>::empty()
  253. {
  254. return interval<T, Policies>(checking::empty_lower(),
  255. checking::empty_upper(), true);
  256. }
  257. template<class T, class Policies> inline
  258. interval<T, Policies> interval<T, Policies>::whole()
  259. {
  260. return interval<T, Policies>(checking::neg_inf(), checking::pos_inf(), true);
  261. }
  262. template<class T, class Policies> inline
  263. const T& interval<T, Policies>::lower() const
  264. {
  265. return low;
  266. }
  267. template<class T, class Policies> inline
  268. const T& interval<T, Policies>::upper() const
  269. {
  270. return up;
  271. }
  272. /*
  273. * interval/interval comparisons
  274. */
  275. template<class T, class Policies> inline
  276. bool interval<T, Policies>::operator< (const interval_holder& r) const
  277. {
  278. if (!checking::is_empty(low, up)) {
  279. if (up < r.low) return true;
  280. else if (low >= r.up) return false;
  281. }
  282. throw interval_lib::comparison_error();
  283. }
  284. template<class T, class Policies> inline
  285. bool interval<T, Policies>::operator> (const interval_holder& r) const
  286. {
  287. if (!checking::is_empty(low, up)) {
  288. if (low > r.up) return true;
  289. else if (up <= r.low) return false;
  290. }
  291. throw interval_lib::comparison_error();
  292. }
  293. template<class T, class Policies> inline
  294. bool interval<T, Policies>::operator<= (const interval_holder& r) const
  295. {
  296. if (!checking::is_empty(low, up)) {
  297. if (up <= r.low) return true;
  298. else if (low > r.up) return false;
  299. }
  300. throw interval_lib::comparison_error();
  301. }
  302. template<class T, class Policies> inline
  303. bool interval<T, Policies>::operator>= (const interval_holder& r) const
  304. {
  305. if (!checking::is_empty(low, up)) {
  306. if (low >= r.up) return true;
  307. else if (up < r.low) return false;
  308. }
  309. throw interval_lib::comparison_error();
  310. }
  311. template<class T, class Policies> inline
  312. bool interval<T, Policies>::operator== (const interval_holder& r) const
  313. {
  314. if (!checking::is_empty(low, up)) {
  315. if (up == r.low && low == r.up) return true;
  316. else if (up < r.low || low > r.up) return false;
  317. }
  318. throw interval_lib::comparison_error();
  319. }
  320. template<class T, class Policies> inline
  321. bool interval<T, Policies>::operator!= (const interval_holder& r) const
  322. {
  323. if (!checking::is_empty(low, up)) {
  324. if (up < r.low || low > r.up) return true;
  325. else if (up == r.low && low == r.up) return false;
  326. }
  327. throw interval_lib::comparison_error();
  328. }
  329. /*
  330. * interval/number comparisons
  331. */
  332. template<class T, class Policies> inline
  333. bool interval<T, Policies>::operator< (const number_holder& r) const
  334. {
  335. if (!checking::is_empty(low, up)) {
  336. if (up < r.val) return true;
  337. else if (low >= r.val) return false;
  338. }
  339. throw interval_lib::comparison_error();
  340. }
  341. template<class T, class Policies> inline
  342. bool interval<T, Policies>::operator> (const number_holder& r) const
  343. {
  344. if (!checking::is_empty(low, up)) {
  345. if (low > r.val) return true;
  346. else if (up <= r.val) return false;
  347. }
  348. throw interval_lib::comparison_error();
  349. }
  350. template<class T, class Policies> inline
  351. bool interval<T, Policies>::operator<= (const number_holder& r) const
  352. {
  353. if (!checking::is_empty(low, up)) {
  354. if (up <= r.val) return true;
  355. else if (low > r.val) return false;
  356. }
  357. throw interval_lib::comparison_error();
  358. }
  359. template<class T, class Policies> inline
  360. bool interval<T, Policies>::operator>= (const number_holder& r) const
  361. {
  362. if (!checking::is_empty(low, up)) {
  363. if (low >= r.val) return true;
  364. else if (up < r.val) return false;
  365. }
  366. throw interval_lib::comparison_error();
  367. }
  368. template<class T, class Policies> inline
  369. bool interval<T, Policies>::operator== (const number_holder& r) const
  370. {
  371. if (!checking::is_empty(low, up)) {
  372. if (up == r.val && low == r.val) return true;
  373. else if (up < r.val || low > r.val) return false;
  374. }
  375. throw interval_lib::comparison_error();
  376. }
  377. template<class T, class Policies> inline
  378. bool interval<T, Policies>::operator!= (const number_holder& r) const
  379. {
  380. if (!checking::is_empty(low, up)) {
  381. if (up < r.val || low > r.val) return true;
  382. else if (up == r.val && low == r.val) return false;
  383. }
  384. throw interval_lib::comparison_error();
  385. }
  386. } // namespace numeric
  387. } // namespace boost
  388. #endif // BOOST_NUMERIC_INTERVAL_INTERVAL_HPP