<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
"http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
<section id="safe_numerics.checked_arithmetic">
  <title>Checked Arithmetic</title>

  <?dbhtml stop-chunking?>

  <section>
    <title>Description</title>

    <para>Perform binary operations on arithmetic types. Return either a valid
    result or an error code. Under no circumstances should an incorrect result
    be returned.</para>
  </section>

  <section>
    <title>Type requirements</title>

    <para>All template parameters of the functions must model <link
    linkend="safe_numerics.integer">Numeric</link> type requirements.</para>
  </section>

  <section>
    <title>Complexity</title>

    <para>Each function performs one and only one arithmetic operation.</para>
  </section>

  <section>
    <title>Example of use</title>

    <programlisting>#include &lt;boost/numeric/safe_numerics/checked_default.hpp&gt;

checked_result&lt;int&gt; r = checked::multiply&lt;int&gt;(24, 42);
</programlisting>
  </section>

  <section>
    <title>Notes</title>

    <para>Some compilers have command line switches (e.g. -ftrapv) which
    enable special behavior such that erroneous integer operations are
    detected at run time. The library has been implemented in such a way that
    these facilities are not used. It's possible they might be helpful in
    particular environment. These could be be exploited by re-implementing
    some functions in this library.</para>
  </section>

  <section>
    <title>Synopsis</title>

    <programlisting>// safe casting on primitive types
template&lt;class R, class T&gt;
checked_result&lt;R&gt; constexpr checked::cast(const T &amp; t);

// safe addition on primitive types
template&lt;class R&gt;
checked_result&lt;R&gt; constexpr checked::add(const R &amp; t, const R &amp; u);

// safe subtraction on primitive types
template&lt;class R&gt;
checked_result&lt;R&gt; constexpr checked::subtract(const R &amp; t, const R &amp; u);

// safe multiplication on primitive types
template&lt;class R&gt;
checked_result&lt;R&gt; constexpr checked::multiply(const R &amp; t, const R &amp; u);

// safe division on primitive types
template&lt;class R&gt;
checked_result&lt;R&gt; constexpr checked::divide(const R &amp; t, const R &amp; u);

// safe modulus on primitive types
template&lt;class R&gt;
checked_result&lt;R&gt; constexpr checked::modulus(const R &amp; t, const R &amp; u);

// safe less than predicate on primitive types
template&lt;class R&gt;
bool constexpr checked::less_than(const R &amp; t, const R &amp; u);

// safe greater_than_equal predicate on primitive types
template&lt;class R&gt;
bool constexpr checked::greater_than_equal(const R &amp; t, const R &amp; u);

// safe greater_than predicate on primitive types
template&lt;class R&gt;
bool constexpr checked::greater_than(const R &amp; t, const R &amp; u);

// safe less_than_equal predicate on primitive types
template&lt;class R&gt;
bool constexpr checked::less_than_equal(const R &amp; t, const R &amp; u);

// safe equal predicate on primitive types
template&lt;class R&gt;
bool constexpr checked::equal(const R &amp; t, const R &amp; u);

// left shift
template&lt;class R&gt;
checked_result&lt;R&gt; constexpr checked::left_shift(const R &amp; t, const R &amp; u);

// right shift
template&lt;class R&gt;
checked_result&lt;R&gt; constexpr checked::right_shift(const R &amp; t, const R &amp; u);

// bitwise operations
template&lt;class R&gt;
checked_result&lt;R&gt; constexpr checked::bitwise_or(const R &amp; t, const R &amp; u);

template&lt;class R&gt;
checked_result&lt;R&gt; constexpr checked::bitwise_and(const R &amp; t, const R &amp; u);

template&lt;class R&gt;
checked_result&lt;R&gt; constexpr checked::bitwise_xor(const R &amp; t, const R &amp; u);
</programlisting>
  </section>

  <section>
    <title>See Also</title>

    <para><link
    linkend="safenumerics.checked_result">checked_result&lt;R&gt;</link></para>
  </section>

  <section>
    <title>Header</title>

    <para><ulink
    url="../../include/boost/safe_numerics/checked_default.hpp"><code>#include
    &lt;boost/numeric/safe_numerics/checked_default.hpp&gt;
    </code></ulink></para>

    <para><ulink
    url="../../include/boost/safe_numerics/checked_integer.hpp"><code>#include
    &lt;boost/numeric/safe_numerics/checked_integer.hpp&gt;
    </code></ulink></para>

    <para><ulink url="../../include/checked.hpp"><code/></ulink><ulink
    url="../../include/boost/safe_numerics/checked_float.hpp"><code>#include
    &lt;boost/numeric/safe_numerics/checked_float.hpp&gt;
    </code></ulink></para>
  </section>
</section>
