mirror of
https://github.com/boostorg/safe_numerics.git
synced 2026-02-13 00:32:14 +00:00
69 lines
3.8 KiB
HTML
69 lines
3.8 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>Solution</title>
|
|
<link rel="stylesheet" href="../boostbook.css" type="text/css">
|
|
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
|
|
<link rel="home" href="../index.html" title="Safe Numerics">
|
|
<link rel="up" href="../introduction.html" title="Introduction">
|
|
<link rel="prev" href="problem.html" title="Problem">
|
|
<link rel="next" href="summary.html" title="Summary">
|
|
</head>
|
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
|
<table cellpadding="2" width="100%"><tr>
|
|
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="../pre-boost.jpg"></td>
|
|
<td align="center"><a href="../../../index.html">Home</a></td>
|
|
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
|
|
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
|
|
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
|
|
<td align="center"><a href="../../../more/index.htm">More</a></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="problem.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../introduction.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="summary.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.introduction.solution"></a>Solution</h3></div></div></div>
|
|
<p>This library implements special versions of int, unsigned, etc.
|
|
which behave exactly like the original ones EXCEPT that the results of
|
|
these operations are guaranteed to be either arithmetically correct or
|
|
invoke an error. Using this library, the above would be rendered
|
|
as:</p>
|
|
<pre class="programlisting">#include <boost/safe_numeric/safe_integer.hpp>
|
|
|
|
int f(safe<int> x, safe<int> y){
|
|
return x + y; // throw exception if correct result cannot be returned
|
|
}
|
|
</pre>
|
|
<p>The addition expression is checked at runtime or (if possible)
|
|
compile time to trap any possible errors resulting from incorrect
|
|
arithmetic behavior. This will permit one to write arithmetic expressions
|
|
that cannot produce an erroneous result. Instead, one and only one of the
|
|
following is guaranteed to occur.</p>
|
|
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
|
|
<li class="listitem"><p>the expression will emit a compilation error.</p></li>
|
|
<li class="listitem"><p>the expression will invoke a runtime exception.</p></li>
|
|
<li class="listitem"><p>the expression will yield the correct mathematical
|
|
result</p></li>
|
|
</ul></div>
|
|
<p>In addition to eliminating undefined behavior from
|
|
primitive integer types, we define new data types
|
|
<code class="computeroutput">safe_signed_range<MIN, MAX></code> and
|
|
<code class="computeroutput">safe_unsigned_range<MIN, MAX></code> which will throw an
|
|
exception if an attempt is made to store a result which is outside the
|
|
closed range [MIN, MAX].</p>
|
|
</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 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="problem.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../introduction.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="summary.html"><img src="../images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|