1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>gmp_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="cpp_int.html" title="cpp_int"> 10<link rel="next" href="tom_int.html" title="tom_int"> 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="cpp_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="tom_int.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.gmp_int"></a><a class="link" href="gmp_int.html" title="gmp_int">gmp_int</a> 28</h4></div></div></div> 29<p> 30 <code class="computeroutput"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">gmp</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></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">gmp_int</span><span class="special">;</span> 35 36<span class="keyword">typedef</span> <span class="identifier">number</span><span class="special"><</span><span class="identifier">gmp_int</span> <span class="special">></span> <span class="identifier">mpz_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">gmp_int</span></code> back-end is 42 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">mpz_int</span></code>. 43 It acts as a thin wrapper around the <a href="http://gmplib.org" target="_top">GMP</a> 44 <code class="computeroutput"><span class="identifier">mpz_t</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 As well as the usual conversions from arithmetic and string types, type 50 <code class="computeroutput"><span class="identifier">mpz_int</span></code> is copy constructible 51 and assignable from: 52 </p> 53<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 54<li class="listitem"> 55 The <a href="http://gmplib.org" target="_top">GMP</a> native types: <code class="computeroutput"><span class="identifier">mpf_t</span></code>, <code class="computeroutput"><span class="identifier">mpz_t</span></code>, 56 <code class="computeroutput"><span class="identifier">mpq_t</span></code>. 57 </li> 58<li class="listitem"> 59 Instances of <code class="computeroutput"><span class="identifier">number</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span></code> that are wrappers around those 60 types: <code class="computeroutput"><span class="identifier">number</span><span class="special"><</span><span class="identifier">gmp_float</span><span class="special"><</span><span class="identifier">N</span><span class="special">></span> <span class="special">></span></code>, <code class="computeroutput"><span class="identifier">number</span><span class="special"><</span><span class="identifier">gmp_rational</span><span class="special">></span></code>. 61 </li> 62</ul></div> 63<p> 64 It's also possible to access the underlying <code class="computeroutput"><span class="identifier">mpz_t</span></code> 65 via the <code class="computeroutput"><span class="identifier">data</span><span class="special">()</span></code> 66 member function of <code class="computeroutput"><span class="identifier">gmp_int</span></code>. 67 </p> 68<p> 69 Things you should know when using this type: 70 </p> 71<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 72<li class="listitem"> 73 No changes are made to the GMP library's global settings - so you can 74 safely mix this type with existing code that uses <a href="http://gmplib.org" target="_top">GMP</a>. 75 </li> 76<li class="listitem"> 77 Default constructed <code class="computeroutput"><span class="identifier">gmp_int</span></code>s 78 have the value zero (this is GMP's default behavior). 79 </li> 80<li class="listitem"> 81 Formatted IO for this type does not support octal or hexadecimal notation 82 for negative values, as a result performing formatted output on this 83 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> 84 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> 85 will be thrown. 86 </li> 87<li class="listitem"> 88 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> 89 being thrown if the string can not be interpreted as a valid integer. 90 </li> 91<li class="listitem"> 92 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> 93 being thrown. 94 </li> 95<li class="listitem"> 96 Although this type is a wrapper around <a href="http://gmplib.org" target="_top">GMP</a> 97 it will work equally well with <a href="http://mpir.org/" target="_top">MPIR</a>. 98 Indeed use of <a href="http://mpir.org/" target="_top">MPIR</a> is recommended 99 on Win32. 100 </li> 101<li class="listitem"> 102 This backend supports rvalue-references and is move-aware, making instantiations 103 of <code class="computeroutput"><span class="identifier">number</span></code> on this backend 104 move aware. 105 </li> 106</ul></div> 107<h6> 108<a name="boost_multiprecision.tut.ints.gmp_int.h0"></a> 109 <span class="phrase"><a name="boost_multiprecision.tut.ints.gmp_int.example"></a></span><a class="link" href="gmp_int.html#boost_multiprecision.tut.ints.gmp_int.example">Example:</a> 110 </h6> 111<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">gmp</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 112<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 113 114<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 115<span class="special">{</span> 116 <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">;</span> 117 118 <span class="identifier">mpz_int</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> 119 120 <span class="comment">// Do some arithmetic:</span> 121 <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"><=</span> <span class="number">1000</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> 122 <span class="identifier">v</span> <span class="special">*=</span> <span class="identifier">i</span><span class="special">;</span> 123 124 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">v</span> <span class="special"><<</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> 125 126 <span class="comment">// Access the underlying representation:</span> 127 <span class="identifier">mpz_t</span> <span class="identifier">z</span><span class="special">;</span> 128 <span class="identifier">mpz_init</span><span class="special">(</span><span class="identifier">z</span><span class="special">);</span> 129 <span class="identifier">mpz_set</span><span class="special">(</span><span class="identifier">z</span><span class="special">,</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">backend</span><span class="special">().</span><span class="identifier">data</span><span class="special">());</span> 130 <span class="identifier">mpz_clear</span><span class="special">(</span><span class="identifier">z</span><span class="special">);</span> 131 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 132<span class="special">}</span> 133</pre> 134</div> 135<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 136<td align="left"></td> 137<td align="right"><div class="copyright-footer">Copyright © 2002-2020 John 138 Maddock and Christopher Kormanyos<p> 139 Distributed under the Boost Software License, Version 1.0. (See accompanying 140 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>) 141 </p> 142</div></td> 143</tr></table> 144<hr> 145<div class="spirit-nav"> 146<a accesskey="p" href="cpp_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="tom_int.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 147</div> 148</body> 149</html> 150