You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
181 lines
11 KiB
HTML
181 lines
11 KiB
HTML
4 years ago
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||
|
<html>
|
||
|
<!-- This manual describes how to install and use the GNU multiple precision
|
||
|
arithmetic library, version 6.1.0.
|
||
|
|
||
|
Copyright 1991, 1993-2015 Free Software Foundation, Inc.
|
||
|
|
||
|
Permission is granted to copy, distribute and/or modify this document under
|
||
|
the terms of the GNU Free Documentation License, Version 1.3 or any later
|
||
|
version published by the Free Software Foundation; with no Invariant Sections,
|
||
|
with the Front-Cover Texts being "A GNU Manual", and with the Back-Cover
|
||
|
Texts being "You have freedom to copy and modify this GNU Manual, like GNU
|
||
|
software". A copy of the license is included in
|
||
|
GNU Free Documentation License. -->
|
||
|
<!-- Created by GNU Texinfo 6.4, http://www.gnu.org/software/texinfo/ -->
|
||
|
<head>
|
||
|
<title>C++ Interface Rationals (GNU MP 6.1.0)</title>
|
||
|
|
||
|
<meta name="description" content="How to install and use the GNU multiple precision arithmetic library, version 6.1.0.">
|
||
|
<meta name="keywords" content="C++ Interface Rationals (GNU MP 6.1.0)">
|
||
|
<meta name="resource-type" content="document">
|
||
|
<meta name="distribution" content="global">
|
||
|
<meta name="Generator" content="makeinfo">
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||
|
<link href="index.html#Top" rel="start" title="Top">
|
||
|
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
|
||
|
<link href="C_002b_002b-Class-Interface.html#C_002b_002b-Class-Interface" rel="up" title="C++ Class Interface">
|
||
|
<link href="C_002b_002b-Interface-Floats.html#C_002b_002b-Interface-Floats" rel="next" title="C++ Interface Floats">
|
||
|
<link href="C_002b_002b-Interface-Integers.html#C_002b_002b-Interface-Integers" rel="prev" title="C++ Interface Integers">
|
||
|
<style type="text/css">
|
||
|
<!--
|
||
|
a.summary-letter {text-decoration: none}
|
||
|
blockquote.indentedblock {margin-right: 0em}
|
||
|
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
|
||
|
blockquote.smallquotation {font-size: smaller}
|
||
|
div.display {margin-left: 3.2em}
|
||
|
div.example {margin-left: 3.2em}
|
||
|
div.lisp {margin-left: 3.2em}
|
||
|
div.smalldisplay {margin-left: 3.2em}
|
||
|
div.smallexample {margin-left: 3.2em}
|
||
|
div.smalllisp {margin-left: 3.2em}
|
||
|
kbd {font-style: oblique}
|
||
|
pre.display {font-family: inherit}
|
||
|
pre.format {font-family: inherit}
|
||
|
pre.menu-comment {font-family: serif}
|
||
|
pre.menu-preformatted {font-family: serif}
|
||
|
pre.smalldisplay {font-family: inherit; font-size: smaller}
|
||
|
pre.smallexample {font-size: smaller}
|
||
|
pre.smallformat {font-family: inherit; font-size: smaller}
|
||
|
pre.smalllisp {font-size: smaller}
|
||
|
span.nolinebreak {white-space: nowrap}
|
||
|
span.roman {font-family: initial; font-weight: normal}
|
||
|
span.sansserif {font-family: sans-serif; font-weight: normal}
|
||
|
ul.no-bullet {list-style: none}
|
||
|
-->
|
||
|
</style>
|
||
|
|
||
|
|
||
|
</head>
|
||
|
|
||
|
<body lang="en">
|
||
|
<a name="C_002b_002b-Interface-Rationals"></a>
|
||
|
<div class="header">
|
||
|
<p>
|
||
|
Next: <a href="C_002b_002b-Interface-Floats.html#C_002b_002b-Interface-Floats" accesskey="n" rel="next">C++ Interface Floats</a>, Previous: <a href="C_002b_002b-Interface-Integers.html#C_002b_002b-Interface-Integers" accesskey="p" rel="prev">C++ Interface Integers</a>, Up: <a href="C_002b_002b-Class-Interface.html#C_002b_002b-Class-Interface" accesskey="u" rel="up">C++ Class Interface</a> [<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
||
|
</div>
|
||
|
<hr>
|
||
|
<a name="C_002b_002b-Interface-Rationals-1"></a>
|
||
|
<h3 class="section">12.3 C++ Interface Rationals</h3>
|
||
|
|
||
|
<p>In all the following constructors, if a fraction is given then it should be in
|
||
|
canonical form, or if not then <code>mpq_class::canonicalize</code> called.
|
||
|
</p>
|
||
|
<dl>
|
||
|
<dt><a name="index-mpq_005fclass_003a_003ampq_005fclass"></a>Function: <em></em> <strong>mpq_class::mpq_class</strong> <em>(type <var>op</var>)</em></dt>
|
||
|
<dt><a name="index-mpq_005fclass_003a_003ampq_005fclass-1"></a>Function: <em></em> <strong>mpq_class::mpq_class</strong> <em>(integer <var>num</var>, integer <var>den</var>)</em></dt>
|
||
|
<dd><p>Construct an <code>mpq_class</code>. The initial value can be a single value of any
|
||
|
type (conversion from <code>mpf_class</code> is <code>explicit</code>), or a pair of
|
||
|
integers (<code>mpz_class</code> or standard C++ integer types) representing a
|
||
|
fraction, except that <code>long long</code> and <code>long double</code> are not
|
||
|
supported. For example,
|
||
|
</p>
|
||
|
<div class="example">
|
||
|
<pre class="example">mpq_class q (99);
|
||
|
mpq_class q (1.75);
|
||
|
mpq_class q (1, 3);
|
||
|
</pre></div>
|
||
|
</dd></dl>
|
||
|
|
||
|
<dl>
|
||
|
<dt><a name="index-mpq_005fclass_003a_003ampq_005fclass-2"></a>Function: <em>explicit</em> <strong>mpq_class::mpq_class</strong> <em>(const mpq_t <var>q</var>)</em></dt>
|
||
|
<dd><p>Construct an <code>mpq_class</code> from an <code>mpq_t</code>. The value in <var>q</var> is
|
||
|
copied into the new <code>mpq_class</code>, there won’t be any permanent association
|
||
|
between it and <var>q</var>.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
<dl>
|
||
|
<dt><a name="index-mpq_005fclass_003a_003ampq_005fclass-3"></a>Function: <em>explicit</em> <strong>mpq_class::mpq_class</strong> <em>(const char *<var>s</var>, int <var>base</var> = 0)</em></dt>
|
||
|
<dt><a name="index-mpq_005fclass_003a_003ampq_005fclass-4"></a>Function: <em>explicit</em> <strong>mpq_class::mpq_class</strong> <em>(const string& <var>s</var>, int <var>base</var> = 0)</em></dt>
|
||
|
<dd><p>Construct an <code>mpq_class</code> converted from a string using <code>mpq_set_str</code>
|
||
|
(see <a href="Initializing-Rationals.html#Initializing-Rationals">Initializing Rationals</a>).
|
||
|
</p>
|
||
|
<p>If the string is not a valid rational, an <code>std::invalid_argument</code>
|
||
|
exception is thrown. The same applies to <code>operator=</code>.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
<dl>
|
||
|
<dt><a name="index-operator_0022_0022-1"></a>Function: <em>mpq_class</em> <strong>operator""</strong> <em>_mpq (const char *<var>str</var>)</em></dt>
|
||
|
<dd><p>With C++11 compilers, integral rationals can be constructed with the syntax
|
||
|
<code>123_mpq</code> which is equivalent to <code>mpq_class(123_mpz)</code>. Other
|
||
|
rationals can be built as <code>-1_mpq/2</code> or <code>0xb_mpq/123456_mpz</code>.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
<dl>
|
||
|
<dt><a name="index-mpq_005fclass_003a_003acanonicalize"></a>Function: <em>void</em> <strong>mpq_class::canonicalize</strong> <em>()</em></dt>
|
||
|
<dd><p>Put an <code>mpq_class</code> into canonical form, as per <a href="Rational-Number-Functions.html#Rational-Number-Functions">Rational Number Functions</a>. All arithmetic operators require their operands in canonical
|
||
|
form, and will return results in canonical form.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
<dl>
|
||
|
<dt><a name="index-abs-1"></a>Function: <em>mpq_class</em> <strong>abs</strong> <em>(mpq_class <var>op</var>)</em></dt>
|
||
|
<dt><a name="index-cmp-2"></a>Function: <em>int</em> <strong>cmp</strong> <em>(mpq_class <var>op1</var>, type <var>op2</var>)</em></dt>
|
||
|
<dt><a name="index-cmp-3"></a>Function: <em>int</em> <strong>cmp</strong> <em>(type <var>op1</var>, mpq_class <var>op2</var>)</em></dt>
|
||
|
<dt><a name="index-mpq_005fclass_003a_003aget_005fd"></a>Function: <em>double</em> <strong>mpq_class::get_d</strong> <em>(void)</em></dt>
|
||
|
<dt><a name="index-mpq_005fclass_003a_003aget_005fstr"></a>Function: <em>string</em> <strong>mpq_class::get_str</strong> <em>(int <var>base</var> = 10)</em></dt>
|
||
|
<dt><a name="index-mpq_005fclass_003a_003aset_005fstr"></a>Function: <em>int</em> <strong>mpq_class::set_str</strong> <em>(const char *<var>str</var>, int <var>base</var>)</em></dt>
|
||
|
<dt><a name="index-mpq_005fclass_003a_003aset_005fstr-1"></a>Function: <em>int</em> <strong>mpq_class::set_str</strong> <em>(const string& <var>str</var>, int <var>base</var>)</em></dt>
|
||
|
<dt><a name="index-sgn-1"></a>Function: <em>int</em> <strong>sgn</strong> <em>(mpq_class <var>op</var>)</em></dt>
|
||
|
<dt><a name="index-mpq_005fclass_003a_003aswap"></a>Function: <em>void</em> <strong>mpq_class::swap</strong> <em>(mpq_class& <var>op</var>)</em></dt>
|
||
|
<dt><a name="index-swap-1"></a>Function: <em>void</em> <strong>swap</strong> <em>(mpq_class& <var>op1</var>, mpq_class& <var>op2</var>)</em></dt>
|
||
|
<dd><p>These functions provide a C++ class interface to the corresponding GMP C
|
||
|
routines.
|
||
|
</p>
|
||
|
<p><code>cmp</code> can be used with any of the classes or the standard C++ types,
|
||
|
except <code>long long</code> and <code>long double</code>.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
<dl>
|
||
|
<dt><a name="index-mpq_005fclass_003a_003aget_005fnum"></a>Function: <em>mpz_class&</em> <strong>mpq_class::get_num</strong> <em>()</em></dt>
|
||
|
<dt><a name="index-mpq_005fclass_003a_003aget_005fden"></a>Function: <em>mpz_class&</em> <strong>mpq_class::get_den</strong> <em>()</em></dt>
|
||
|
<dd><p>Get a reference to an <code>mpz_class</code> which is the numerator or denominator
|
||
|
of an <code>mpq_class</code>. This can be used both for read and write access. If
|
||
|
the object returned is modified, it modifies the original <code>mpq_class</code>.
|
||
|
</p>
|
||
|
<p>If direct manipulation might produce a non-canonical value, then
|
||
|
<code>mpq_class::canonicalize</code> must be called before further operations.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
<dl>
|
||
|
<dt><a name="index-mpq_005fclass_003a_003aget_005fnum_005fmpz_005ft"></a>Function: <em>mpz_t</em> <strong>mpq_class::get_num_mpz_t</strong> <em>()</em></dt>
|
||
|
<dt><a name="index-mpq_005fclass_003a_003aget_005fden_005fmpz_005ft"></a>Function: <em>mpz_t</em> <strong>mpq_class::get_den_mpz_t</strong> <em>()</em></dt>
|
||
|
<dd><p>Get a reference to the underlying <code>mpz_t</code> numerator or denominator of an
|
||
|
<code>mpq_class</code>. This can be passed to C functions expecting an
|
||
|
<code>mpz_t</code>. Any modifications made to the <code>mpz_t</code> will modify the
|
||
|
original <code>mpq_class</code>.
|
||
|
</p>
|
||
|
<p>If direct manipulation might produce a non-canonical value, then
|
||
|
<code>mpq_class::canonicalize</code> must be called before further operations.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
<dl>
|
||
|
<dt><a name="index-operator_003e_003e-3"></a>Function: <em>istream&</em> <strong>operator>></strong> <em>(istream& <var>stream</var>, mpq_class& <var>rop</var>);</em></dt>
|
||
|
<dd><p>Read <var>rop</var> from <var>stream</var>, using its <code>ios</code> formatting settings,
|
||
|
the same as <code>mpq_t operator>></code> (see <a href="C_002b_002b-Formatted-Input.html#C_002b_002b-Formatted-Input">C++ Formatted Input</a>).
|
||
|
</p>
|
||
|
<p>If the <var>rop</var> read might not be in canonical form then
|
||
|
<code>mpq_class::canonicalize</code> must be called.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
|
||
|
<hr>
|
||
|
<div class="header">
|
||
|
<p>
|
||
|
Next: <a href="C_002b_002b-Interface-Floats.html#C_002b_002b-Interface-Floats" accesskey="n" rel="next">C++ Interface Floats</a>, Previous: <a href="C_002b_002b-Interface-Integers.html#C_002b_002b-Interface-Integers" accesskey="p" rel="prev">C++ Interface Integers</a>, Up: <a href="C_002b_002b-Class-Interface.html#C_002b_002b-Class-Interface" accesskey="u" rel="up">C++ Class Interface</a> [<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
||
|
</div>
|
||
|
|
||
|
|
||
|
|
||
|
</body>
|
||
|
</html>
|