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.

213 lines
14 KiB
HTML

<!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 Floats (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 Floats (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-Random-Numbers.html#C_002b_002b-Interface-Random-Numbers" rel="next" title="C++ Interface Random Numbers">
<link href="C_002b_002b-Interface-Rationals.html#C_002b_002b-Interface-Rationals" rel="prev" title="C++ Interface Rationals">
<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-Floats"></a>
<div class="header">
<p>
Next: <a href="C_002b_002b-Interface-Random-Numbers.html#C_002b_002b-Interface-Random-Numbers" accesskey="n" rel="next">C++ Interface Random Numbers</a>, Previous: <a href="C_002b_002b-Interface-Rationals.html#C_002b_002b-Interface-Rationals" accesskey="p" rel="prev">C++ Interface Rationals</a>, Up: <a href="C_002b_002b-Class-Interface.html#C_002b_002b-Class-Interface" accesskey="u" rel="up">C++ Class Interface</a> &nbsp; [<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="C_002b_002b-Interface-Floats-1"></a>
<h3 class="section">12.4 C++ Interface Floats</h3>
<p>When an expression requires the use of temporary intermediate <code>mpf_class</code>
values, like <code>f=g*h+x*y</code>, those temporaries will have the same precision
as the destination <code>f</code>. Explicit constructors can be used if this
doesn&rsquo;t suit.
</p>
<dl>
<dt><a name="index-mpf_005fclass_003a_003ampf_005fclass"></a>Function: <em></em> <strong>mpf_class::mpf_class</strong> <em>(type <var>op</var>)</em></dt>
<dt><a name="index-mpf_005fclass_003a_003ampf_005fclass-1"></a>Function: <em></em> <strong>mpf_class::mpf_class</strong> <em>(type <var>op</var>, mp_bitcnt_t <var>prec</var>)</em></dt>
<dd><p>Construct an <code>mpf_class</code>. Any standard C++ type can be used, except
<code>long long</code> and <code>long double</code>, and any of the GMP C++ classes can be
used.
</p>
<p>If <var>prec</var> is given, the initial precision is that value, in bits. If
<var>prec</var> is not given, then the initial precision is determined by the type
of <var>op</var> given. An <code>mpz_class</code>, <code>mpq_class</code>, or C++
builtin type will give the default <code>mpf</code> precision (see <a href="Initializing-Floats.html#Initializing-Floats">Initializing Floats</a>). An <code>mpf_class</code> or expression will give the precision of that
value. The precision of a binary expression is the higher of the two
operands.
</p>
<div class="example">
<pre class="example">mpf_class f(1.5); // default precision
mpf_class f(1.5, 500); // 500 bits (at least)
mpf_class f(x); // precision of x
mpf_class f(abs(x)); // precision of x
mpf_class f(-g, 1000); // 1000 bits (at least)
mpf_class f(x+y); // greater of precisions of x and y
</pre></div>
</dd></dl>
<dl>
<dt><a name="index-mpf_005fclass_003a_003ampf_005fclass-2"></a>Function: <em>explicit</em> <strong>mpf_class::mpf_class</strong> <em>(const mpf_t <var>f</var>)</em></dt>
<dt><a name="index-mpf_005fclass_003a_003ampf_005fclass-3"></a>Function: <em></em> <strong>mpf_class::mpf_class</strong> <em>(const mpf_t <var>f</var>, mp_bitcnt_t <var>prec</var>)</em></dt>
<dd><p>Construct an <code>mpf_class</code> from an <code>mpf_t</code>. The value in <var>f</var> is
copied into the new <code>mpf_class</code>, there won&rsquo;t be any permanent association
between it and <var>f</var>.
</p>
<p>If <var>prec</var> is given, the initial precision is that value, in bits. If
<var>prec</var> is not given, then the initial precision is that of <var>f</var>.
</p></dd></dl>
<dl>
<dt><a name="index-mpf_005fclass_003a_003ampf_005fclass-4"></a>Function: <em>explicit</em> <strong>mpf_class::mpf_class</strong> <em>(const char *<var>s</var>)</em></dt>
<dt><a name="index-mpf_005fclass_003a_003ampf_005fclass-5"></a>Function: <em></em> <strong>mpf_class::mpf_class</strong> <em>(const char *<var>s</var>, mp_bitcnt_t <var>prec</var>, int <var>base</var> = 0)</em></dt>
<dt><a name="index-mpf_005fclass_003a_003ampf_005fclass-6"></a>Function: <em>explicit</em> <strong>mpf_class::mpf_class</strong> <em>(const string&amp; <var>s</var>)</em></dt>
<dt><a name="index-mpf_005fclass_003a_003ampf_005fclass-7"></a>Function: <em></em> <strong>mpf_class::mpf_class</strong> <em>(const string&amp; <var>s</var>, mp_bitcnt_t <var>prec</var>, int <var>base</var> = 0)</em></dt>
<dd><p>Construct an <code>mpf_class</code> converted from a string using <code>mpf_set_str</code>
(see <a href="Assigning-Floats.html#Assigning-Floats">Assigning Floats</a>). If <var>prec</var> is given, the initial precision is
that value, in bits. If not, the default <code>mpf</code> precision
(see <a href="Initializing-Floats.html#Initializing-Floats">Initializing Floats</a>) is used.
</p>
<p>If the string is not a valid float, 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-2"></a>Function: <em>mpf_class</em> <strong>operator&quot;&quot;</strong> <em>_mpf (const char *<var>str</var>)</em></dt>
<dd><p>With C++11 compilers, floats can be constructed with the syntax
<code>1.23e-1_mpf</code> which is equivalent to <code>mpf_class(&quot;1.23e-1&quot;)</code>.
</p></dd></dl>
<dl>
<dt><a name="index-mpf_005fclass_003a_003aoperator_003d"></a>Function: <em>mpf_class&amp;</em> <strong>mpf_class::operator=</strong> <em>(type <var>op</var>)</em></dt>
<dd><p>Convert and store the given <var>op</var> value to an <code>mpf_class</code> object. The
same types are accepted as for the constructors above.
</p>
<p>Note that <code>operator=</code> only stores a new value, it doesn&rsquo;t copy or change
the precision of the destination, instead the value is truncated if necessary.
This is the same as <code>mpf_set</code> etc. Note in particular this means for
<code>mpf_class</code> a copy constructor is not the same as a default constructor
plus assignment.
</p>
<div class="example">
<pre class="example">mpf_class x (y); // x created with precision of y
mpf_class x; // x created with default precision
x = y; // value truncated to that precision
</pre></div>
<p>Applications using templated code may need to be careful about the assumptions
the code makes in this area, when working with <code>mpf_class</code> values of
various different or non-default precisions. For instance implementations of
the standard <code>complex</code> template have been seen in both styles above,
though of course <code>complex</code> is normally only actually specified for use
with the builtin float types.
</p></dd></dl>
<dl>
<dt><a name="index-abs-2"></a>Function: <em>mpf_class</em> <strong>abs</strong> <em>(mpf_class <var>op</var>)</em></dt>
<dt><a name="index-ceil"></a>Function: <em>mpf_class</em> <strong>ceil</strong> <em>(mpf_class <var>op</var>)</em></dt>
<dt><a name="index-cmp-4"></a>Function: <em>int</em> <strong>cmp</strong> <em>(mpf_class <var>op1</var>, type <var>op2</var>)</em></dt>
<dt><a name="index-cmp-5"></a>Function: <em>int</em> <strong>cmp</strong> <em>(type <var>op1</var>, mpf_class <var>op2</var>)</em></dt>
<dt><a name="index-mpf_005fclass_003a_003afits_005fsint_005fp"></a>Function: <em>bool</em> <strong>mpf_class::fits_sint_p</strong> <em>(void)</em></dt>
<dt><a name="index-mpf_005fclass_003a_003afits_005fslong_005fp"></a>Function: <em>bool</em> <strong>mpf_class::fits_slong_p</strong> <em>(void)</em></dt>
<dt><a name="index-mpf_005fclass_003a_003afits_005fsshort_005fp"></a>Function: <em>bool</em> <strong>mpf_class::fits_sshort_p</strong> <em>(void)</em></dt>
<dt><a name="index-mpf_005fclass_003a_003afits_005fuint_005fp"></a>Function: <em>bool</em> <strong>mpf_class::fits_uint_p</strong> <em>(void)</em></dt>
<dt><a name="index-mpf_005fclass_003a_003afits_005fulong_005fp"></a>Function: <em>bool</em> <strong>mpf_class::fits_ulong_p</strong> <em>(void)</em></dt>
<dt><a name="index-mpf_005fclass_003a_003afits_005fushort_005fp"></a>Function: <em>bool</em> <strong>mpf_class::fits_ushort_p</strong> <em>(void)</em></dt>
<dt><a name="index-floor"></a>Function: <em>mpf_class</em> <strong>floor</strong> <em>(mpf_class <var>op</var>)</em></dt>
<dt><a name="index-hypot"></a>Function: <em>mpf_class</em> <strong>hypot</strong> <em>(mpf_class <var>op1</var>, mpf_class <var>op2</var>)</em></dt>
<dt><a name="index-mpf_005fclass_003a_003aget_005fd"></a>Function: <em>double</em> <strong>mpf_class::get_d</strong> <em>(void)</em></dt>
<dt><a name="index-mpf_005fclass_003a_003aget_005fsi"></a>Function: <em>long</em> <strong>mpf_class::get_si</strong> <em>(void)</em></dt>
<dt><a name="index-mpf_005fclass_003a_003aget_005fstr"></a>Function: <em>string</em> <strong>mpf_class::get_str</strong> <em>(mp_exp_t&amp; <var>exp</var>, int <var>base</var> = 10, size_t <var>digits</var> = 0)</em></dt>
<dt><a name="index-mpf_005fclass_003a_003aget_005fui"></a>Function: <em>unsigned long</em> <strong>mpf_class::get_ui</strong> <em>(void)</em></dt>
<dt><a name="index-mpf_005fclass_003a_003aset_005fstr"></a>Function: <em>int</em> <strong>mpf_class::set_str</strong> <em>(const char *<var>str</var>, int <var>base</var>)</em></dt>
<dt><a name="index-mpf_005fclass_003a_003aset_005fstr-1"></a>Function: <em>int</em> <strong>mpf_class::set_str</strong> <em>(const string&amp; <var>str</var>, int <var>base</var>)</em></dt>
<dt><a name="index-sgn-2"></a>Function: <em>int</em> <strong>sgn</strong> <em>(mpf_class <var>op</var>)</em></dt>
<dt><a name="index-sqrt-1"></a>Function: <em>mpf_class</em> <strong>sqrt</strong> <em>(mpf_class <var>op</var>)</em></dt>
<dt><a name="index-mpf_005fclass_003a_003aswap"></a>Function: <em>void</em> <strong>mpf_class::swap</strong> <em>(mpf_class&amp; <var>op</var>)</em></dt>
<dt><a name="index-swap-2"></a>Function: <em>void</em> <strong>swap</strong> <em>(mpf_class&amp; <var>op1</var>, mpf_class&amp; <var>op2</var>)</em></dt>
<dt><a name="index-trunc"></a>Function: <em>mpf_class</em> <strong>trunc</strong> <em>(mpf_class <var>op</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>
<p>The accuracy provided by <code>hypot</code> is not currently guaranteed.
</p></dd></dl>
<dl>
<dt><a name="index-mpf_005fclass_003a_003aget_005fprec"></a>Function: <em>mp_bitcnt_t</em> <strong>mpf_class::get_prec</strong> <em>()</em></dt>
<dt><a name="index-mpf_005fclass_003a_003aset_005fprec"></a>Function: <em>void</em> <strong>mpf_class::set_prec</strong> <em>(mp_bitcnt_t <var>prec</var>)</em></dt>
<dt><a name="index-mpf_005fclass_003a_003aset_005fprec_005fraw"></a>Function: <em>void</em> <strong>mpf_class::set_prec_raw</strong> <em>(mp_bitcnt_t <var>prec</var>)</em></dt>
<dd><p>Get or set the current precision of an <code>mpf_class</code>.
</p>
<p>The restrictions described for <code>mpf_set_prec_raw</code> (see <a href="Initializing-Floats.html#Initializing-Floats">Initializing Floats</a>) apply to <code>mpf_class::set_prec_raw</code>. Note in particular that the
<code>mpf_class</code> must be restored to it&rsquo;s allocated precision before being
destroyed. This must be done by application code, there&rsquo;s no automatic
mechanism for it.
</p></dd></dl>
<hr>
<div class="header">
<p>
Next: <a href="C_002b_002b-Interface-Random-Numbers.html#C_002b_002b-Interface-Random-Numbers" accesskey="n" rel="next">C++ Interface Random Numbers</a>, Previous: <a href="C_002b_002b-Interface-Rationals.html#C_002b_002b-Interface-Rationals" accesskey="p" rel="prev">C++ Interface Rationals</a>, Up: <a href="C_002b_002b-Class-Interface.html#C_002b_002b-Class-Interface" accesskey="u" rel="up">C++ Class Interface</a> &nbsp; [<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>