1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>cpp_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="../rational.html" title="Rational Number Types"> 10<link rel="next" href="gmp_rational.html" title="gmp_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="../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="gmp_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.cpp_rational"></a><a class="link" href="cpp_rational.html" title="cpp_rational">cpp_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">cpp_int</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">typedef</span> <span class="identifier">rational_adaptor</span><span class="special"><</span><span class="identifier">cpp_int_backend</span><span class="special"><></span> <span class="special">></span> <span class="identifier">cpp_rational_backend</span><span class="special">;</span> 35 36<span class="keyword">typedef</span> <span class="identifier">number</span><span class="special"><</span><span class="identifier">cpp_rational_backend</span><span class="special">></span> <span class="identifier">cpp_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">cpp_rational_backend</span></code> 42 type 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">cpp_rational</span></code>. 43 It provides a rational number type that is a drop-in replacement for the 44 native C++ number types, but with unlimited precision. 45 </p> 46<p> 47 As well as the usual conversions from arithmetic and string types, instances 48 of <code class="computeroutput"><span class="identifier">cpp_rational</span></code> are copy 49 constructible and assignable from type <code class="computeroutput"><span class="identifier">cpp_int</span></code>. 50 </p> 51<p> 52 There is also a two argument constructor that accepts a numerator and denominator: 53 both of type <code class="computeroutput"><span class="identifier">cpp_int</span></code>. 54 </p> 55<p> 56 There are also non-member functions: 57 </p> 58<pre class="programlisting"><span class="identifier">cpp_int</span> <span class="identifier">numerator</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">cpp_rational</span><span class="special">&);</span> 59<span class="identifier">cpp_int</span> <span class="identifier">denominator</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">cpp_rational</span><span class="special">&);</span> 60</pre> 61<p> 62 which return the numerator and denominator of the number. 63 </p> 64<p> 65 Things you should know when using this type: 66 </p> 67<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 68<li class="listitem"> 69 Default constructed <code class="computeroutput"><span class="identifier">cpp_rational</span></code>s 70 have the value zero. 71 </li> 72<li class="listitem"> 73 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> 74 being thrown. 75 </li> 76<li class="listitem"> 77 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> 78 being thrown if the string can not be interpreted as a valid rational 79 number. 80 </li> 81</ul></div> 82<h6> 83<a name="boost_multiprecision.tut.rational.cpp_rational.h0"></a> 84 <span class="phrase"><a name="boost_multiprecision.tut.rational.cpp_rational.example"></a></span><a class="link" href="cpp_rational.html#boost_multiprecision.tut.rational.cpp_rational.example">Example:</a> 85 </h6> 86<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">cpp_int</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 87<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 88 89<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> 90<span class="special">{</span> 91 <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> 92 93 <span class="identifier">cpp_rational</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> 94 95 <span class="comment">// Do some arithmetic:</span> 96 <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> 97 <span class="identifier">v</span> <span class="special">*=</span> <span class="identifier">i</span><span class="special">;</span> 98 <span class="identifier">v</span> <span class="special">/=</span> <span class="number">10</span><span class="special">;</span> 99 100 <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> 101 <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> 102 <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> 103 104 <span class="identifier">cpp_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> 105 <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> 106 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 107<span class="special">}</span> 108</pre> 109</div> 110<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 111<td align="left"></td> 112<td align="right"><div class="copyright-footer">Copyright © 2002-2020 John 113 Maddock and Christopher Kormanyos<p> 114 Distributed under the Boost Software License, Version 1.0. (See accompanying 115 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>) 116 </p> 117</div></td> 118</tr></table> 119<hr> 120<div class="spirit-nav"> 121<a accesskey="p" href="../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="gmp_rational.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 122</div> 123</body> 124</html> 125