• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>tom_int</title>
5<link rel="stylesheet" href="../../../multiprecision.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../../index.html" title="Chapter 1. Boost.Multiprecision">
8<link rel="up" href="../ints.html" title="Integer Types">
9<link rel="prev" href="gmp_int.html" title="gmp_int">
10<link rel="next" href="egs.html" title="Examples">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="gmp_int.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../ints.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="egs.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h4 class="title">
27<a name="boost_multiprecision.tut.ints.tom_int"></a><a class="link" href="tom_int.html" title="tom_int">tom_int</a>
28</h4></div></div></div>
29<p>
30          <code class="computeroutput"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">tommath</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></code>
31        </p>
32<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">multiprecision</span><span class="special">{</span>
33
34<span class="keyword">class</span> <span class="identifier">tommath_int</span><span class="special">;</span>
35
36<span class="keyword">typedef</span> <span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">tommath_int</span> <span class="special">&gt;</span>         <span class="identifier">tom_int</span><span class="special">;</span>
37
38<span class="special">}}</span> <span class="comment">// namespaces</span>
39</pre>
40<p>
41          The <code class="computeroutput"><span class="identifier">tommath_int</span></code> back-end
42          is used via the typedef <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">tom_int</span></code>.
43          It acts as a thin wrapper around the <a href="http://libtom.net" target="_top">libtommath</a>
44          <code class="computeroutput"><span class="identifier">tom_int</span></code> to provide an integer
45          type that is a drop-in replacement for the native C++ integer types, but
46          with unlimited precision.
47        </p>
48<p>
49          Things you should know when using this type:
50        </p>
51<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
52<li class="listitem">
53              Default constructed objects have the value zero (this is <a href="http://libtom.net" target="_top">libtommath</a>'s
54              default behavior).
55            </li>
56<li class="listitem">
57              Although <code class="computeroutput"><span class="identifier">tom_int</span></code> is
58              mostly a drop in replacement for the integer <a href="https://en.cppreference.com/w/cpp/language/types" target="_top">fundamental
59              (built-in) types</a>, it should be noted that it is a rather strange
60              beast as it's a signed type that is not a 2's complement type. As a
61              result the bitwise operations <code class="computeroutput"><span class="special">|</span>
62              <span class="special">&amp;</span> <span class="special">^</span></code>
63              will throw a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span></code> exception if either
64              of the arguments is negative. Similarly the complement operator<code class="computeroutput"><span class="special">~</span></code> is deliberately not implemented for
65              this type.
66            </li>
67<li class="listitem">
68              Formatted IO for this type does not support octal or hexadecimal notation
69              for negative values, as a result performing formatted output on this
70              type when the argument is negative and either of the flags <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">ios_base</span><span class="special">::</span><span class="identifier">oct</span></code> or <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">ios_base</span><span class="special">::</span><span class="identifier">hex</span></code>
71              are set, will result in a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span></code>
72              will be thrown.
73            </li>
74<li class="listitem">
75              Conversion from a string results in a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span></code>
76              being thrown if the string can not be interpreted as a valid integer.
77            </li>
78<li class="listitem">
79              Division by zero results in a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">overflow_error</span></code>
80              being thrown.
81            </li>
82</ul></div>
83<h6>
84<a name="boost_multiprecision.tut.ints.tom_int.h0"></a>
85          <span class="phrase"><a name="boost_multiprecision.tut.ints.tom_int.example"></a></span><a class="link" href="tom_int.html#boost_multiprecision.tut.ints.tom_int.example">Example:</a>
86        </h6>
87<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">tommath</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
88<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
89
90<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
91<span class="special">{</span>
92   <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">tom_int</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span>
93
94   <span class="comment">// Do some arithmetic:</span>
95   <span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;=</span> <span class="number">1000</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span>
96      <span class="identifier">v</span> <span class="special">*=</span> <span class="identifier">i</span><span class="special">;</span>
97
98   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">v</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="comment">// prints 1000!</span>
99   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">hex</span> <span class="special">&lt;&lt;</span> <span class="identifier">v</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="comment">// prints 1000! in hex format</span>
100
101   <span class="keyword">try</span><span class="special">{</span>
102      <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">hex</span> <span class="special">&lt;&lt;</span> <span class="special">-</span><span class="identifier">v</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="comment">// Ooops! can't print a negative value in hex format!</span>
103   <span class="special">}</span>
104   <span class="keyword">catch</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span><span class="special">&amp;</span> <span class="identifier">e</span><span class="special">)</span>
105   <span class="special">{</span>
106      <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
107   <span class="special">}</span>
108
109   <span class="keyword">try</span><span class="special">{</span>
110      <span class="comment">// v is not a 2's complement type, bitwise operations are only supported</span>
111      <span class="comment">// on positive values:</span>
112      <span class="identifier">v</span> <span class="special">=</span> <span class="special">-</span><span class="identifier">v</span> <span class="special">&amp;</span> <span class="number">2</span><span class="special">;</span>
113   <span class="special">}</span>
114   <span class="keyword">catch</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span><span class="special">&amp;</span> <span class="identifier">e</span><span class="special">)</span>
115   <span class="special">{</span>
116      <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
117   <span class="special">}</span>
118
119   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
120<span class="special">}</span>
121</pre>
122</div>
123<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
124<td align="left"></td>
125<td align="right"><div class="copyright-footer">Copyright © 2002-2020 John
126      Maddock and Christopher Kormanyos<p>
127        Distributed under the Boost Software License, Version 1.0. (See accompanying
128        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
129      </p>
130</div></td>
131</tr></table>
132<hr>
133<div class="spirit-nav">
134<a accesskey="p" href="gmp_int.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../ints.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="egs.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
135</div>
136</body>
137</html>
138