1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>gmp_rational</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="../rational.html" title="Rational Number Types"> 9<link rel="prev" href="cpp_rational.html" title="cpp_rational"> 10<link rel="next" href="tommath_rational.html" title="tommath_rational"> 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_rational.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../rational.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="tommath_rational.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.rational.gmp_rational"></a><a class="link" href="gmp_rational.html" title="gmp_rational">gmp_rational</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_rational</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_rational</span> <span class="special">></span> <span class="identifier">mpq_rational</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_rational</span></code> back-end 42 is 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">mpq_rational</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">mpq_t</span></code> to provide a rational 45 number type that is a drop-in replacement for the native C++ number types, 46 but with unlimited precision. 47 </p> 48<p> 49 As well as the usual conversions from arithmetic and string types, instances 50 of <code class="computeroutput"><span class="identifier">number</span><span class="special"><</span><span class="identifier">gmp_rational</span><span class="special">></span></code> 51 are copy constructible 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">mpz_t</span></code>, <code class="computeroutput"><span class="identifier">mpq_t</span></code>. 56 </li> 57<li class="listitem"> 58 <code class="computeroutput"><span class="identifier">number</span><span class="special"><</span><span class="identifier">gmp_int</span><span class="special">></span></code>. 59 </li> 60</ul></div> 61<p> 62 There is also a two-argument constructor that accepts a numerator and denominator 63 (both of type <code class="computeroutput"><span class="identifier">number</span><span class="special"><</span><span class="identifier">gmp_int</span><span class="special">></span></code>). 64 </p> 65<p> 66 There are also non-member functions: 67 </p> 68<pre class="programlisting"><span class="identifier">mpz_int</span> <span class="identifier">numerator</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">mpq_rational</span><span class="special">&);</span> 69<span class="identifier">mpz_int</span> <span class="identifier">denominator</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">mpq_rational</span><span class="special">&);</span> 70</pre> 71<p> 72 which return the numerator and denominator of the number. 73 </p> 74<p> 75 It's also possible to access the underlying <code class="computeroutput"><span class="identifier">mpq_t</span></code> 76 via the <code class="computeroutput"><span class="identifier">data</span><span class="special">()</span></code> 77 member function of <code class="computeroutput"><span class="identifier">mpq_rational</span></code>. 78 </p> 79<p> 80 Things you should know when using this type: 81 </p> 82<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 83<li class="listitem"> 84 Default constructed <code class="computeroutput"><span class="identifier">mpq_rational</span></code>s 85 have the value zero (this is the <a href="http://gmplib.org" target="_top">GMP</a> 86 default behavior). 87 </li> 88<li class="listitem"> 89 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> 90 being thrown. 91 </li> 92<li class="listitem"> 93 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> 94 being thrown if the string can not be interpreted as a valid rational 95 number. 96 </li> 97<li class="listitem"> 98 No changes are made to the <a href="http://gmplib.org" target="_top">GMP</a> 99 library's global settings, so this type can coexist with existing 100 <a href="http://gmplib.org" target="_top">GMP</a> code. 101 </li> 102<li class="listitem"> 103 The code can equally be used with <a href="http://mpir.org/" target="_top">MPIR</a> 104 as the underlying library - indeed that is the preferred option on 105 Win32. 106 </li> 107</ul></div> 108<h6> 109<a name="boost_multiprecision.tut.rational.gmp_rational.h0"></a> 110 <span class="phrase"><a name="boost_multiprecision.tut.rational.gmp_rational.example"></a></span><a class="link" href="gmp_rational.html#boost_multiprecision.tut.rational.gmp_rational.example">Example:</a> 111 </h6> 112<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> 113<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> 114<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 115 116<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 117<span class="special">{</span> 118 <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> 119 120 <span class="identifier">mpq_rational</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> 121 122 <span class="comment">// Do some arithmetic:</span> 123 <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> 124 <span class="identifier">v</span> <span class="special">*=</span> <span class="identifier">i</span><span class="special">;</span> 125 <span class="identifier">v</span> <span class="special">/=</span> <span class="number">10</span><span class="special">;</span> 126 127 <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! / 10</span> 128 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">numerator</span><span class="special">(</span><span class="identifier">v</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> 129 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">denominator</span><span class="special">(</span><span class="identifier">v</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> 130 131 <span class="identifier">mpq_rational</span> <span class="identifier">w</span><span class="special">(</span><span class="number">2</span><span class="special">,</span> <span class="number">3</span><span class="special">);</span> <span class="comment">// component wise constructor</span> 132 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">w</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 2/3</span> 133 134 <span class="comment">// Access the underlying data:</span> 135 <span class="identifier">mpq_t</span> <span class="identifier">q</span><span class="special">;</span> 136 <span class="identifier">mpq_init</span><span class="special">(</span><span class="identifier">q</span><span class="special">);</span> 137 <span class="identifier">mpq_set</span><span class="special">(</span><span class="identifier">q</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> 138 <span class="identifier">mpq_clear</span><span class="special">(</span><span class="identifier">q</span><span class="special">);</span> 139 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 140<span class="special">}</span> 141</pre> 142</div> 143<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 144<td align="left"></td> 145<td align="right"><div class="copyright-footer">Copyright © 2002-2020 John 146 Maddock and Christopher Kormanyos<p> 147 Distributed under the Boost Software License, Version 1.0. (See accompanying 148 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>) 149 </p> 150</div></td> 151</tr></table> 152<hr> 153<div class="spirit-nav"> 154<a accesskey="p" href="cpp_rational.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../rational.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="tommath_rational.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 155</div> 156</body> 157</html> 158