125 lines
11 KiB
HTML
125 lines
11 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>cpp_rational</title>
|
|
<link rel="stylesheet" href="../../../multiprecision.css" type="text/css">
|
|
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
|
|
<link rel="home" href="../../../index.html" title="Chapter 1. Boost.Multiprecision">
|
|
<link rel="up" href="../rational.html" title="Rational Number Types">
|
|
<link rel="prev" href="../rational.html" title="Rational Number Types">
|
|
<link rel="next" href="gmp_rational.html" title="gmp_rational">
|
|
</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="../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>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="boost_multiprecision.tut.rational.cpp_rational"></a><a class="link" href="cpp_rational.html" title="cpp_rational">cpp_rational</a>
|
|
</h4></div></div></div>
|
|
<p>
|
|
<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>
|
|
</p>
|
|
<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>
|
|
|
|
<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>
|
|
|
|
<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>
|
|
|
|
<span class="special">}}</span> <span class="comment">// namespaces</span>
|
|
</pre>
|
|
<p>
|
|
The <code class="computeroutput"><span class="identifier">cpp_rational_backend</span></code>
|
|
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>.
|
|
It provides a rational number type that is a drop-in replacement for the
|
|
native C++ number types, but with unlimited precision.
|
|
</p>
|
|
<p>
|
|
As well as the usual conversions from arithmetic and string types, instances
|
|
of <code class="computeroutput"><span class="identifier">cpp_rational</span></code> are copy
|
|
constructible and assignable from type <code class="computeroutput"><span class="identifier">cpp_int</span></code>.
|
|
</p>
|
|
<p>
|
|
There is also a two argument constructor that accepts a numerator and denominator:
|
|
both of type <code class="computeroutput"><span class="identifier">cpp_int</span></code>.
|
|
</p>
|
|
<p>
|
|
There are also non-member functions:
|
|
</p>
|
|
<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>
|
|
<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>
|
|
</pre>
|
|
<p>
|
|
which return the numerator and denominator of the number.
|
|
</p>
|
|
<p>
|
|
Things you should know when using this type:
|
|
</p>
|
|
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
|
<li class="listitem">
|
|
Default constructed <code class="computeroutput"><span class="identifier">cpp_rational</span></code>s
|
|
have the value zero.
|
|
</li>
|
|
<li class="listitem">
|
|
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>
|
|
being thrown.
|
|
</li>
|
|
<li class="listitem">
|
|
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>
|
|
being thrown if the string can not be interpreted as a valid rational
|
|
number.
|
|
</li>
|
|
</ul></div>
|
|
<h6>
|
|
<a name="boost_multiprecision.tut.rational.cpp_rational.h0"></a>
|
|
<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>
|
|
</h6>
|
|
<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>
|
|
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span>
|
|
|
|
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
|
|
<span class="special">{</span>
|
|
<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>
|
|
|
|
<span class="identifier">cpp_rational</span> <span class="identifier">v</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span>
|
|
|
|
<span class="comment">// Do some arithmetic:</span>
|
|
<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>
|
|
<span class="identifier">v</span> <span class="special">*=</span> <span class="identifier">i</span><span class="special">;</span>
|
|
<span class="identifier">v</span> <span class="special">/=</span> <span class="number">10</span><span class="special">;</span>
|
|
|
|
<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>
|
|
<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>
|
|
<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>
|
|
|
|
<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>
|
|
<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>
|
|
<span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
|
|
<span class="special">}</span>
|
|
</pre>
|
|
</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 © 2002-2019 John Maddock
|
|
and Christopher Kormanyos<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="../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>
|
|
</div>
|
|
</body>
|
|
</html>
|