• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>tommath_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="gmp_rational.html" title="gmp_rational">
10<link rel="next" href="br.html" title="Use With Boost.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="gmp_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="br.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.tommath_rational"></a><a class="link" href="tommath_rational.html" title="tommath_rational">tommath_rational</a>
28</h4></div></div></div>
29<p>
30          <code class="computeroutput"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">tommath</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</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_adpater</span><span class="special">&lt;</span><span class="identifier">tommath_int</span><span class="special">&gt;</span>        <span class="identifier">tommath_rational</span><span class="special">;</span>
35<span class="keyword">typedef</span> <span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">tommath_rational</span> <span class="special">&gt;</span>         <span class="identifier">tom_rational</span><span class="special">;</span>
36
37<span class="special">}}</span> <span class="comment">// namespaces</span>
38</pre>
39<p>
40          The <code class="computeroutput"><span class="identifier">tommath_rational</span></code> back-end
41          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">tom_rational</span></code>.
42          It acts as a thin wrapper around <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">rational</span><span class="special">&lt;</span><span class="identifier">tom_int</span><span class="special">&gt;</span></code> to provide a rational number type that
43          is a drop-in replacement for the native C++ number types, but with unlimited
44          precision.
45        </p>
46<p>
47          The advantage of using this type rather than <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">rational</span><span class="special">&lt;</span><span class="identifier">tom_int</span><span class="special">&gt;</span></code> directly, is that it is expression-template
48          enabled, greatly reducing the number of temporaries created in complex
49          expressions.
50        </p>
51<p>
52          There are also non-member functions:
53        </p>
54<pre class="programlisting"><span class="identifier">tom_int</span> <span class="identifier">numerator</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">tom_rational</span><span class="special">&amp;);</span>
55<span class="identifier">tom_int</span> <span class="identifier">denominator</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">tom_rational</span><span class="special">&amp;);</span>
56</pre>
57<p>
58          which return the numerator and denominator of the number.
59        </p>
60<p>
61          Things you should know when using this type:
62        </p>
63<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
64<li class="listitem">
65              Default constructed <code class="computeroutput"><span class="identifier">tom_rational</span></code>s
66              have the value zero (this the inherited Boost.Rational behavior).
67            </li>
68<li class="listitem">
69              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>
70              being thrown.
71            </li>
72<li class="listitem">
73              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>
74              being thrown if the string can not be interpreted as a valid rational
75              number.
76            </li>
77<li class="listitem">
78              No changes are made to <a href="http://libtom.net" target="_top">libtommath</a>'s
79              global state, so this type can safely coexist with other <a href="http://libtom.net" target="_top">libtommath</a>
80              code.
81            </li>
82<li class="listitem">
83              Performance of this type has been found to be pretty poor - this need
84              further investigation - but it appears that Boost.Rational needs some
85              improvement in this area.
86            </li>
87</ul></div>
88<h6>
89<a name="boost_multiprecision.tut.rational.tommath_rational.h0"></a>
90          <span class="phrase"><a name="boost_multiprecision.tut.rational.tommath_rational.example"></a></span><a class="link" href="tommath_rational.html#boost_multiprecision.tut.rational.tommath_rational.example">Example:</a>
91        </h6>
92<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">multiprecision</span><span class="special">/</span><span class="identifier">tommath</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
93<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
94
95<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
96<span class="special">{</span>
97   <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>
98
99   <span class="identifier">tom_rational</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span>
100
101   <span class="comment">// Do some arithmetic:</span>
102   <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">&lt;=</span> <span class="number">1000</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span>
103      <span class="identifier">v</span> <span class="special">*=</span> <span class="identifier">i</span><span class="special">;</span>
104   <span class="identifier">v</span> <span class="special">/=</span> <span class="number">10</span><span class="special">;</span>
105
106   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</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="comment">// prints 1000! / 10</span>
107   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">numerator</span><span class="special">(</span><span class="identifier">v</span><span class="special">)</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>
108   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">denominator</span><span class="special">(</span><span class="identifier">v</span><span class="special">)</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>
109
110   <span class="identifier">tom_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>
111   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">w</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="comment">// prints 2/3</span>
112
113   <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
114<span class="special">}</span>
115</pre>
116</div>
117<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
118<td align="left"></td>
119<td align="right"><div class="copyright-footer">Copyright © 2002-2020 John
120      Maddock and Christopher Kormanyos<p>
121        Distributed under the Boost Software License, Version 1.0. (See accompanying
122        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>)
123      </p>
124</div></td>
125</tr></table>
126<hr>
127<div class="spirit-nav">
128<a accesskey="p" href="gmp_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="br.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
129</div>
130</body>
131</html>
132