• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>gmp_float</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="../floats.html" title="Floating-point Types">
9<link rel="prev" href="cpp_dec_float.html" title="cpp_dec_float">
10<link rel="next" href="mpfr_float.html" title="mpfr_float">
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_dec_float.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../floats.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="mpfr_float.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.floats.gmp_float"></a><a class="link" href="gmp_float.html" title="gmp_float">gmp_float</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">gmp</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">template</span> <span class="special">&lt;</span><span class="keyword">unsigned</span> <span class="identifier">Digits10</span><span class="special">&gt;</span>
35<span class="keyword">class</span> <span class="identifier">gmp_float</span><span class="special">;</span>
36
37<span class="keyword">typedef</span> <span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">gmp_float</span><span class="special">&lt;</span><span class="number">50</span><span class="special">&gt;</span> <span class="special">&gt;</span>    <span class="identifier">mpf_float_50</span><span class="special">;</span>
38<span class="keyword">typedef</span> <span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">gmp_float</span><span class="special">&lt;</span><span class="number">100</span><span class="special">&gt;</span> <span class="special">&gt;</span>   <span class="identifier">mpf_float_100</span><span class="special">;</span>
39<span class="keyword">typedef</span> <span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">gmp_float</span><span class="special">&lt;</span><span class="number">500</span><span class="special">&gt;</span> <span class="special">&gt;</span>   <span class="identifier">mpf_float_500</span><span class="special">;</span>
40<span class="keyword">typedef</span> <span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">gmp_float</span><span class="special">&lt;</span><span class="number">1000</span><span class="special">&gt;</span> <span class="special">&gt;</span>  <span class="identifier">mpf_float_1000</span><span class="special">;</span>
41<span class="keyword">typedef</span> <span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">gmp_float</span><span class="special">&lt;</span><span class="number">0</span><span class="special">&gt;</span> <span class="special">&gt;</span>     <span class="identifier">mpf_float</span><span class="special">;</span>
42
43<span class="special">}}</span> <span class="comment">// namespaces</span>
44</pre>
45<p>
46          The <code class="computeroutput"><span class="identifier">gmp_float</span></code> back-end
47          is used in conjunction with <code class="computeroutput"><span class="identifier">number</span></code>
48          : it acts as a thin wrapper around the <a href="http://gmplib.org" target="_top">GMP</a>
49          <code class="computeroutput"><span class="identifier">mpf_t</span></code> to provide an real-number
50          type that is a drop-in replacement for the native C++ floating-point types,
51          but with much greater precision.
52        </p>
53<p>
54          Type <code class="computeroutput"><span class="identifier">gmp_float</span></code> can be used
55          at fixed precision by specifying a non-zero <code class="computeroutput"><span class="identifier">Digits10</span></code>
56          template parameter, or at variable precision by setting the template argument
57          to zero. The typedefs mpf_float_50, mpf_float_100, mpf_float_500, mpf_float_1000
58          provide arithmetic types at 50, 100, 500 and 1000 decimal digits precision
59          respectively. The typedef mpf_float provides a variable precision type
60          whose precision can be controlled via the <code class="computeroutput"><span class="identifier">number</span></code>s
61          member functions.
62        </p>
63<div class="note"><table border="0" summary="Note">
64<tr>
65<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td>
66<th align="left">Note</th>
67</tr>
68<tr><td align="left" valign="top"><p>
69            This type only provides standard library and <code class="computeroutput"><span class="identifier">numeric_limits</span></code>
70            support when the precision is fixed at compile time.
71          </p></td></tr>
72</table></div>
73<p>
74          As well as the usual conversions from arithmetic and string types, instances
75          of <code class="computeroutput"><span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">mpf_float</span><span class="special">&lt;</span><span class="identifier">N</span><span class="special">&gt;</span> <span class="special">&gt;</span></code> are copy constructible and assignable
76          from:
77        </p>
78<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
79<li class="listitem">
80              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>,
81              <code class="computeroutput"><span class="identifier">mpq_t</span></code>.
82            </li>
83<li class="listitem">
84              The <code class="computeroutput"><span class="identifier">number</span></code> wrappers
85              around those types: <code class="computeroutput"><span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">mpf_float</span><span class="special">&lt;</span><span class="identifier">M</span><span class="special">&gt;</span> <span class="special">&gt;</span></code>,
86              <code class="computeroutput"><span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">gmp_int</span><span class="special">&gt;</span></code>,
87              <code class="computeroutput"><span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">gmp_rational</span><span class="special">&gt;</span></code>.
88            </li>
89</ul></div>
90<p>
91          It's also possible to access the underlying <code class="computeroutput"><span class="identifier">mpf_t</span></code>
92          via the <code class="computeroutput"><span class="identifier">data</span><span class="special">()</span></code>
93          member function of <code class="computeroutput"><span class="identifier">gmp_float</span></code>.
94        </p>
95<p>
96          Things you should know when using this type:
97        </p>
98<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
99<li class="listitem">
100              Default constructed <code class="computeroutput"><span class="identifier">gmp_float</span></code>s
101              have the value zero (this is the <a href="http://gmplib.org" target="_top">GMP</a>
102              library's default behavior).
103            </li>
104<li class="listitem">
105              No changes are made to the <a href="http://gmplib.org" target="_top">GMP</a>
106              library's global settings, so this type can be safely mixed with existing
107              <a href="http://gmplib.org" target="_top">GMP</a> code.
108            </li>
109<li class="listitem">
110              This backend supports rvalue-references and is move-aware, making instantiations
111              of <code class="computeroutput"><span class="identifier">number</span></code> on this backend
112              move aware.
113            </li>
114<li class="listitem">
115              It is not possible to round-trip objects of this type to and from a
116              string and get back exactly the same value. This appears to be a limitation
117              of <a href="http://gmplib.org" target="_top">GMP</a>.
118            </li>
119<li class="listitem">
120              Since the underlying <a href="http://gmplib.org" target="_top">GMP</a> types
121              have no notion of infinities or NaNs, care should be taken to avoid
122              numeric overflow or division by zero. That latter will result in a
123              std::overflow_error being thrown, while generating excessively large
124              exponents may result in instability of the underlying <a href="http://gmplib.org" target="_top">GMP</a>
125              library (in testing, converting a number with an excessively large
126              or small exponent to a string caused <a href="http://gmplib.org" target="_top">GMP</a>
127              to segfault).
128            </li>
129<li class="listitem">
130              This type can equally be used with <a href="http://mpir.org/" target="_top">MPIR</a>
131              as the underlying implementation - indeed that is the recommended option
132              on Win32.
133            </li>
134<li class="listitem">
135              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>
136              being thrown if the string can not be interpreted as a valid floating-point
137              number.
138            </li>
139<li class="listitem">
140              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>
141              being thrown.
142            </li>
143</ul></div>
144<h6>
145<a name="boost_multiprecision.tut.floats.gmp_float.h0"></a>
146          <span class="phrase"><a name="boost_multiprecision.tut.floats.gmp_float.gmp_example"></a></span><a class="link" href="gmp_float.html#boost_multiprecision.tut.floats.gmp_float.gmp_example"> GMP example:</a>
147        </h6>
148<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">gmp</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
149<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">special_functions</span><span class="special">/</span><span class="identifier">gamma</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
150<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
151
152<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
153<span class="special">{</span>
154   <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>
155
156   <span class="comment">// Operations at variable precision and limited standard library support:</span>
157   <span class="identifier">mpf_float</span> <span class="identifier">a</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span>
158   <span class="identifier">mpf_float</span><span class="special">::</span><span class="identifier">default_precision</span><span class="special">(</span><span class="number">1000</span><span class="special">);</span>
159   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">mpf_float</span><span class="special">::</span><span class="identifier">default_precision</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>
160   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">sqrt</span><span class="special">(</span><span class="identifier">a</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> <span class="comment">// print root-2</span>
161
162   <span class="comment">// Operations at fixed precision and full standard library support:</span>
163   <span class="identifier">mpf_float_100</span> <span class="identifier">b</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span>
164   <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">numeric_limits</span><span class="special">&lt;</span><span class="identifier">mpf_float_100</span><span class="special">&gt;::</span><span class="identifier">digits</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>
165   <span class="comment">// We can use any C++ std lib function:</span>
166   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">log</span><span class="special">(</span><span class="identifier">b</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> <span class="comment">// print log(2)</span>
167   <span class="comment">// We can also use any function from Boost.Math:</span>
168   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tgamma</span><span class="special">(</span><span class="identifier">b</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>
169   <span class="comment">// These even work when the argument is an expression template:</span>
170   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">tgamma</span><span class="special">(</span><span class="identifier">b</span> <span class="special">*</span> <span class="identifier">b</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>
171
172   <span class="comment">// Access the underlying representation:</span>
173   <span class="identifier">mpf_t</span> <span class="identifier">f</span><span class="special">;</span>
174   <span class="identifier">mpf_init</span><span class="special">(</span><span class="identifier">f</span><span class="special">);</span>
175   <span class="identifier">mpf_set</span><span class="special">(</span><span class="identifier">f</span><span class="special">,</span> <span class="identifier">a</span><span class="special">.</span><span class="identifier">backend</span><span class="special">().</span><span class="identifier">data</span><span class="special">());</span>
176   <span class="identifier">mpf_clear</span><span class="special">(</span><span class="identifier">f</span><span class="special">);</span>
177   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
178<span class="special">}</span>
179</pre>
180</div>
181<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
182<td align="left"></td>
183<td align="right"><div class="copyright-footer">Copyright © 2002-2020 John
184      Maddock and Christopher Kormanyos<p>
185        Distributed under the Boost Software License, Version 1.0. (See accompanying
186        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>)
187      </p>
188</div></td>
189</tr></table>
190<hr>
191<div class="spirit-nav">
192<a accesskey="p" href="cpp_dec_float.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../floats.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="mpfr_float.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
193</div>
194</body>
195</html>
196