• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Using With MPFR or GMP - High-Precision Floating-Point Library</title>
5<link rel="stylesheet" href="../../math.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../index.html" title="Math Toolkit 2.12.0">
8<link rel="up" href="../high_precision.html" title="Using Boost.Math with High-Precision Floating-Point Libraries">
9<link rel="prev" href="float128.html" title="Using with GCC's __float128 datatype">
10<link rel="next" href="e_float.html" title="Using e_float Library">
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="float128.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_precision.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="e_float.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="math_toolkit.high_precision.use_mpfr"></a><a class="link" href="use_mpfr.html" title="Using With MPFR or GMP - High-Precision Floating-Point Library">Using With MPFR
28      or GMP - High-Precision Floating-Point Library</a>
29</h3></div></div></div>
30<p>
31        The special functions and tools in this library can be used with <a href="http://www.mpfr.org" target="_top">MPFR</a>
32        (an arbitrary precision number type based on the <a href="http://gmplib.org/" target="_top">GNU
33        Multiple Precision Arithmetic Library</a>), either via the bindings in
34        <a href="../../../../../../boost/math/bindings/mpfr.hpp" target="_top">boost/math/bindings/mpfr.hpp</a>,
35        or via <a href="../../../../../../boost/math/bindings/mpfr.hpp" target="_top">boost/math/bindings/mpreal.hpp</a>.
36      </p>
37<p>
38        <span class="bold"><strong>New projects are recommended to use <a href="../../../../../../libs/multiprecision/doc/html/index.html" target="_top">Boost.Multiprecision</a>
39        with GMP/MPFR backend instead.</strong></span>
40      </p>
41<p>
42        In order to use these bindings you will need to have installed <a href="http://www.mpfr.org" target="_top">MPFR</a>
43        plus its dependency the <a href="http://gmplib.org" target="_top">GMP library</a>.
44        You will also need one of the two supported C++ wrappers for MPFR: <a href="http://math.berkeley.edu/~wilken/code/gmpfrxx/" target="_top">gmpfrxx (or mpfr_class)</a>,
45        or <a href="http://www.holoborodko.com/pavel/mpfr/" target="_top">mpfr-C++ (mpreal)</a>.
46      </p>
47<p>
48        Unfortunately neither <code class="computeroutput"><span class="identifier">mpfr_class</span></code>
49        nor <code class="computeroutput"><span class="identifier">mpreal</span></code> quite satisfy
50        our conceptual requirements, so there is a very thin set of additional interfaces
51        and some helper traits defined in <a href="../../../../../../boost/math/bindings/mpfr.hpp" target="_top">boost/math/bindings/mpfr.hpp</a>
52        and <a href="../../../../../../boost/math/bindings/mpreal.hpp" target="_top">boost/math/bindings/mpreal.hpp</a>
53        that you should use in place of including 'gmpfrxx.h' or 'mpreal.h' directly.
54        The classes <code class="computeroutput"><span class="identifier">mpfr_class</span></code> or
55        <code class="computeroutput"><span class="identifier">mpreal</span></code> are then usable unchanged
56        once this header is included, so for example <code class="computeroutput"><span class="identifier">mpfr_class</span></code>'s
57        performance-enhancing expression templates are preserved and fully supported
58        by this library:
59      </p>
60<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">math</span><span class="special">/</span><span class="identifier">bindings</span><span class="special">/</span><span class="identifier">mpfr</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
61<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>
62
63<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
64<span class="special">{</span>
65   <span class="identifier">mpfr_class</span><span class="special">::</span><span class="identifier">set_dprec</span><span class="special">(</span><span class="number">500</span><span class="special">);</span> <span class="comment">// 500 bit precision</span>
66   <span class="comment">//</span>
67   <span class="comment">// Note that the argument to tgamma is</span>
68   <span class="comment">// an expression template - that's just fine here.</span>
69   <span class="comment">//</span>
70   <span class="identifier">mpfr_class</span> <span class="identifier">v</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">sqrt</span><span class="special">(</span><span class="identifier">mpfr_class</span><span class="special">(</span><span class="number">2</span><span class="special">)));</span>
71   <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">setprecision</span><span class="special">(</span><span class="number">50</span><span class="special">)</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>
72<span class="special">}</span>
73</pre>
74<p>
75        Alternatively use with <code class="computeroutput"><span class="identifier">mpreal</span></code>
76        would look like:
77      </p>
78<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">math</span><span class="special">/</span><span class="identifier">bindings</span><span class="special">/</span><span class="identifier">mpreal</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
79<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>
80
81<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
82<span class="special">{</span>
83   <span class="identifier">mpfr</span><span class="special">::</span><span class="identifier">mpreal</span><span class="special">::</span><span class="identifier">set_precision</span><span class="special">(</span><span class="number">500</span><span class="special">);</span> <span class="comment">// 500 bit precision</span>
84   <span class="identifier">mpfr</span><span class="special">::</span><span class="identifier">mpreal</span> <span class="identifier">v</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">sqrt</span><span class="special">(</span><span class="identifier">mpfr</span><span class="special">::</span><span class="identifier">mpreal</span><span class="special">(</span><span class="number">2</span><span class="special">)));</span>
85   <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">setprecision</span><span class="special">(</span><span class="number">50</span><span class="special">)</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>
86<span class="special">}</span>
87</pre>
88<p>
89        There is a concept checking test program for mpfr support <a href="../../../../../../libs/math/test/mpfr_concept_check.cpp" target="_top">here</a>
90        and <a href="../../../../../../libs/math/test/mpreal_concept_check.cpp" target="_top">here</a>.
91      </p>
92</div>
93<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
94<td align="left"></td>
95<td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar
96      Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
97      Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
98      Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
99      Daryle Walker and Xiaogang Zhang<p>
100        Distributed under the Boost Software License, Version 1.0. (See accompanying
101        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>)
102      </p>
103</div></td>
104</tr></table>
105<hr>
106<div class="spirit-nav">
107<a accesskey="p" href="float128.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../high_precision.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="e_float.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
108</div>
109</body>
110</html>
111