1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN" 3"http://www.boost.org/tools/boostbook/dtd/boostbook.dtd"> 4<section id="safe_numerics.checked_arithmetic"> 5 <title>Checked Arithmetic</title> 6 7 <?dbhtml stop-chunking?> 8 9 <section> 10 <title>Description</title> 11 12 <para>Perform binary operations on arithmetic types. Return either a valid 13 result or an error code. Under no circumstances should an incorrect result 14 be returned.</para> 15 </section> 16 17 <section> 18 <title>Type requirements</title> 19 20 <para>All template parameters of the functions must model <link 21 linkend="safe_numerics.integer">Numeric</link> type requirements.</para> 22 </section> 23 24 <section> 25 <title>Complexity</title> 26 27 <para>Each function performs one and only one arithmetic operation.</para> 28 </section> 29 30 <section> 31 <title>Example of use</title> 32 33 <programlisting>#include <boost/numeric/safe_numerics/checked_default.hpp> 34 35checked_result<int> r = checked::multiply<int>(24, 42); 36</programlisting> 37 </section> 38 39 <section> 40 <title>Notes</title> 41 42 <para>Some compilers have command line switches (e.g. -ftrapv) which 43 enable special behavior such that erroneous integer operations are 44 detected at run time. The library has been implemented in such a way that 45 these facilities are not used. It's possible they might be helpful in 46 particular environment. These could be be exploited by re-implementing 47 some functions in this library.</para> 48 </section> 49 50 <section> 51 <title>Synopsis</title> 52 53 <programlisting>// safe casting on primitive types 54template<class R, class T> 55checked_result<R> constexpr checked::cast(const T & t); 56 57// safe addition on primitive types 58template<class R> 59checked_result<R> constexpr checked::add(const R & t, const R & u); 60 61// safe subtraction on primitive types 62template<class R> 63checked_result<R> constexpr checked::subtract(const R & t, const R & u); 64 65// safe multiplication on primitive types 66template<class R> 67checked_result<R> constexpr checked::multiply(const R & t, const R & u); 68 69// safe division on primitive types 70template<class R> 71checked_result<R> constexpr checked::divide(const R & t, const R & u); 72 73// safe modulus on primitive types 74template<class R> 75checked_result<R> constexpr checked::modulus(const R & t, const R & u); 76 77// safe less than predicate on primitive types 78template<class R> 79bool constexpr checked::less_than(const R & t, const R & u); 80 81// safe greater_than_equal predicate on primitive types 82template<class R> 83bool constexpr checked::greater_than_equal(const R & t, const R & u); 84 85// safe greater_than predicate on primitive types 86template<class R> 87bool constexpr checked::greater_than(const R & t, const R & u); 88 89// safe less_than_equal predicate on primitive types 90template<class R> 91bool constexpr checked::less_than_equal(const R & t, const R & u); 92 93// safe equal predicate on primitive types 94template<class R> 95bool constexpr checked::equal(const R & t, const R & u); 96 97// left shift 98template<class R> 99checked_result<R> constexpr checked::left_shift(const R & t, const R & u); 100 101// right shift 102template<class R> 103checked_result<R> constexpr checked::right_shift(const R & t, const R & u); 104 105// bitwise operations 106template<class R> 107checked_result<R> constexpr checked::bitwise_or(const R & t, const R & u); 108 109template<class R> 110checked_result<R> constexpr checked::bitwise_and(const R & t, const R & u); 111 112template<class R> 113checked_result<R> constexpr checked::bitwise_xor(const R & t, const R & u); 114</programlisting> 115 </section> 116 117 <section> 118 <title>See Also</title> 119 120 <para><link 121 linkend="safenumerics.checked_result">checked_result<R></link></para> 122 </section> 123 124 <section> 125 <title>Header</title> 126 127 <para><ulink 128 url="../../include/boost/safe_numerics/checked_default.hpp"><code>#include 129 <boost/numeric/safe_numerics/checked_default.hpp> 130 </code></ulink></para> 131 132 <para><ulink 133 url="../../include/boost/safe_numerics/checked_integer.hpp"><code>#include 134 <boost/numeric/safe_numerics/checked_integer.hpp> 135 </code></ulink></para> 136 137 <para><ulink url="../../include/checked.hpp"><code/></ulink><ulink 138 url="../../include/boost/safe_numerics/checked_float.hpp"><code>#include 139 <boost/numeric/safe_numerics/checked_float.hpp> 140 </code></ulink></para> 141 </section> 142</section> 143