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.

155 lines
8.4 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>Floating-point Functions (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="Floating-point Functions (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="index.html#Top" rel="up" title="Top">
<link href="Initializing-Floats.html#Initializing-Floats" rel="next" title="Initializing Floats">
<link href="I_002fO-of-Rationals.html#I_002fO-of-Rationals" rel="prev" title="I/O of 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="Floating_002dpoint-Functions"></a>
<div class="header">
<p>
Next: <a href="Low_002dlevel-Functions.html#Low_002dlevel-Functions" accesskey="n" rel="next">Low-level Functions</a>, Previous: <a href="Rational-Number-Functions.html#Rational-Number-Functions" accesskey="p" rel="prev">Rational Number Functions</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Floating_002dpoint-Functions-1"></a>
<h2 class="chapter">7 Floating-point Functions</h2>
<a name="index-Floating_002dpoint-functions"></a>
<a name="index-Float-functions"></a>
<a name="index-User_002ddefined-precision"></a>
<a name="index-Precision-of-floats"></a>
<p>GMP floating point numbers are stored in objects of type <code>mpf_t</code> and
functions operating on them have an <code>mpf_</code> prefix.
</p>
<p>The mantissa of each float has a user-selectable precision, in practice only
limited by available memory. Each variable has its own precision, and that can
be increased or decreased at any time. This selectable precision is a minimum
value, GMP rounds it up to a whole limb.
</p>
<p>The accuracy of a calculation is determined by the priorly set precision of the
destination variable and the numeric values of the input variables. Input
variables&rsquo; set precisions do not affect calculations (except indirectly as
their values might have been affected when they were assigned).
</p>
<p>The exponent of each float has fixed precision, one machine word on most
systems. In the current implementation the exponent is a count of limbs, so
for example on a 32-bit system this means a range of roughly
<em>2^<span class="nolinebreak">-68719476768</span><!-- /@w --></em> to <em>2^68719476736<!-- /@w --></em>, or on a 64-bit system
this will be much greater. Note however that <code>mpf_get_str</code> can only
return an exponent which fits an <code>mp_exp_t</code> and currently
<code>mpf_set_str</code> doesn&rsquo;t accept exponents bigger than a <code>long</code>.
</p>
<p>Each variable keeps track of the mantissa data actually in use. This means
that if a float is exactly represented in only a few bits then only those bits
will be used in a calculation, even if the variable&rsquo;s selected precision is
high. This is a performance optimization; it does not affect the numeric
results.
</p>
<p>Internally, GMP sometimes calculates with higher precision than that of the
destination variable in order to limit errors. Final results are always
truncated to the destination variable&rsquo;s precision.
</p>
<p>The mantissa is stored in binary. One consequence of this is that decimal
fractions like <em>0.1</em> cannot be represented exactly. The same is true of
plain IEEE <code>double</code> floats. This makes both highly unsuitable for
calculations involving money or other values that should be exact decimal
fractions. (Suitably scaled integers, or perhaps rationals, are better
choices.)
</p>
<p>The <code>mpf</code> functions and variables have no special notion of infinity or
not-a-number, and applications must take care not to overflow the exponent or
results will be unpredictable.
</p>
<p>Note that the <code>mpf</code> functions are <em>not</em> intended as a smooth
extension to IEEE P754 arithmetic. In particular results obtained on one
computer often differ from the results on a computer with a different word
size.
</p>
<p>New projects should consider using the GMP extension library MPFR
(<a href="http://mpfr.org">http://mpfr.org</a>) instead. MPFR provides well-defined precision and
accurate rounding, and thereby naturally extends IEEE P754.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="Initializing-Floats.html#Initializing-Floats" accesskey="1">Initializing Floats</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Assigning-Floats.html#Assigning-Floats" accesskey="2">Assigning Floats</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Simultaneous-Float-Init-_0026-Assign.html#Simultaneous-Float-Init-_0026-Assign" accesskey="3">Simultaneous Float Init &amp; Assign</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Converting-Floats.html#Converting-Floats" accesskey="4">Converting Floats</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Float-Arithmetic.html#Float-Arithmetic" accesskey="5">Float Arithmetic</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Float-Comparison.html#Float-Comparison" accesskey="6">Float Comparison</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="I_002fO-of-Floats.html#I_002fO-of-Floats" accesskey="7">I/O of Floats</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="Miscellaneous-Float-Functions.html#Miscellaneous-Float-Functions" accesskey="8">Miscellaneous Float Functions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<div class="header">
<p>
Next: <a href="Low_002dlevel-Functions.html#Low_002dlevel-Functions" accesskey="n" rel="next">Low-level Functions</a>, Previous: <a href="Rational-Number-Functions.html#Rational-Number-Functions" accesskey="p" rel="prev">Rational Number Functions</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>