Files
safe_numerics/doc/html/tutorial/1.html
Robert Ramey 1bc0b94e65 changes to implement the following:
a) made trap_exception work
b) updated manual and examples to show how to use library to eliminate runtime penalty
c) added in safe_literal
d) made corrections of various types
2015-12-21 23:14:06 -08:00

94 lines
4.7 KiB
HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Arithmetic Expressions Can Yield Incorrect Results.</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="../tutorial.html" title="Tutorial and Motivating Examples">
<link rel="prev" href="../tutorial.html" title="Tutorial and Motivating Examples">
<link rel="next" href="2.html" title="Arithmetic Operations can Overflow Silently">
</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="../tutorial.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.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="2.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.tutorial.1"></a>Arithmetic Expressions Can Yield Incorrect Results.</h3></div></div></div>
<p>When some operation results in a result which exceeds the capacity
of a data variable to hold it, the result is undefined. This is called
"overflow". Since word size can differ between machines, code which
produces correct results in one set of circumstances may fail when
re-compiled on a machine with different hardware. When this occurs, Most
C++ compilers will continue to execute with no indication that the results
are wrong. It is the programmer's responsibility to ensure such undefined
behavior is avoided.</p>
<p>This program demonstrates this problem. The solution is to replace
instances of <code class="computeroutput">char</code> type with <code class="computeroutput">safe&lt;char&gt;</code>
type.</p>
<pre class="programlisting">#include &lt;cassert&gt;
#include &lt;exception&gt;
#include &lt;iostream&gt;
#include &lt;cstdint&gt;
#include "../include/safe_integer.hpp"
int main(int argc, const char * argv[]){
std::cout &lt;&lt; "example 1:";
std::cout &lt;&lt; "undetected erroneous expression evaluation" &lt;&lt; std::endl;
std::cout &lt;&lt; "Not using safe numerics" &lt;&lt; std::endl;
// problem: arithmetic operations can yield incorrect results.
try{
std::int8_t x = 127;
std::int8_t y = 2;
std::int8_t z;
// this produces an invalid result !
z = x + y;
// but assert fails to detect it since C++ implicitly
// converts variables to int before evaluating he expression!
// assert(z == x + y);
std::cout &lt;&lt; static_cast&lt;int&gt;(z) &lt;&lt; " != " &lt;&lt; x + y &lt;&lt; std::endl;
std::cout &lt;&lt; "error NOT detected!" &lt;&lt; std::endl;
}
catch(std::exception){
std::cout &lt;&lt; "error detected!" &lt;&lt; std::endl;
}
// solution: replace std::int8_t with safe&lt;std::int8_t&gt;
std::cout &lt;&lt; "Using safe numerics" &lt;&lt; std::endl;
try{
using namespace boost::numeric;
safe&lt;std::int8_t&gt; x = 127;
safe&lt;std::int8_t&gt; y = 2;
safe&lt;std::int8_t&gt; z;
// rather than producing and invalid result an exception is thrown
z = x + y;
}
catch(std::exception &amp; e){
// which can catch here
std::cout &lt;&lt; e.what() &lt;&lt; std::endl;
}
return 0;
}
</pre>
<p>Note that I've used <code class="computeroutput">char</code> types in this example to make
the problem and solution easier to see. The exact same example could have
been done with <code class="computeroutput">int</code> types albeit with different values.</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 &#169; 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="../tutorial.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.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="2.html"><img src="../images/next.png" alt="Next"></a>
</div>
</body>
</html>