interval.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #ifndef BOOST_SAFE_NUMERICS_INTERVAL_HPP
  2. #define BOOST_SAFE_NUMERICS_INTERVAL_HPP
  3. // Copyright (c) 2012 Robert Ramey
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #include <limits>
  9. #include <cassert>
  10. #include <type_traits>
  11. #include <initializer_list>
  12. #include <algorithm> // minmax, min, max
  13. #include <boost/logic/tribool.hpp>
  14. #include "utility.hpp" // log
  15. // from stack overflow
  16. // http://stackoverflow.com/questions/23815138/implementing-variadic-min-max-functions
  17. namespace boost {
  18. namespace safe_numerics {
  19. template<typename R>
  20. struct interval {
  21. const R l;
  22. const R u;
  23. template<typename T>
  24. constexpr interval(const T & lower, const T & upper) :
  25. l(lower),
  26. u(upper)
  27. {
  28. // assert(static_cast<bool>(l <= u));
  29. }
  30. template<typename T>
  31. constexpr interval(const std::pair<T, T> & p) :
  32. l(p.first),
  33. u(p.second)
  34. {}
  35. template<class T>
  36. constexpr interval(const interval<T> & rhs) :
  37. l(rhs.l),
  38. u(rhs.u)
  39. {}
  40. constexpr interval() :
  41. l(std::numeric_limits<R>::min()),
  42. u(std::numeric_limits<R>::max())
  43. {}
  44. // return true if this interval contains the given point
  45. constexpr tribool includes(const R & t) const {
  46. return l <= t && t <= u;
  47. }
  48. // if this interval contains every point found in some other inteval t
  49. // return true
  50. // otherwise
  51. // return false or indeterminate
  52. constexpr tribool includes(const interval<R> & t) const {
  53. return u >= t.u && l <= t.l;
  54. }
  55. // return true if this interval contains the given point
  56. constexpr tribool excludes(const R & t) const {
  57. return t < l || t > u;
  58. }
  59. // if this interval excludes every point found in some other inteval t
  60. // return true
  61. // otherwise
  62. // return false or indeterminate
  63. constexpr tribool excludes(const interval<R> & t) const {
  64. return t.u < l || u < t.l;
  65. }
  66. };
  67. template<class R>
  68. constexpr inline interval<R> make_interval(){
  69. return interval<R>();
  70. }
  71. template<class R>
  72. constexpr inline interval<R> make_interval(const R &){
  73. return interval<R>();
  74. }
  75. // account for the fact that for floats and doubles
  76. // the most negative value is called "lowest" rather
  77. // than min
  78. template<>
  79. constexpr inline interval<float>::interval() :
  80. l(std::numeric_limits<float>::lowest()),
  81. u(std::numeric_limits<float>::max())
  82. {}
  83. template<>
  84. constexpr inline interval<double>::interval() :
  85. l(std::numeric_limits<double>::lowest()),
  86. u(std::numeric_limits<double>::max())
  87. {}
  88. template<typename T>
  89. constexpr inline interval<T> operator+(const interval<T> & t, const interval<T> & u){
  90. // adapted from https://en.wikipedia.org/wiki/Interval_arithmetic
  91. return {t.l + u.l, t.u + u.u};
  92. }
  93. template<typename T>
  94. constexpr inline interval<T> operator-(const interval<T> & t, const interval<T> & u){
  95. // adapted from https://en.wikipedia.org/wiki/Interval_arithmetic
  96. return {t.l - u.u, t.u - u.l};
  97. }
  98. template<typename T>
  99. constexpr inline interval<T> operator*(const interval<T> & t, const interval<T> & u){
  100. // adapted from https://en.wikipedia.org/wiki/Interval_arithmetic
  101. return utility::minmax<T>(
  102. std::initializer_list<T> {
  103. t.l * u.l,
  104. t.l * u.u,
  105. t.u * u.l,
  106. t.u * u.u
  107. }
  108. );
  109. }
  110. // interval division
  111. // note: presumes 0 is not included in the range of the denominator
  112. template<typename T>
  113. constexpr inline interval<T> operator/(const interval<T> & t, const interval<T> & u){
  114. assert(static_cast<bool>(u.excludes(T(0))));
  115. return utility::minmax<T>(
  116. std::initializer_list<T> {
  117. t.l / u.l,
  118. t.l / u.u,
  119. t.u / u.l,
  120. t.u / u.u
  121. }
  122. );
  123. }
  124. // modulus of two intervals. This will give a new range of for the modulus.
  125. // note: presumes 0 is not included in the range of the denominator
  126. template<typename T>
  127. constexpr inline interval<T> operator%(const interval<T> & t, const interval<T> & u){
  128. assert(static_cast<bool>(u.excludes(T(0))));
  129. return utility::minmax<T>(
  130. std::initializer_list<T> {
  131. t.l % u.l,
  132. t.l % u.u,
  133. t.u % u.l,
  134. t.u % u.u
  135. }
  136. );
  137. }
  138. template<typename T>
  139. constexpr inline interval<T> operator<<(const interval<T> & t, const interval<T> & u){
  140. //return interval<T>{t.l << u.l, t.u << u.u};
  141. return utility::minmax<T>(
  142. std::initializer_list<T> {
  143. t.l << u.l,
  144. t.l << u.u,
  145. t.u << u.l,
  146. t.u << u.u
  147. }
  148. );
  149. }
  150. template<typename T>
  151. constexpr inline interval<T> operator>>(const interval<T> & t, const interval<T> & u){
  152. //return interval<T>{t.l >> u.u, t.u >> u.l};
  153. return utility::minmax<T>(
  154. std::initializer_list<T> {
  155. t.l >> u.l,
  156. t.l >> u.u,
  157. t.u >> u.l,
  158. t.u >> u.u
  159. }
  160. );
  161. }
  162. // union of two intervals
  163. template<typename T>
  164. constexpr interval<T> operator|(const interval<T> & t, const interval<T> & u){
  165. const T & rl = std::min(t.l, u.l);
  166. const T & ru = std::max(t.u, u.u);
  167. return interval<T>(rl, ru);
  168. }
  169. // intersection of two intervals
  170. template<typename T>
  171. constexpr inline interval<T> operator&(const interval<T> & t, const interval<T> & u){
  172. const T & rl = std::max(t.l, u.l);
  173. const T & ru = std::min(t.u, u.u);
  174. return interval<T>(rl, ru);
  175. }
  176. // determine whether two intervals intersect
  177. template<typename T>
  178. constexpr inline boost::logic::tribool intersect(const interval<T> & t, const interval<T> & u){
  179. return t.u >= u.l || t.l <= u.u;
  180. }
  181. template<typename T>
  182. constexpr inline boost::logic::tribool operator<(
  183. const interval<T> & t,
  184. const interval<T> & u
  185. ){
  186. return
  187. // if every element in t is less than every element in u
  188. t.u < u.l ? boost::logic::tribool(true):
  189. // if every element in t is greater than every element in u
  190. t.l > u.u ? boost::logic::tribool(false):
  191. // otherwise some element(s) in t are greater than some element in u
  192. boost::logic::indeterminate
  193. ;
  194. }
  195. template<typename T>
  196. constexpr inline boost::logic::tribool operator>(
  197. const interval<T> & t,
  198. const interval<T> & u
  199. ){
  200. return
  201. // if every element in t is greater than every element in u
  202. t.l > u.u ? boost::logic::tribool(true) :
  203. // if every element in t is less than every element in u
  204. t.u < u.l ? boost::logic::tribool(false) :
  205. // otherwise some element(s) in t are greater than some element in u
  206. boost::logic::indeterminate
  207. ;
  208. }
  209. template<typename T>
  210. constexpr inline bool operator==(
  211. const interval<T> & t,
  212. const interval<T> & u
  213. ){
  214. // intervals have the same limits
  215. return t.l == u.l && t.u == u.u;
  216. }
  217. template<typename T>
  218. constexpr inline bool operator!=(
  219. const interval<T> & t,
  220. const interval<T> & u
  221. ){
  222. return ! (t == u);
  223. }
  224. template<typename T>
  225. constexpr inline boost::logic::tribool operator<=(
  226. const interval<T> & t,
  227. const interval<T> & u
  228. ){
  229. return ! (t > u);
  230. }
  231. template<typename T>
  232. constexpr inline boost::logic::tribool operator>=(
  233. const interval<T> & t,
  234. const interval<T> & u
  235. ){
  236. return ! (t < u);
  237. }
  238. } // safe_numerics
  239. } // boost
  240. #include <iosfwd>
  241. namespace std {
  242. template<typename CharT, typename Traits, typename T>
  243. inline std::basic_ostream<CharT, Traits> &
  244. operator<<(
  245. std::basic_ostream<CharT, Traits> & os,
  246. const boost::safe_numerics::interval<T> & i
  247. ){
  248. return os << '[' << i.l << ',' << i.u << ']';
  249. }
  250. template<typename CharT, typename Traits>
  251. inline std::basic_ostream<CharT, Traits> &
  252. operator<<(
  253. std::basic_ostream<CharT, Traits> & os,
  254. const boost::safe_numerics::interval<unsigned char> & i
  255. ){
  256. os << "[" << (unsigned)i.l << "," << (unsigned)i.u << "]";
  257. return os;
  258. }
  259. template<typename CharT, typename Traits>
  260. inline std::basic_ostream<CharT, Traits> &
  261. operator<<(
  262. std::basic_ostream<CharT, Traits> & os,
  263. const boost::safe_numerics::interval<signed char> & i
  264. ){
  265. os << "[" << (int)i.l << "," << (int)i.u << "]";
  266. return os;
  267. }
  268. } // std
  269. #endif // BOOST_SAFE_NUMERICS_INTERVAL_HPP