165 lines
20 KiB
HTML
165 lines
20 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>Checked Arithmetic</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_result.html" title="checked_result<R>">
|
|
<link rel="next" href="interval.html" title="interval<R>">
|
|
</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_result.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="interval.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.checked_arithmetic"></a>Checked Arithmetic</h3></div></div></div>
|
|
<div class="toc"><dl class="toc">
|
|
<dt><span class="section"><a href="checked_arithmetic.html#idm130202409440">Description</a></span></dt>
|
|
<dt><span class="section"><a href="checked_arithmetic.html#idm130202408112">Type requirements</a></span></dt>
|
|
<dt><span class="section"><a href="checked_arithmetic.html#idm130202406112">Complexity</a></span></dt>
|
|
<dt><span class="section"><a href="checked_arithmetic.html#idm130202404960">Example of use</a></span></dt>
|
|
<dt><span class="section"><a href="checked_arithmetic.html#idm130202387696">Notes</a></span></dt>
|
|
<dt><span class="section"><a href="checked_arithmetic.html#idm130202386128">Synopsis</a></span></dt>
|
|
<dt><span class="section"><a href="checked_arithmetic.html#idm130202157712">See Also</a></span></dt>
|
|
<dt><span class="section"><a href="checked_arithmetic.html#idm130202155984">Header</a></span></dt>
|
|
</dl></div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm130202409440"></a>Description</h4></div></div></div>
|
|
<p>Perform binary operations on arithmetic types. Return either a valid
|
|
result or an error code. Under no circumstances should an incorrect result
|
|
be returned.</p>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm130202408112"></a>Type requirements</h4></div></div></div>
|
|
<p>All template parameters of the functions must model <a class="link" href="integer.html" title="Integer<T>">Numeric</a> type requirements.</p>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm130202406112"></a>Complexity</h4></div></div></div>
|
|
<p>Each function performs one and only one arithmetic operation.</p>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm130202404960"></a>Example of use</h4></div></div></div>
|
|
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</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">checked_default</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
|
|
|
<span class="identifier">checked_result</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="identifier">r</span> <span class="special">=</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">multiply</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span><span class="special">(</span><span class="number">24</span><span class="special">,</span> <span class="number">42</span><span class="special">)</span><span class="special">;</span>
|
|
</pre>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm130202387696"></a>Notes</h4></div></div></div>
|
|
<p>Some compilers have command line switches (e.g. -ftrapv) which
|
|
enable special behavior such that erroneous integer operations are
|
|
detected at run time. The library has been implemented in such a way that
|
|
these facilities are not used. It's possible they might be helpful in
|
|
particular environment. These could be be exploited by re-implementing
|
|
some functions in this library.</p>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm130202386128"></a>Synopsis</h4></div></div></div>
|
|
<pre class="programlisting"><span class="comment">// safe casting on primitive types</span>
|
|
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span>
|
|
<span class="identifier">checked_result</span><span class="special"><</span><span class="identifier">R</span><span class="special">></span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">cast</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">T</span> <span class="special">&</span> <span class="identifier">t</span><span class="special">)</span><span class="special">;</span>
|
|
|
|
<span class="comment">// safe addition on primitive types</span>
|
|
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span>
|
|
<span class="identifier">checked_result</span><span class="special"><</span><span class="identifier">R</span><span class="special">></span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">add</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>
|
|
|
|
<span class="comment">// safe subtraction on primitive types</span>
|
|
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span>
|
|
<span class="identifier">checked_result</span><span class="special"><</span><span class="identifier">R</span><span class="special">></span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">subtract</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>
|
|
|
|
<span class="comment">// safe multiplication on primitive types</span>
|
|
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span>
|
|
<span class="identifier">checked_result</span><span class="special"><</span><span class="identifier">R</span><span class="special">></span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">multiply</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>
|
|
|
|
<span class="comment">// safe division on primitive types</span>
|
|
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span>
|
|
<span class="identifier">checked_result</span><span class="special"><</span><span class="identifier">R</span><span class="special">></span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">divide</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>
|
|
|
|
<span class="comment">// safe modulus on primitive types</span>
|
|
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span>
|
|
<span class="identifier">checked_result</span><span class="special"><</span><span class="identifier">R</span><span class="special">></span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">modulus</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>
|
|
|
|
<span class="comment">// safe less than predicate on primitive types</span>
|
|
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span>
|
|
<span class="keyword">bool</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">less_than</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>
|
|
|
|
<span class="comment">// safe greater_than_equal predicate on primitive types</span>
|
|
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span>
|
|
<span class="keyword">bool</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">greater_than_equal</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>
|
|
|
|
<span class="comment">// safe greater_than predicate on primitive types</span>
|
|
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span>
|
|
<span class="keyword">bool</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">greater_than</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>
|
|
|
|
<span class="comment">// safe less_than_equal predicate on primitive types</span>
|
|
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span>
|
|
<span class="keyword">bool</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">less_than_equal</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>
|
|
|
|
<span class="comment">// safe equal predicate on primitive types</span>
|
|
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span>
|
|
<span class="keyword">bool</span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">equal</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>
|
|
|
|
<span class="comment">// left shift</span>
|
|
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span>
|
|
<span class="identifier">checked_result</span><span class="special"><</span><span class="identifier">R</span><span class="special">></span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">left_shift</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>
|
|
|
|
<span class="comment">// right shift</span>
|
|
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span>
|
|
<span class="identifier">checked_result</span><span class="special"><</span><span class="identifier">R</span><span class="special">></span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">right_shift</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>
|
|
|
|
<span class="comment">// bitwise operations</span>
|
|
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span>
|
|
<span class="identifier">checked_result</span><span class="special"><</span><span class="identifier">R</span><span class="special">></span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">bitwise_or</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>
|
|
|
|
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span>
|
|
<span class="identifier">checked_result</span><span class="special"><</span><span class="identifier">R</span><span class="special">></span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">bitwise_and</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>
|
|
|
|
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">R</span><span class="special">></span>
|
|
<span class="identifier">checked_result</span><span class="special"><</span><span class="identifier">R</span><span class="special">></span> <span class="keyword">constexpr</span> <span class="identifier">checked</span><span class="special">::</span><span class="identifier">bitwise_xor</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">t</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">R</span> <span class="special">&</span> <span class="identifier">u</span><span class="special">)</span><span class="special">;</span>
|
|
</pre>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm130202157712"></a>See Also</h4></div></div></div>
|
|
<p><a class="link" href="checked_result.html" title="checked_result<R>">checked_result<R></a></p>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="idm130202155984"></a>Header</h4></div></div></div>
|
|
<p><a href="../../include/checked.hpp" target="_top"><code class="computeroutput">#include
|
|
<boost/numeric/safe_numerics/checked_default.hpp>
|
|
</code></a></p>
|
|
<p><a href="../../include/checked_integer.hpp" target="_top"><code class="computeroutput">#include
|
|
<boost/numeric/safe_numerics/checked_integer.hpp>
|
|
</code></a></p>
|
|
<p><a href="../../include/checked.hpp" target="_top"><code class="computeroutput"></code></a><a href="../../include/checked_float.hpp" target="_top"><code class="computeroutput">#include
|
|
<boost/numeric/safe_numerics/checked_float.hpp>
|
|
</code></a></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 © 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_result.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="interval.html"><img src="images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|