mirror of
https://github.com/boostorg/safe_numerics.git
synced 2026-02-09 23:32:37 +00:00
100 lines
4.2 KiB
HTML
100 lines
4.2 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>Mixing Data Types Can Create Subtle Errors</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="4.html" title="Implicit Conversions Change Data Values">
|
|
<link rel="next" href="5.html" title="Array Index Value Can Exceed Array Limits">
|
|
</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="4.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="5.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.10"></a>Mixing Data Types Can Create Subtle Errors</h3></div></div></div>
|
|
<p>C++ contains signed and unsigned integer types. In spite of their
|
|
names, they function differently which often produces surprising results
|
|
for some operands. Program errors from this behavior can be exceedingly
|
|
difficult to find. This has lead to recommendations of various ad hoc
|
|
"rules" to avoid these problems. It's not always easy to apply these
|
|
"rules" to existing code without creating even more bugs. Here is a
|
|
typical example of this problem:</p>
|
|
<pre class="programlisting">#include <iostream>
|
|
#include <cstdint>
|
|
|
|
#include "../include/safe_integer.hpp"
|
|
#include "../include/cpp.hpp"
|
|
|
|
using namespace std;
|
|
using namespace boost::numeric;
|
|
|
|
void f(const unsigned int & x, const int8_t & y){
|
|
cout << x * y << endl;
|
|
}
|
|
void safe_f(
|
|
const safe<unsigned int> & x,
|
|
const safe<int8_t> & y
|
|
){
|
|
cout << x * y << endl;
|
|
}
|
|
|
|
int main(){
|
|
cout << "example 10: ";
|
|
cout << "mixing types produces surprising results" << endl;
|
|
try {
|
|
std::cout << "Not using safe numerics" << std::endl;
|
|
// problem: arithmetic operations can yield incorrect results.
|
|
f(100, 100); // works as expected
|
|
f(100, -100); // wrong result - unnoticed
|
|
}
|
|
catch(std::exception){
|
|
// never arrive here
|
|
std::cout << "error detected!" << std::endl;
|
|
}
|
|
try {
|
|
// solution: use safe types
|
|
std::cout << "Using safe numerics" << std::endl;
|
|
safe_f(100, 100); // works as expected
|
|
safe_f(100, -100); // throw error
|
|
}
|
|
catch(const exception & e){
|
|
cout << "detected error:" << e.what() << endl;;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
</pre>
|
|
<p>Here
|
|
is the output of the above program:</p>
|
|
<pre class="programlisting">example 10: mixing types produces surprising results
|
|
Not using safe numerics
|
|
10000
|
|
4294957296
|
|
Using safe numerics
|
|
10000
|
|
detected error:converted negative value to unsigned
|
|
</pre>
|
|
<p>This solution is simple, Just replace instances of the
|
|
<code class="computeroutput">int</code>with <code class="computeroutput">safe<int></code>.</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="4.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="5.html"><img src="../images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|