mirror of
https://github.com/boostorg/safe_numerics.git
synced 2026-02-09 23:32:37 +00:00
84 lines
3.6 KiB
HTML
84 lines
3.6 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>Undetected underflow</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="2.html" title="Undetected overflow">
|
|
<link rel="next" href="4.html" title="Implicit conversions change data values">
|
|
</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="2.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="4.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.3"></a>Undetected underflow</h3></div></div></div>
|
|
<p>A variation of the above is when a value is incremented/decremented
|
|
beyond it's domain. This is a common problem with for loops.</p>
|
|
<pre class="programlisting">#include <cassert>
|
|
#include <exception>
|
|
#include <iostream>
|
|
|
|
#include "../include/safe_integer.hpp"
|
|
|
|
void detected_msg(bool detected){
|
|
std::cout << (detected ? "error detected!" : "error NOT detected! ") << std::endl;
|
|
}
|
|
|
|
int main(int argc, const char * argv[]){
|
|
std::cout << "example 3:";
|
|
std::cout << "undetected underflow in data type" << std::endl;
|
|
std::cout << "Not using safe numerics" << std::endl;
|
|
try{
|
|
unsigned int x = 0;
|
|
// the following silently produces an incorrect result
|
|
--x;
|
|
// because C/C++ implicitly converts mis-matched arguments to int
|
|
// suggests that the operation is correct
|
|
assert(x == -1);
|
|
// even though it's not !!!
|
|
|
|
// so the error is not detected!
|
|
std::cout << x << " != " << -1 << std::endl;
|
|
detected_msg(false);
|
|
}
|
|
catch(std::exception){
|
|
assert(false); // never arrive here
|
|
}
|
|
// solution: replace unsigned int with safe<unsigned int>
|
|
std::cout << "Using safe numerics" << std::endl;
|
|
try{
|
|
using namespace boost::numeric;
|
|
safe<unsigned int> x = 0;
|
|
// decrement unsigned to less than zero throws exception
|
|
--x;
|
|
assert(false); // never arrive here
|
|
}
|
|
catch(std::exception & e){
|
|
std::cout << e.what() << std::endl;
|
|
detected_msg(true);
|
|
}
|
|
return 0;
|
|
}
|
|
</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="2.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="4.html"><img src="../images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|