mirror of
https://github.com/boostorg/safe_numerics.git
synced 2026-02-13 00:32:14 +00:00
79 lines
4.3 KiB
HTML
79 lines
4.3 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>Problem</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="../introduction.html" title="Introduction">
|
|
<link rel="next" href="solution.html" title="Solution">
|
|
</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="../introduction.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="solution.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.problem"></a>Problem</h3></div></div></div>
|
|
<p>Arithmetic operations in C++ are NOT guaranteed to yield a correct
|
|
mathematical result. This feature is inherited from the early days of C.
|
|
The behavior of <code class="computeroutput">int</code>, <code class="computeroutput">unsigned int</code> and others
|
|
were designed to map closely to the underlying hardware. Computer hardware
|
|
implements these types as a fixed number of bits. When the result of
|
|
arithmetic operations exceeds this number of bits, the result will not be
|
|
arithmetically correct. The following example illustrates this
|
|
problem.</p>
|
|
<pre class="programlisting">int f(int x, int y){
|
|
// this returns an invalid result for some legal values of x and y !
|
|
return x + y;
|
|
}
|
|
</pre>
|
|
<p>It is incumbent up the C/C++ programmer to guarantee that this
|
|
behavior does not result in incorrect or unexpected operation of the
|
|
program. There are no language facilities which do this. They have to be
|
|
explicitly addressed in the program code. There are a number of ways to do
|
|
this. See[<a class="citation" href="bibliography.html#idm354480662352"><span class="citation">INT32-C</span></a>] seems to recommend the following
|
|
approach.</p>
|
|
<pre class="programlisting">int f(int x, int y){
|
|
if (((y > 0) && (x > (INT_MAX - y)))
|
|
|| ((y < 0) && (x < (INT_MIN - x)))) {
|
|
/* Handle error */
|
|
}
|
|
return x + y;
|
|
}
|
|
</pre>
|
|
<p>This will indeed trap the error. However, it would be tedious and
|
|
laborious for a programmer to do alter his code to do. Altering code in
|
|
this way for all arithmetic operations would likely render the code
|
|
unreadable and add another source of potential programming errors. This
|
|
approach is clearly not functional when the expression is even a little
|
|
more complex as is shown in the following example.</p>
|
|
<pre class="programlisting">int f(int x, int y, int z){
|
|
// this returns an invalid result for some legal values of x and y !
|
|
return x + y * z;
|
|
}
|
|
</pre>
|
|
</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="../introduction.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="solution.html"><img src="../images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|