math/doc/html/math_toolkit/high_precision/use_mpfr.html
2019-10-31 17:55:35 +00:00

111 lines
11 KiB
HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Using With MPFR or GMP - High-Precision Floating-Point Library</title>
<link rel="stylesheet" href="../../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Math Toolkit 2.11.0">
<link rel="up" href="../high_precision.html" title="Using Boost.Math with High-Precision Floating-Point Libraries">
<link rel="prev" href="float128.html" title="Using with GCC's __float128 datatype">
<link rel="next" href="e_float.html" title="Using e_float Library">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<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>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<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
or GMP - High-Precision Floating-Point Library</a>
</h3></div></div></div>
<p>
The special functions and tools in this library can be used with <a href="http://www.mpfr.org" target="_top">MPFR</a>
(an arbitrary precision number type based on the <a href="http://gmplib.org/" target="_top">GNU
Multiple Precision Arithmetic Library</a>), either via the bindings in
<a href="../../../../../../boost/math/bindings/mpfr.hpp" target="_top">boost/math/bindings/mpfr.hpp</a>,
or via <a href="../../../../../../boost/math/bindings/mpfr.hpp" target="_top">boost/math/bindings/mpreal.hpp</a>.
</p>
<p>
<span class="bold"><strong>New projects are recommended to use <a href="../../../../../../libs/multiprecision/doc/html/index.html" target="_top">Boost.Multiprecision</a>
with GMP/MPFR backend instead.</strong></span>
</p>
<p>
In order to use these bindings you will need to have installed <a href="http://www.mpfr.org" target="_top">MPFR</a>
plus its dependency the <a href="http://gmplib.org" target="_top">GMP library</a>.
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>,
or <a href="http://www.holoborodko.com/pavel/mpfr/" target="_top">mpfr-C++ (mpreal)</a>.
</p>
<p>
Unfortunately neither <code class="computeroutput"><span class="identifier">mpfr_class</span></code>
nor <code class="computeroutput"><span class="identifier">mpreal</span></code> quite satisfy
our conceptual requirements, so there is a very thin set of additional interfaces
and some helper traits defined in <a href="../../../../../../boost/math/bindings/mpfr.hpp" target="_top">boost/math/bindings/mpfr.hpp</a>
and <a href="../../../../../../boost/math/bindings/mpreal.hpp" target="_top">boost/math/bindings/mpreal.hpp</a>
that you should use in place of including 'gmpfrxx.h' or 'mpreal.h' directly.
The classes <code class="computeroutput"><span class="identifier">mpfr_class</span></code> or
<code class="computeroutput"><span class="identifier">mpreal</span></code> are then usable unchanged
once this header is included, so for example <code class="computeroutput"><span class="identifier">mpfr_class</span></code>'s
performance-enhancing expression templates are preserved and fully supported
by this library:
</p>
<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>
<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>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
<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>
<span class="comment">//</span>
<span class="comment">// Note that the argument to tgamma is</span>
<span class="comment">// an expression template - that's just fine here.</span>
<span class="comment">//</span>
<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>
<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>
<span class="special">}</span>
</pre>
<p>
Alternatively use with <code class="computeroutput"><span class="identifier">mpreal</span></code>
would look like:
</p>
<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>
<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>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</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="identifier">set_precision</span><span class="special">(</span><span class="number">500</span><span class="special">);</span> <span class="comment">// 500 bit precision</span>
<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>
<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>
<span class="special">}</span>
</pre>
<p>
There is a concept checking test program for mpfr support <a href="../../../../../../libs/math/test/mpfr_concept_check.cpp" target="_top">here</a>
and <a href="../../../../../../libs/math/test/mpreal_concept_check.cpp" target="_top">here</a>.
</p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2006-2019 Nikhar
Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
R&#229;de, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
Daryle Walker and Xiaogang Zhang<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
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>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<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>
</div>
</body>
</html>