interval.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
  3. "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
  4. <section id="safe_numerics.interval">
  5. <title>interval&lt;R&gt;</title>
  6. <?dbhtml stop-chunking?>
  7. <section>
  8. <title>Description</title>
  9. <para>A closed arithmetic interval represented by a pair of elements of
  10. type R. In principle, one should be able to use Boost.Interval library for
  11. this. But the functions in this library are not <code>constexpr</code>.
  12. Also, this Boost.Interval is more complex and does not support certain
  13. operations such bit operations. Perhaps some time in the future,
  14. Boost.Interval will be used instead of this <code>interval&lt;R&gt;</code>
  15. type.</para>
  16. </section>
  17. <section>
  18. <title>Template Parameters</title>
  19. <para>R must model the type requirements of <link
  20. linkend="safe_numerics.numeric">Numeric</link>. Note this in principle
  21. includes any numeric type including floating point numbers and instances
  22. of <link
  23. linkend="safenumerics.checked_result"><code>checked_result&lt;R&gt;</code></link>.</para>
  24. </section>
  25. <section>
  26. <title>Notation</title>
  27. <informaltable>
  28. <tgroup cols="2">
  29. <colspec align="left" colwidth="1*"/>
  30. <colspec align="left" colwidth="4*"/>
  31. <thead>
  32. <row>
  33. <entry align="left">Symbol</entry>
  34. <entry align="left">Description</entry>
  35. </row>
  36. </thead>
  37. <tbody>
  38. <row>
  39. <entry><code>I</code></entry>
  40. <entry>An interval type</entry>
  41. </row>
  42. <row>
  43. <entry><code>i, j</code></entry>
  44. <entry>An instance of interval type</entry>
  45. </row>
  46. <row>
  47. <entry><code>R</code></entry>
  48. <entry>Numeric types which can be used to make an interval</entry>
  49. </row>
  50. <row>
  51. <entry><code>r</code></entry>
  52. <entry>An instance of type R</entry>
  53. </row>
  54. <row>
  55. <entry><code>p</code></entry>
  56. <entry>An instance of std::pair&lt;R, R&gt;</entry>
  57. </row>
  58. <row>
  59. <entry><code>l, u</code></entry>
  60. <entry>Lowermost and uppermost values in an interval</entry>
  61. </row>
  62. <row>
  63. <entry><code>os</code></entry>
  64. <entry>std::basic_ostream&lt;class CharT, class Traits =
  65. std::char_traits&lt;CharT&gt;&gt;</entry>
  66. </row>
  67. </tbody>
  68. </tgroup>
  69. </informaltable>
  70. </section>
  71. <section>
  72. <title>Associated Types</title>
  73. <informaltable>
  74. <tgroup cols="2">
  75. <colspec align="left" colwidth="1*"/>
  76. <colspec align="left" colwidth="4*"/>
  77. <tbody>
  78. <row>
  79. <entry><link
  80. linkend="safenumerics.checked_result"><code>checked_result</code></link></entry>
  81. <entry>holds either the result of an operation or information as
  82. to why it failed</entry>
  83. </row>
  84. </tbody>
  85. </tgroup>
  86. </informaltable>
  87. </section>
  88. <section>
  89. <title>Valid Expressions</title>
  90. <para>Note that all expressions are constexpr.</para>
  91. <para><informaltable>
  92. <tgroup cols="3">
  93. <colspec align="left" colwidth="1*"/>
  94. <colspec align="left" colwidth="1*"/>
  95. <colspec align="left" colwidth="3*"/>
  96. <thead>
  97. <row>
  98. <entry align="left">Expression</entry>
  99. <entry>Return Type</entry>
  100. <entry>Semantics</entry>
  101. </row>
  102. </thead>
  103. <tbody>
  104. <row>
  105. <entry><code>interval&lt;R&gt;(l, u)</code></entry>
  106. <entry><code>interval&lt;R&gt;</code></entry>
  107. <entry>construct a new interval from a pair of limits</entry>
  108. </row>
  109. <row>
  110. <entry><code>interval&lt;R&gt;(p)</code></entry>
  111. <entry><code>interval&lt;R&gt;</code></entry>
  112. <entry>construct a new interval from a pair of limits</entry>
  113. </row>
  114. <row>
  115. <entry><code>interval&lt;R&gt;(i)</code></entry>
  116. <entry><code>interval&lt;R&gt;</code></entry>
  117. <entry>copy constructor</entry>
  118. </row>
  119. <row>
  120. <entry><code>make_interval&lt;R&gt;()</code></entry>
  121. <entry><code>interval&lt;R&gt;</code></entry>
  122. <entry>return new interval with
  123. std::numric_limits&lt;R&gt;::min() and
  124. std::numric_limits&lt;R&gt;::max()</entry>
  125. </row>
  126. <row>
  127. <entry><code>make_interval&lt;R&gt;(const R
  128. &amp;r)</code></entry>
  129. <entry><code>interval&lt;R&gt;</code></entry>
  130. <entry>return new interval with
  131. std::numric_limits&lt;R&gt;::min() and
  132. std::numric_limits&lt;R&gt;::max()</entry>
  133. </row>
  134. <row>
  135. <entry><code>i.l</code></entry>
  136. <entry><code>R</code></entry>
  137. <entry>lowermost value in the interval i</entry>
  138. </row>
  139. <row>
  140. <entry><code>i.u</code></entry>
  141. <entry><code>R</code></entry>
  142. <entry>uppermost value in the interval i</entry>
  143. </row>
  144. <row>
  145. <entry><code>i.includes(j)</code></entry>
  146. <entry><code>boost::logic::tribool</code></entry>
  147. <entry>return true if interval i includes interval j</entry>
  148. </row>
  149. <row>
  150. <entry><code>i.excludes(j)</code></entry>
  151. <entry><code>boost::logic::tribool</code></entry>
  152. <entry>return true if interval i includes interval j</entry>
  153. </row>
  154. <row>
  155. <entry><code>i.includes(t)</code></entry>
  156. <entry><code>bool</code></entry>
  157. <entry>return true if interval i includes value t</entry>
  158. </row>
  159. <row>
  160. <entry><code>i.excludes(t)</code></entry>
  161. <entry><code>bool</code></entry>
  162. <entry>return true if interval i includes value t</entry>
  163. </row>
  164. <row>
  165. <entry><code>i + j</code></entry>
  166. <entry><code>interval&lt;R&gt;</code></entry>
  167. <entry>add two intervals and return the result</entry>
  168. </row>
  169. <row>
  170. <entry><code>i - j</code></entry>
  171. <entry><code>interval&lt;R&gt;</code></entry>
  172. <entry>subtract two intervals and return the result</entry>
  173. </row>
  174. <row>
  175. <entry><code>i * j</code></entry>
  176. <entry><code>interval&lt;R&gt;</code></entry>
  177. <entry>multiply two intervals and return the result</entry>
  178. </row>
  179. <row>
  180. <entry><code>i / j</code></entry>
  181. <entry><code>interval&lt;R&gt;</code></entry>
  182. <entry>divide one interval by another and return the
  183. result</entry>
  184. </row>
  185. <row>
  186. <entry><code>i % j</code></entry>
  187. <entry><code>interval&lt;R&gt;</code></entry>
  188. <entry>calculate modulus of one interval by another and return
  189. the result</entry>
  190. </row>
  191. <row>
  192. <entry><code>i &lt;&lt; j</code></entry>
  193. <entry><code>interval&lt;R&gt;</code></entry>
  194. <entry>calculate the range that would result from shifting one
  195. interval by another</entry>
  196. </row>
  197. <row>
  198. <entry><code>i &gt;&gt; j</code></entry>
  199. <entry><code>interval&lt;R&gt;</code></entry>
  200. <entry>calculate the range that would result from shifting one
  201. interval by another</entry>
  202. </row>
  203. <row>
  204. <entry><code>i | j</code></entry>
  205. <entry><code>interval&lt;R&gt;</code></entry>
  206. <entry>range of values which can result from applying | to any
  207. pair of operands from I and j</entry>
  208. </row>
  209. <row>
  210. <entry><code>i &amp; j</code></entry>
  211. <entry><code>interval&lt;R&gt;</code></entry>
  212. <entry>range of values which can result from applying &amp; to
  213. any pair of operands from I and j</entry>
  214. </row>
  215. <row>
  216. <entry><code>i ^ j</code></entry>
  217. <entry><code>interval&lt;R&gt;</code></entry>
  218. <entry>range of values which can result from applying ^ to any
  219. pair of operands from I and j</entry>
  220. </row>
  221. <row>
  222. <entry><code>t &lt; u</code></entry>
  223. <entry><code>boost::logic::tribool</code></entry>
  224. <entry>true if every element in t is less than every element in
  225. u</entry>
  226. </row>
  227. <row>
  228. <entry><code>t &gt; u</code></entry>
  229. <entry><code>boost::logic::tribool</code></entry>
  230. <entry>true if every element in t is greater than every element
  231. in u</entry>
  232. </row>
  233. <row>
  234. <entry><code>t &lt;= u</code></entry>
  235. <entry><code>boost::logic::tribool</code></entry>
  236. <entry>true if every element in t is less than or equal to every
  237. element in u</entry>
  238. </row>
  239. <row>
  240. <entry><code>t &gt;= u</code></entry>
  241. <entry><code>boost::logic::tribool</code></entry>
  242. <entry>true if every element in t is greater than or equal to
  243. every element in u</entry>
  244. </row>
  245. <row>
  246. <entry><code>t == u</code></entry>
  247. <entry><code>bool</code></entry>
  248. <entry>true if limits are equal</entry>
  249. </row>
  250. <row>
  251. <entry><code>t != u</code></entry>
  252. <entry><code>bool</code></entry>
  253. <entry>true if limits are not equal</entry>
  254. </row>
  255. <row>
  256. <entry><code>os &lt;&lt; i</code></entry>
  257. <entry><code>os &amp;</code></entry>
  258. <entry>print interval to output stream</entry>
  259. </row>
  260. </tbody>
  261. </tgroup>
  262. </informaltable></para>
  263. </section>
  264. <section>
  265. <title>Example of use</title>
  266. <programlisting>#include &lt;iostream&gt;
  267. #include &lt;cstdint&gt;
  268. #include &lt;cassert&gt;
  269. #include &lt;boost/numeric/safe_numerics/interval.hpp&gt;
  270. int main(){
  271. std::cout &lt;&lt; "test1" &lt;&lt; std::endl;
  272. interval&lt;std::int16_t&gt; x = {-64, 63};
  273. std::cout &lt;&lt; "x = " &lt;&lt; x &lt;&lt; std::endl;
  274. interval&lt;std::int16_t&gt; y(-128, 126);
  275. std::cout &lt;&lt; "y = " &lt;&lt; y &lt;&lt; std::endl;
  276. assert(static_cast&lt;interval&lt;std::int16_t&gt;&gt;(add&lt;std::int16_t&gt;(x,x)) == y);
  277. std::cout &lt;&lt; "x + x =" &lt;&lt; add&lt;std::int16_t&gt;(x, x) &lt;&lt; std::endl;
  278. std::cout &lt;&lt; "x - x = " &lt;&lt; subtract&lt;std::int16_t&gt;(x, x) &lt;&lt; std::endl;
  279. return 0;
  280. }</programlisting>
  281. </section>
  282. <section>
  283. <title>Header</title>
  284. <para><ulink
  285. url="../../include/boost/safe_numerics/interval.hpp"><code>#include
  286. &lt;boost/numeric/safe_numerics/interval.hpp&gt;</code></ulink></para>
  287. </section>
  288. </section>