safe_numerics/doc/html/interval.html

324 lines
19 KiB
HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>interval&lt;R&gt;</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="library_implementation.html" title="Library Implementation">
<link rel="prev" href="checked_arithmetic.html" title="Checked Arithmetic">
<link rel="next" href="checked_integer_arithmetic.html" title="safe_compare&lt;T, U&gt;">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img href="index.html" height="164px" src="pre-boost.jpg" alt="Library Documentation Index"></td>
<td><h2>Safe Numerics</h2></td>
</tr></table>
<div class="spirit-nav">
<a accesskey="p" href="checked_arithmetic.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="library_implementation.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="checked_integer_arithmetic.html"><img src="images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="safe_numerics.interval"></a>interval&lt;R&gt;</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="interval.html#idm130202132352">Description</a></span></dt>
<dt><span class="section"><a href="interval.html#idm130202129408">Template Parameters</a></span></dt>
<dt><span class="section"><a href="interval.html#idm130202126048">Notation</a></span></dt>
<dt><span class="section"><a href="interval.html#idm130202108592">Associated Types</a></span></dt>
<dt><span class="section"><a href="interval.html#idm130202102752">Valid Expressions</a></span></dt>
<dt><span class="section"><a href="interval.html#idm130202023104">Example of use</a></span></dt>
<dt><span class="section"><a href="interval.html#idm130201930272">Header</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm130202132352"></a>Description</h4></div></div></div>
<p>A closed arithmetic interval represented by a pair of elements of
type R. In principle, one should be able to use Boost.Interval library for
this. But the functions in this library are not <code class="computeroutput">constexpr</code>.
Also, this Boost.Interval is more complex and does not support certain
operations such bit operations. Perhaps some time in the future,
Boost.Interval will be used instead of this <code class="computeroutput">interval&lt;R&gt;</code>
type.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm130202129408"></a>Template Parameters</h4></div></div></div>
<p>R must model the type requirements of <a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Numeric</a>. Note this in principle
includes any numeric type including floating point numbers and instances
of <a class="link" href="checked_result.html" title="checked_result&lt;R&gt;"><code class="computeroutput">checked_result&lt;R&gt;</code></a>.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm130202126048"></a>Notation</h4></div></div></div>
<div class="informaltable"><table class="table">
<colgroup>
<col align="left">
<col align="left">
</colgroup>
<thead><tr>
<th align="left">Symbol</th>
<th align="left">Description</th>
</tr></thead>
<tbody>
<tr>
<td align="left"><code class="computeroutput">I</code></td>
<td align="left">An interval type</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">i, j</code></td>
<td align="left">An instance of interval type</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">R</code></td>
<td align="left">Numeric types which can be used to make an interval</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">r</code></td>
<td align="left">An instance of type R</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">p</code></td>
<td align="left">An instance of std::pair&lt;R, R&gt;</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">l, u</code></td>
<td align="left">Lowermost and uppermost values in an interval</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">os</code></td>
<td align="left">std::basic_ostream&lt;class CharT, class Traits =
std::char_traits&lt;CharT&gt;&gt;</td>
</tr>
</tbody>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm130202108592"></a>Associated Types</h4></div></div></div>
<div class="informaltable"><table class="table">
<colgroup>
<col align="left">
<col align="left">
</colgroup>
<tbody><tr>
<td align="left"><a class="link" href="checked_result.html" title="checked_result&lt;R&gt;"><code class="computeroutput">checked_result</code></a></td>
<td align="left">holds either the result of an operation or information as
to why it failed</td>
</tr></tbody>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm130202102752"></a>Valid Expressions</h4></div></div></div>
<p>Note that all expressions are constexpr.</p>
<div class="informaltable"><table class="table">
<colgroup>
<col align="left">
<col align="left">
<col align="left">
</colgroup>
<thead><tr>
<th align="left">Expression</th>
<th align="left">Return Type</th>
<th align="left">Semantics</th>
</tr></thead>
<tbody>
<tr>
<td align="left"><code class="computeroutput">interval&lt;R&gt;(l, u)</code></td>
<td align="left"><code class="computeroutput">interval&lt;R&gt;</code></td>
<td align="left">construct a new interval from a pair of limits</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">interval&lt;R&gt;(p)</code></td>
<td align="left"><code class="computeroutput">interval&lt;R&gt;</code></td>
<td align="left">construct a new interval from a pair of limits</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">interval&lt;R&gt;(i)</code></td>
<td align="left"><code class="computeroutput">interval&lt;R&gt;</code></td>
<td align="left">copy constructor</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">make_interval&lt;R&gt;()</code></td>
<td align="left"><code class="computeroutput">interval&lt;R&gt;</code></td>
<td align="left">return new interval with
std::numric_limits&lt;R&gt;::min() and
std::numric_limits&lt;R&gt;::max()</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">make_interval&lt;R&gt;(const R
&amp;r)</code></td>
<td align="left"><code class="computeroutput">interval&lt;R&gt;</code></td>
<td align="left">return new interval with
std::numric_limits&lt;R&gt;::min() and
std::numric_limits&lt;R&gt;::max()</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">i.l</code></td>
<td align="left"><code class="computeroutput">R</code></td>
<td align="left">lowermost value in the interval i</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">i.u</code></td>
<td align="left"><code class="computeroutput">R</code></td>
<td align="left">uppermost value in the interval i</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">i.includes(j)</code></td>
<td align="left"><code class="computeroutput">boost::logic::tribool</code></td>
<td align="left">return true if interval i includes interval j</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">i.excludes(j)</code></td>
<td align="left"><code class="computeroutput">boost::logic::tribool</code></td>
<td align="left">return true if interval i includes interval j</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">i.includes(t)</code></td>
<td align="left"><code class="computeroutput">bool</code></td>
<td align="left">return true if interval i includes value t</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">i.excludes(t)</code></td>
<td align="left"><code class="computeroutput">bool</code></td>
<td align="left">return true if interval i includes value t</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">i + j</code></td>
<td align="left"><code class="computeroutput">interval&lt;R&gt;</code></td>
<td align="left">add two intervals and return the result</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">i - j</code></td>
<td align="left"><code class="computeroutput">interval&lt;R&gt;</code></td>
<td align="left">subtract two intervals and return the result</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">i * j</code></td>
<td align="left"><code class="computeroutput">interval&lt;R&gt;</code></td>
<td align="left">multiply two intervals and return the result</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">i / j</code></td>
<td align="left"><code class="computeroutput">interval&lt;R&gt;</code></td>
<td align="left">divide one interval by another and return the
result</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">i % j</code></td>
<td align="left"><code class="computeroutput">interval&lt;R&gt;</code></td>
<td align="left">calculate modulus of one interval by another and return
the result</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">i &lt;&lt; j</code></td>
<td align="left"><code class="computeroutput">interval&lt;R&gt;</code></td>
<td align="left">calculate the range that would result from shifting one
interval by another</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">i &gt;&gt; j</code></td>
<td align="left"><code class="computeroutput">interval&lt;R&gt;</code></td>
<td align="left">calculate the range that would result from shifting one
interval by another</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">i | j</code></td>
<td align="left"><code class="computeroutput">interval&lt;R&gt;</code></td>
<td align="left">range of values which can result from applying | to any
pair of operands from I and j</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">i &amp; j</code></td>
<td align="left"><code class="computeroutput">interval&lt;R&gt;</code></td>
<td align="left">range of values which can result from applying &amp; to
any pair of operands from I and j</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">i ^ j</code></td>
<td align="left"><code class="computeroutput">interval&lt;R&gt;</code></td>
<td align="left">range of values which can result from applying ^ to any
pair of operands from I and j</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t &lt; u</code></td>
<td align="left"><code class="computeroutput">boost::logic::tribool</code></td>
<td align="left">true if every element in t is less than every element in
u</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t &gt; u</code></td>
<td align="left"><code class="computeroutput">boost::logic::tribool</code></td>
<td align="left">true if every element in t is greater than every element
in u</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t &lt;= u</code></td>
<td align="left"><code class="computeroutput">boost::logic::tribool</code></td>
<td align="left">true if every element in t is less than or equal to every
element in u</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t &gt;= u</code></td>
<td align="left"><code class="computeroutput">boost::logic::tribool</code></td>
<td align="left">true if every element in t is greater than or equal to
every element in u</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t == u</code></td>
<td align="left"><code class="computeroutput">bool</code></td>
<td align="left">true if limits are equal</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t != u</code></td>
<td align="left"><code class="computeroutput">bool</code></td>
<td align="left">true if limits are not equal</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">os &lt;&lt; i</code></td>
<td align="left"><code class="computeroutput">os &amp;</code></td>
<td align="left">print interval to output stream</td>
</tr>
</tbody>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm130202023104"></a>Example of use</h4></div></div></div>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cstdint</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cassert</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">numeric</span><span class="special">/</span><span class="identifier">safe_numerics</span><span class="special">/</span><span class="identifier">interval</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="special">)</span><span class="special">{</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"test1"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="identifier">interval</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">int16_t</span><span class="special">&gt;</span> <span class="identifier">x</span> <span class="special">=</span> <span class="special">{</span><span class="special">-</span><span class="number">64</span><span class="special">,</span> <span class="number">63</span><span class="special">}</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"x = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">x</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="identifier">interval</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">int16_t</span><span class="special">&gt;</span> <span class="identifier">y</span><span class="special">(</span><span class="special">-</span><span class="number">128</span><span class="special">,</span> <span class="number">126</span><span class="special">)</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"y = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">y</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="identifier">assert</span><span class="special">(</span><span class="keyword">static_cast</span><span class="special">&lt;</span><span class="identifier">interval</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">int16_t</span><span class="special">&gt;&gt;</span><span class="special">(</span><span class="identifier">add</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">int16_t</span><span class="special">&gt;</span><span class="special">(</span><span class="identifier">x</span><span class="special">,</span><span class="identifier">x</span><span class="special">)</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">y</span><span class="special">)</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"x + x ="</span> <span class="special">&lt;&lt;</span> <span class="identifier">add</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">int16_t</span><span class="special">&gt;</span><span class="special">(</span><span class="identifier">x</span><span class="special">,</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"x - x = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">subtract</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">int16_t</span><span class="special">&gt;</span><span class="special">(</span><span class="identifier">x</span><span class="special">,</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span></pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="idm130201930272"></a>Header</h4></div></div></div>
<p><code class="computeroutput">#include
&lt;boost/numeric/safe_numerics/interval.hpp&gt;</code></p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2012-2018 Robert Ramey<p><a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">Subject to Boost
Software License</a></p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="checked_arithmetic.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="library_implementation.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="checked_integer_arithmetic.html"><img src="images/next.png" alt="Next"></a>
</div>
</body>
</html>