mirror of
https://github.com/boostorg/safe_numerics.git
synced 2026-02-25 16:42:33 +00:00
More carefully defined the concepts. That is the concept of the library is now clear: Trap at either compile or runtime integer operations which yield a result which differs from the normal arithmetic result. Pending on this check in: a) only addition is fixed up so far b) uses BOOST_TYPEOF... rather than decltype(... - final decision sill pending. c) comments out some compile time checks which should be re-instated. d) fixed tests so that they actually work!
461 lines
10 KiB
XML
461 lines
10 KiB
XML
<?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.numeric">
|
||
<title>Numeric<T></title>
|
||
|
||
<section>
|
||
<title>Description</title>
|
||
|
||
<para>A type is Numeric if it has the properties of a number.</para>
|
||
|
||
<para>More specifically, a type T is Numeric if there exists
|
||
specialization of <code>std::numeric_limits<T></code>. See the
|
||
documentation for standard library class numeric_limits. The standard
|
||
library includes such specializations for all the primitive numeric types.
|
||
Note that this concept is distinct from the C++ standard library type
|
||
traits <code>is_integral</code> and <code>is_arithmetic</code>. These
|
||
latter fullfill the requirement of the concept Numeric. But there are
|
||
types T which fullfill this concept for which
|
||
<code>is_arithmetic<T>::value == false</code>. For example see
|
||
<code>safe_signed_integer<int></code>.</para>
|
||
</section>
|
||
|
||
<section>
|
||
<title>Notation</title>
|
||
|
||
<table colsep="0" frame="all" rowsep="0">
|
||
<title>Notation</title>
|
||
|
||
<tgroup cols="2" colsep="1" rowsep="1">
|
||
<colspec align="left"/>
|
||
|
||
<colspec align="left" colwidth="3*"/>
|
||
|
||
<tbody>
|
||
<row>
|
||
<entry><code>T, U, V</code></entry>
|
||
|
||
<entry>A type that is a model of the Numeric</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t, u, v</code></entry>
|
||
|
||
<entry>An object of type modeling Numeric</entry>
|
||
</row>
|
||
</tbody>
|
||
</tgroup>
|
||
</table>
|
||
</section>
|
||
|
||
<section>
|
||
<title>Associated Types</title>
|
||
|
||
<table>
|
||
<title>Associated Types</title>
|
||
|
||
<tgroup cols="2">
|
||
<colspec align="left"/>
|
||
|
||
<colspec align="left" colwidth="3*"/>
|
||
|
||
<tbody>
|
||
<row>
|
||
<entry><code>std::numeric_limits<T></code></entry>
|
||
|
||
<entry>The numeric_limits class template provides a C++ program
|
||
with information about various properties of the implementation’s
|
||
representation of the arithmetic types. See C++ standard
|
||
18.3.2.2.</entry>
|
||
</row>
|
||
</tbody>
|
||
</tgroup>
|
||
</table>
|
||
</section>
|
||
|
||
<section>
|
||
<title>Valid Expressions</title>
|
||
|
||
<para>In addition to the expressions defined in <ulink
|
||
url="http://www.sgi.com/tech/stl/Assignable.html">Assignable</ulink> the
|
||
following expressions must be valid.</para>
|
||
|
||
<para>Any operations which result in integers which cannot be represented
|
||
as some Numeric type will throw an exception.<table>
|
||
<title>General</title>
|
||
|
||
<tgroup cols="2">
|
||
<colspec align="left"/>
|
||
|
||
<colspec align="left"/>
|
||
|
||
<thead>
|
||
<row>
|
||
<entry align="left">Expression</entry>
|
||
|
||
<entry>Return Value</entry>
|
||
</row>
|
||
</thead>
|
||
|
||
<tbody>
|
||
<row>
|
||
<entry><code>std::numeric_limits<T>.is_bounded
|
||
</code></entry>
|
||
|
||
<entry>true</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>std::numeric_limits<T>.is_integer</code></entry>
|
||
|
||
<entry>true</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>std::numeric_limits<T>.is_specialized
|
||
</code></entry>
|
||
|
||
<entry>true</entry>
|
||
</row>
|
||
</tbody>
|
||
</tgroup>
|
||
</table></para>
|
||
|
||
<table>
|
||
<title>Unary Operators</title>
|
||
|
||
<tgroup cols="3">
|
||
<colspec align="left"/>
|
||
|
||
<colspec align="left"/>
|
||
|
||
<colspec align="left" colwidth="3*"/>
|
||
|
||
<thead>
|
||
<row>
|
||
<entry align="left">Expression</entry>
|
||
|
||
<entry>Return Type</entry>
|
||
|
||
<entry>Semantics</entry>
|
||
</row>
|
||
</thead>
|
||
|
||
<tbody>
|
||
<row>
|
||
<entry><code>-t</code></entry>
|
||
|
||
<entry>T</entry>
|
||
|
||
<entry>Invert sign</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>+t</code></entry>
|
||
|
||
<entry>T</entry>
|
||
|
||
<entry>unary plus - a no op</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t--</code></entry>
|
||
|
||
<entry>T</entry>
|
||
|
||
<entry>post decrement</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t++</code></entry>
|
||
|
||
<entry>T</entry>
|
||
|
||
<entry>post increment</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>--t</code></entry>
|
||
|
||
<entry>T</entry>
|
||
|
||
<entry>predecrement</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>++t</code></entry>
|
||
|
||
<entry>T</entry>
|
||
|
||
<entry>preincrement</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>~</code></entry>
|
||
|
||
<entry>T</entry>
|
||
|
||
<entry>complement</entry>
|
||
</row>
|
||
</tbody>
|
||
</tgroup>
|
||
</table>
|
||
|
||
<table>
|
||
<title>Binary Operators</title>
|
||
|
||
<tgroup cols="3">
|
||
<colspec align="left"/>
|
||
|
||
<colspec align="left"/>
|
||
|
||
<colspec align="left" colwidth="3*"/>
|
||
|
||
<thead>
|
||
<row>
|
||
<entry align="left">Expression</entry>
|
||
|
||
<entry>Return Type</entry>
|
||
|
||
<entry>Semantics</entry>
|
||
</row>
|
||
</thead>
|
||
|
||
<tbody>
|
||
<row>
|
||
<entry><code>t - u</code></entry>
|
||
|
||
<entry>V</entry>
|
||
|
||
<entry>subtract u from t</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t + u</code></entry>
|
||
|
||
<entry>V</entry>
|
||
|
||
<entry>add u to t</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t * u</code></entry>
|
||
|
||
<entry>V</entry>
|
||
|
||
<entry>multiply t by u</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t / u</code></entry>
|
||
|
||
<entry>T</entry>
|
||
|
||
<entry>divide t by u</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t % u</code></entry>
|
||
|
||
<entry>T</entry>
|
||
|
||
<entry>t modulus u</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t << u</code></entry>
|
||
|
||
<entry>T</entry>
|
||
|
||
<entry>shift t left u bits</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t >> u</code></entry>
|
||
|
||
<entry>T</entry>
|
||
|
||
<entry>shift t right by ubits</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t < u</code></entry>
|
||
|
||
<entry>bool</entry>
|
||
|
||
<entry>true if t less than u, false otherwise</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t <= u</code></entry>
|
||
|
||
<entry>bool</entry>
|
||
|
||
<entry>true if t less than or equal to u, false otherwise</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t > u</code></entry>
|
||
|
||
<entry>bool</entry>
|
||
|
||
<entry>true if t greater than u, false otherwise</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t >= u</code></entry>
|
||
|
||
<entry>bool</entry>
|
||
|
||
<entry>true if t greathan or equal to u, false otherwise</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t == u</code></entry>
|
||
|
||
<entry>bool</entry>
|
||
|
||
<entry>true if t equal to u, false otherwise</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t != u</code></entry>
|
||
|
||
<entry>bool</entry>
|
||
|
||
<entry>true if t not equal to u, false otherwise</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t & u</code></entry>
|
||
|
||
<entry>V</entry>
|
||
|
||
<entry>and of t and u padded out max # bits in t, u</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t | u</code></entry>
|
||
|
||
<entry>V</entry>
|
||
|
||
<entry>or of t and u padded out max # bits in t, u</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t ^ u</code></entry>
|
||
|
||
<entry>V</entry>
|
||
|
||
<entry>exclusive or of t and u padded out max # bits in t,
|
||
u</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t = u</code></entry>
|
||
|
||
<entry><code>T</code></entry>
|
||
|
||
<entry>assign value of u to t</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t += u</code></entry>
|
||
|
||
<entry><code>T</code></entry>
|
||
|
||
<entry>add u to t and assign to t</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t -= u</code></entry>
|
||
|
||
<entry><code>T</code></entry>
|
||
|
||
<entry>subtract u from t and assign to t</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t *= u</code></entry>
|
||
|
||
<entry><code>T</code></entry>
|
||
|
||
<entry>multiply t by u and assign to t</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t /= u</code></entry>
|
||
|
||
<entry><code>T</code></entry>
|
||
|
||
<entry>divide t by u and assign to t</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t &= u</code></entry>
|
||
|
||
<entry><code>T</code></entry>
|
||
|
||
<entry>and t with u and assign to t</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t <<= u</code></entry>
|
||
|
||
<entry>T</entry>
|
||
|
||
<entry>left shift the value of t by u bits</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t >>= u</code></entry>
|
||
|
||
<entry>T</entry>
|
||
|
||
<entry>right shift the value of t by u bits</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t &= u</code></entry>
|
||
|
||
<entry>T</entry>
|
||
|
||
<entry>and the value of t with u and assign to t</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t |= u</code></entry>
|
||
|
||
<entry>T</entry>
|
||
|
||
<entry>or the value of t with u and assign to t</entry>
|
||
</row>
|
||
|
||
<row>
|
||
<entry><code>t ^= u</code></entry>
|
||
|
||
<entry>T</entry>
|
||
|
||
<entry>exclusive or the value of t with u and assign to t</entry>
|
||
</row>
|
||
</tbody>
|
||
</tgroup>
|
||
</table>
|
||
</section>
|
||
|
||
<section>
|
||
<title>Header</title>
|
||
|
||
<para><ulink url="../../include/concept/numeric.hpp"><code>#include
|
||
<safe_numerics/include/concepts/numeric.hpp> </code></ulink></para>
|
||
</section>
|
||
|
||
<section>
|
||
<title>Models</title>
|
||
|
||
<para><code>int, safe_signed_integer<int>,
|
||
safe_signed_range<int>, etc.</code></para>
|
||
</section>
|
||
</section>
|