1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>mpfr_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="gmp_float.html" title="gmp_float"> 10<link rel="next" href="float128.html" title="float128"> 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_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="float128.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.mpfr_float"></a><a class="link" href="mpfr_float.html" title="mpfr_float">mpfr_float</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">mpfr</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">enum</span> <span class="identifier">mpfr_allocation_type</span> 35<span class="special">{</span> 36 <span class="identifier">allocate_stack</span><span class="special">,</span> 37 <span class="identifier">allocate_dynamic</span> 38<span class="special">};</span> 39 40<span class="keyword">template</span> <span class="special"><</span><span class="keyword">unsigned</span> <span class="identifier">Digits10</span><span class="special">,</span> <span class="identifier">mpfr_allocation_type</span> <span class="identifier">AllocateType</span> <span class="special">=</span> <span class="identifier">allocate_dynamic</span><span class="special">></span> 41<span class="keyword">class</span> <span class="identifier">mpfr_float_backend</span><span class="special">;</span> 42 43<span class="keyword">typedef</span> <span class="identifier">number</span><span class="special"><</span><span class="identifier">mpfr_float_backend</span><span class="special"><</span><span class="number">50</span><span class="special">></span> <span class="special">></span> <span class="identifier">mpfr_float_50</span><span class="special">;</span> 44<span class="keyword">typedef</span> <span class="identifier">number</span><span class="special"><</span><span class="identifier">mpfr_float_backend</span><span class="special"><</span><span class="number">100</span><span class="special">></span> <span class="special">></span> <span class="identifier">mpfr_float_100</span><span class="special">;</span> 45<span class="keyword">typedef</span> <span class="identifier">number</span><span class="special"><</span><span class="identifier">mpfr_float_backend</span><span class="special"><</span><span class="number">500</span><span class="special">></span> <span class="special">></span> <span class="identifier">mpfr_float_500</span><span class="special">;</span> 46<span class="keyword">typedef</span> <span class="identifier">number</span><span class="special"><</span><span class="identifier">mpfr_float_backend</span><span class="special"><</span><span class="number">1000</span><span class="special">></span> <span class="special">></span> <span class="identifier">mpfr_float_1000</span><span class="special">;</span> 47<span class="keyword">typedef</span> <span class="identifier">number</span><span class="special"><</span><span class="identifier">mpfr_float_backend</span><span class="special"><</span><span class="number">0</span><span class="special">></span> <span class="special">></span> <span class="identifier">mpfr_float</span><span class="special">;</span> 48 49<span class="keyword">typedef</span> <span class="identifier">number</span><span class="special"><</span><span class="identifier">mpfr_float_backend</span><span class="special"><</span><span class="number">50</span><span class="special">,</span> <span class="identifier">allocate_stack</span><span class="special">></span> <span class="special">></span> <span class="identifier">static_mpfr_float_50</span><span class="special">;</span> 50<span class="keyword">typedef</span> <span class="identifier">number</span><span class="special"><</span><span class="identifier">mpfr_float_backend</span><span class="special"><</span><span class="number">100</span><span class="special">,</span> <span class="identifier">allocate_stack</span><span class="special">></span> <span class="special">></span> <span class="identifier">static_mpfr_float_100</span><span class="special">;</span> 51 52<span class="special">}}</span> <span class="comment">// namespaces</span> 53</pre> 54<p> 55 The <code class="computeroutput"><span class="identifier">mpfr_float_backend</span></code> 56 type is used in conjunction with <code class="computeroutput"><span class="identifier">number</span></code>: 57 It acts as a thin wrapper around the <a href="http://www.mpfr.org" target="_top">MPFR</a> 58 <code class="computeroutput"><span class="identifier">mpfr_t</span></code> to provide an real-number 59 type that is a drop-in replacement for the native C++ floating-point types, 60 but with much greater precision. 61 </p> 62<p> 63 Type <code class="computeroutput"><span class="identifier">mpfr_float_backend</span></code> 64 can be used at fixed precision by specifying a non-zero <code class="computeroutput"><span class="identifier">Digits10</span></code> 65 template parameter, or at variable precision by setting the template argument 66 to zero. The typedefs mpfr_float_50, mpfr_float_100, mpfr_float_500, mpfr_float_1000 67 provide arithmetic types at 50, 100, 500 and 1000 decimal digits precision 68 respectively. The typedef mpfr_float provides a variable precision type 69 whose precision can be controlled via the <code class="computeroutput"><span class="identifier">number</span></code>s 70 member functions. 71 </p> 72<p> 73 In addition the second template parameter lets you choose between dynamic 74 allocation (the default, and uses MPFR's normal allocation routines), or 75 stack allocation (where all the memory required for the underlying data 76 types is stored within <code class="computeroutput"><span class="identifier">mpfr_float_backend</span></code>). 77 The latter option can result in significantly faster code, at the expense 78 of growing the size of <code class="computeroutput"><span class="identifier">mpfr_float_backend</span></code>. 79 It can only be used at <span class="emphasis"><em>fixed precision</em></span>, and should 80 only be used for lower digit counts. Note that we can not guarantee that 81 using <code class="computeroutput"><span class="identifier">allocate_stack</span></code> won't 82 cause any calls to <code class="computeroutput"><span class="identifier">mpfr</span></code>'s 83 allocation routines, as <code class="computeroutput"><span class="identifier">mpfr</span></code> 84 may call these inside its own code. The following table gives an idea of 85 the performance tradeoff's at 50 decimal digits precision<a href="#ftn.boost_multiprecision.tut.floats.mpfr_float.f0" class="footnote" name="boost_multiprecision.tut.floats.mpfr_float.f0"><sup class="footnote">[2]</sup></a>: 86 </p> 87<div class="informaltable"><table class="table"> 88<colgroup> 89<col> 90<col> 91</colgroup> 92<thead><tr> 93<th> 94 <p> 95 Type 96 </p> 97 </th> 98<th> 99 <p> 100 Bessel function evaluation, relative times 101 </p> 102 </th> 103</tr></thead> 104<tbody> 105<tr> 106<td> 107 <p> 108 <code class="computeroutput"><span class="identifier">number</span><span class="special"><</span><span class="identifier">mpfr_float_backend</span><span class="special"><</span><span class="number">50</span><span class="special">,</span> <span class="identifier">allocate_static</span><span class="special">>,</span> 109 <span class="identifier">et_on</span><span class="special">></span></code> 110 </p> 111 </td> 112<td> 113 <p> 114 1.0 (5.5s) 115 </p> 116 </td> 117</tr> 118<tr> 119<td> 120 <p> 121 <code class="computeroutput"><span class="identifier">number</span><span class="special"><</span><span class="identifier">mpfr_float_backend</span><span class="special"><</span><span class="number">50</span><span class="special">,</span> <span class="identifier">allocate_static</span><span class="special">>,</span> 122 <span class="identifier">et_off</span><span class="special">></span></code> 123 </p> 124 </td> 125<td> 126 <p> 127 1.05 (5.8s) 128 </p> 129 </td> 130</tr> 131<tr> 132<td> 133 <p> 134 <code class="computeroutput"><span class="identifier">number</span><span class="special"><</span><span class="identifier">mpfr_float_backend</span><span class="special"><</span><span class="number">50</span><span class="special">,</span> <span class="identifier">allocate_dynamic</span><span class="special">>,</span> 135 <span class="identifier">et_on</span><span class="special">></span></code> 136 </p> 137 </td> 138<td> 139 <p> 140 1.05 (5.8s) 141 </p> 142 </td> 143</tr> 144<tr> 145<td> 146 <p> 147 <code class="computeroutput"><span class="identifier">number</span><span class="special"><</span><span class="identifier">mpfr_float_backend</span><span class="special"><</span><span class="number">50</span><span class="special">,</span> <span class="identifier">allocate_dynamic</span><span class="special">>,</span> 148 <span class="identifier">et_off</span><span class="special">></span></code> 149 </p> 150 </td> 151<td> 152 <p> 153 1.16 (6.4s) 154 </p> 155 </td> 156</tr> 157</tbody> 158</table></div> 159<div class="note"><table border="0" summary="Note"> 160<tr> 161<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td> 162<th align="left">Note</th> 163</tr> 164<tr><td align="left" valign="top"><p> 165 This type only provides <code class="computeroutput"><span class="identifier">numeric_limits</span></code> 166 support when the precision is fixed at compile time. 167 </p></td></tr> 168</table></div> 169<p> 170 As well as the usual conversions from arithmetic and string types, instances 171 of <code class="computeroutput"><span class="identifier">number</span><span class="special"><</span><span class="identifier">mpfr_float_backend</span><span class="special"><</span><span class="identifier">N</span><span class="special">></span> <span class="special">></span></code> are copy constructible and assignable 172 from: 173 </p> 174<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 175<li class="listitem"> 176 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>, 177 <code class="computeroutput"><span class="identifier">mpq_t</span></code>. 178 </li> 179<li class="listitem"> 180 The <a href="http://www.mpfr.org" target="_top">MPFR</a> native type <code class="computeroutput"><span class="identifier">mpfr_t</span></code>. 181 </li> 182<li class="listitem"> 183 The <code class="computeroutput"><span class="identifier">number</span></code> wrappers 184 around those types: <code class="computeroutput"><span class="identifier">number</span><span class="special"><</span><span class="identifier">mpfr_float_backend</span><span class="special"><</span><span class="identifier">M</span><span class="special">></span> <span class="special">></span></code>, 185 <code class="computeroutput"><span class="identifier">number</span><span class="special"><</span><span class="identifier">mpf_float</span><span class="special"><</span><span class="identifier">M</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_int</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>. 186 </li> 187</ul></div> 188<p> 189 It's also possible to access the underlying <code class="computeroutput"><span class="identifier">mpfr_t</span></code> 190 via the data() member function of <code class="computeroutput"><span class="identifier">mpfr_float_backend</span></code>. 191 </p> 192<p> 193 Things you should know when using this type: 194 </p> 195<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 196<li class="listitem"> 197 A default constructed <code class="computeroutput"><span class="identifier">mpfr_float_backend</span></code> 198 is set to zero (<span class="emphasis"><em>Note that this is <span class="bold"><strong>not</strong></span> 199 the default <a href="http://www.mpfr.org" target="_top">MPFR</a> behavior</em></span>). 200 </li> 201<li class="listitem"> 202 All operations use round to nearest. 203 </li> 204<li class="listitem"> 205 No changes are made to <a href="http://gmplib.org" target="_top">GMP</a> or 206 <a href="http://www.mpfr.org" target="_top">MPFR</a> global settings, so this 207 type can coexist with existing <a href="http://www.mpfr.org" target="_top">MPFR</a> 208 or <a href="http://gmplib.org" target="_top">GMP</a> code. 209 </li> 210<li class="listitem"> 211 The code can equally use <a href="http://mpir.org/" target="_top">MPIR</a> 212 in place of <a href="http://gmplib.org" target="_top">GMP</a> - indeed that 213 is the preferred option on Win32. 214 </li> 215<li class="listitem"> 216 This backend supports rvalue-references and is move-aware, making instantiations 217 of <code class="computeroutput"><span class="identifier">number</span></code> on this backend 218 move aware. 219 </li> 220<li class="listitem"> 221 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> 222 being thrown if the string can not be interpreted as a valid floating-point 223 number. 224 </li> 225<li class="listitem"> 226 Division by zero results in an infinity. 227 </li> 228<li class="listitem"> 229 When using the variable precision type <code class="computeroutput"><span class="identifier">mpfr_float</span></code>, 230 then copy construction and assignment <span class="emphasis"><em>copies the precision 231 of the source variable</em></span>. Likewise move construction and assignment. 232 </li> 233<li class="listitem"> 234 When constructing the variable precision type <code class="computeroutput"><span class="identifier">mpfr_float</span></code> 235 you can specify two arguments to the constructor - the first is the 236 value to assign to the variable, the second is an unsigned integer 237 specifying the precision in decimal places. The <code class="computeroutput"><span class="identifier">assign</span></code> 238 member function similarly has a 2-argument overload taking the value 239 to assign and the precision. You can use this to preserve the precision 240 of the target variable using the somewhat arcane: <code class="computeroutput"><span class="identifier">a</span><span class="special">.</span><span class="identifier">assign</span><span class="special">(</span><span class="identifier">b</span><span class="special">,</span> <span class="identifier">a</span><span class="special">.</span><span class="identifier">precision</span><span class="special">())</span></code>, which assigns <code class="computeroutput"><span class="identifier">b</span></code> 241 to <code class="computeroutput"><span class="identifier">a</span></code> but preserves 242 the precision of <code class="computeroutput"><span class="identifier">a</span></code>. 243 </li> 244</ul></div> 245<h6> 246<a name="boost_multiprecision.tut.floats.mpfr_float.h0"></a> 247 <span class="phrase"><a name="boost_multiprecision.tut.floats.mpfr_float.mpfr_example"></a></span><a class="link" href="mpfr_float.html#boost_multiprecision.tut.floats.mpfr_float.mpfr_example"> MPFR example:</a> 248 </h6> 249<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">mpfr</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 250<span class="preprocessor">#include</span> <span class="special"><</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">></span> 251<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 252 253<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 254<span class="special">{</span> 255 <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> 256 257 <span class="comment">// Operations at variable precision and no numeric_limits support:</span> 258 <span class="identifier">mpfr_float</span> <span class="identifier">a</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span> 259 <span class="identifier">mpfr_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> 260 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">mpfr_float</span><span class="special">::</span><span class="identifier">default_precision</span><span class="special">()</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 261 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">sqrt</span><span class="special">(</span><span class="identifier">a</span><span class="special">)</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">// print root-2</span> 262 263 <span class="comment">// Operations at fixed precision and full numeric_limits support:</span> 264 <span class="identifier">mpfr_float_100</span> <span class="identifier">b</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span> 265 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="identifier">mpfr_float_100</span><span class="special">>::</span><span class="identifier">digits</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 266 <span class="comment">// We can use any C++ std lib function:</span> 267 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">log</span><span class="special">(</span><span class="identifier">b</span><span class="special">)</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">// print log(2)</span> 268 <span class="comment">// We can also use any function from Boost.Math:</span> 269 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</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"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 270 <span class="comment">// These even work when the argument is an expression template:</span> 271 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</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"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 272 273 <span class="comment">// Access the underlying data:</span> 274 <span class="identifier">mpfr_t</span> <span class="identifier">r</span><span class="special">;</span> 275 <span class="identifier">mpfr_init</span><span class="special">(</span><span class="identifier">r</span><span class="special">);</span> 276 <span class="identifier">mpfr_set</span><span class="special">(</span><span class="identifier">r</span><span class="special">,</span> <span class="identifier">b</span><span class="special">.</span><span class="identifier">backend</span><span class="special">().</span><span class="identifier">data</span><span class="special">(),</span> <span class="identifier">GMP_RNDN</span><span class="special">);</span> 277 <span class="identifier">mpfr_clear</span><span class="special">(</span><span class="identifier">r</span><span class="special">);</span> 278 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 279<span class="special">}</span> 280</pre> 281<div class="footnotes"> 282<br><hr style="width:100; text-align:left;margin-left: 0"> 283<div id="ftn.boost_multiprecision.tut.floats.mpfr_float.f0" class="footnote"><p><a href="#boost_multiprecision.tut.floats.mpfr_float.f0" class="para"><sup class="para">[2] </sup></a> 284 Compiled with VC++10 and /Ox, with MPFR-3.0.0 and MPIR-2.3.0 285 </p></div> 286</div> 287</div> 288<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 289<td align="left"></td> 290<td align="right"><div class="copyright-footer">Copyright © 2002-2020 John 291 Maddock and Christopher Kormanyos<p> 292 Distributed under the Boost Software License, Version 1.0. (See accompanying 293 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>) 294 </p> 295</div></td> 296</tr></table> 297<hr> 298<div class="spirit-nav"> 299<a accesskey="p" href="gmp_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="float128.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 300</div> 301</body> 302</html> 303