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.

204 lines
12 KiB
HTML

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- This manual documents how to install and use the Multiple Precision
Floating-Point Reliable Library, version 3.1.4.
Copyright 1991, 1993-2016 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.2 or any later
version published by the Free Software Foundation; with no Invariant Sections,
with no Front-Cover Texts, and with no Back-Cover Texts. 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>Integer Related Functions (GNU MPFR 3.1.4)</title>
<meta name="description" content="How to install and use GNU MPFR, a library for reliable multiple precision
floating-point arithmetic, version 3.1.4.">
<meta name="keywords" content="Integer Related Functions (GNU MPFR 3.1.4)">
<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=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="MPFR-Interface.html#MPFR-Interface" rel="up" title="MPFR Interface">
<link href="Rounding-Related-Functions.html#Rounding-Related-Functions" rel="next" title="Rounding Related Functions">
<link href="Formatted-Output-Functions.html#Formatted-Output-Functions" rel="prev" title="Formatted Output Functions">
<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="Integer-Related-Functions"></a>
<div class="header">
<p>
Next: <a href="Rounding-Related-Functions.html#Rounding-Related-Functions" accesskey="n" rel="next">Rounding Related Functions</a>, Previous: <a href="Formatted-Output-Functions.html#Formatted-Output-Functions" accesskey="p" rel="prev">Formatted Output Functions</a>, Up: <a href="MPFR-Interface.html#MPFR-Interface" accesskey="u" rel="up">MPFR Interface</a> &nbsp; [<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="index-Integer-related-functions"></a>
<a name="Integer-and-Remainder-Related-Functions"></a>
<h3 class="section">5.10 Integer and Remainder Related Functions</h3>
<dl>
<dt><a name="index-mpfr_005frint"></a>Function: <em>int</em> <strong>mpfr_rint</strong> <em>(mpfr_t <var>rop</var>, mpfr_t <var>op</var>, mpfr_rnd_t <var>rnd</var>)</em></dt>
<dt><a name="index-mpfr_005fceil"></a>Function: <em>int</em> <strong>mpfr_ceil</strong> <em>(mpfr_t <var>rop</var>, mpfr_t <var>op</var>)</em></dt>
<dt><a name="index-mpfr_005ffloor"></a>Function: <em>int</em> <strong>mpfr_floor</strong> <em>(mpfr_t <var>rop</var>, mpfr_t <var>op</var>)</em></dt>
<dt><a name="index-mpfr_005fround"></a>Function: <em>int</em> <strong>mpfr_round</strong> <em>(mpfr_t <var>rop</var>, mpfr_t <var>op</var>)</em></dt>
<dt><a name="index-mpfr_005ftrunc"></a>Function: <em>int</em> <strong>mpfr_trunc</strong> <em>(mpfr_t <var>rop</var>, mpfr_t <var>op</var>)</em></dt>
<dd><p>Set <var>rop</var> to <var>op</var> rounded to an integer.
<code>mpfr_rint</code> rounds to the nearest representable integer in the
given direction <var>rnd</var>, <code>mpfr_ceil</code> rounds
to the next higher or equal representable integer, <code>mpfr_floor</code> to
the next lower or equal representable integer, <code>mpfr_round</code> to the
nearest representable integer, rounding halfway cases away from zero
(as in the roundTiesToAway mode of IEEE 754-2008),
and <code>mpfr_trunc</code> to the next representable integer toward zero.
</p>
<p>The returned value is zero when the result is exact, positive when it is
greater than the original value of <var>op</var>, and negative when it is smaller.
More precisely, the returned value is 0 when <var>op</var> is an integer
representable in <var>rop</var>, 1 or 1 when <var>op</var> is an integer
that is not representable in <var>rop</var>, 2 or 2 when <var>op</var> is
not an integer.
</p>
<p>When <var>op</var> is NaN, the NaN flag is set as usual. In the other cases,
the inexact flag is set when <var>rop</var> differs from <var>op</var>, following
the ISO C99 rule for the <code>rint</code> function. If you want the behavior to
be more like IEEE 754 / ISO TS 18661-1, i.e., the usual behavior where the
round-to-integer function is regarded as any other mathematical function,
you should use one the <code>mpfr_rint_*</code> functions instead (however it is
not possible to round to nearest with the even rounding rule yet).
</p>
<p>Note that <code>mpfr_round</code> is different from <code>mpfr_rint</code> called with
the rounding to nearest mode (where halfway cases are rounded to an even
integer or significand). Note also that no double rounding is performed; for
instance, 10.5 (1010.1 in binary) is rounded by <code>mpfr_rint</code> with
rounding to nearest to 12 (1100
in binary) in 2-bit precision, because the two enclosing numbers representable
on two bits are 8 and 12, and the closest is 12.
(If one first rounded to an integer, one would round 10.5 to 10 with
even rounding, and then 10 would be rounded to 8 again with even rounding.)
</p></dd></dl>
<dl>
<dt><a name="index-mpfr_005frint_005fceil"></a>Function: <em>int</em> <strong>mpfr_rint_ceil</strong> <em>(mpfr_t <var>rop</var>, mpfr_t <var>op</var>, mpfr_rnd_t <var>rnd</var>)</em></dt>
<dt><a name="index-mpfr_005frint_005ffloor"></a>Function: <em>int</em> <strong>mpfr_rint_floor</strong> <em>(mpfr_t <var>rop</var>, mpfr_t <var>op</var>, mpfr_rnd_t <var>rnd</var>)</em></dt>
<dt><a name="index-mpfr_005frint_005fround"></a>Function: <em>int</em> <strong>mpfr_rint_round</strong> <em>(mpfr_t <var>rop</var>, mpfr_t <var>op</var>, mpfr_rnd_t <var>rnd</var>)</em></dt>
<dt><a name="index-mpfr_005frint_005ftrunc"></a>Function: <em>int</em> <strong>mpfr_rint_trunc</strong> <em>(mpfr_t <var>rop</var>, mpfr_t <var>op</var>, mpfr_rnd_t <var>rnd</var>)</em></dt>
<dd><p>Set <var>rop</var> to <var>op</var> rounded to an integer.
<code>mpfr_rint_ceil</code> rounds to the next higher or equal integer,
<code>mpfr_rint_floor</code> to the next lower or equal integer,
<code>mpfr_rint_round</code> to the nearest integer, rounding halfway cases away
from zero, and <code>mpfr_rint_trunc</code> to the next integer toward zero.
If the result is not representable, it is rounded in the direction <var>rnd</var>.
The returned value is the ternary value associated with the considered
round-to-integer function (regarded in the same way as any other
mathematical function).
</p>
<p>Contrary to <code>mpfr_rint</code>, those functions do perform a double rounding:
first <var>op</var> is rounded to the nearest integer in the direction given by
the function name, then this nearest integer (if not representable) is
rounded in the given direction <var>rnd</var>. Thus these round-to-integer
functions behave more like the other mathematical functions, i.e., the
returned result is the correct rounding of the exact result of the function
in the real numbers.
</p>
<p>For example, <code>mpfr_rint_round</code> with rounding to nearest and a precision
of two bits rounds 6.5 to 7 (halfway cases away from zero), then 7 is
rounded to 8 by the round-even rule, despite the fact that 6 is also
representable on two bits, and is closer to 6.5 than 8.
</p></dd></dl>
<dl>
<dt><a name="index-mpfr_005ffrac"></a>Function: <em>int</em> <strong>mpfr_frac</strong> <em>(mpfr_t <var>rop</var>, mpfr_t <var>op</var>, mpfr_rnd_t <var>rnd</var>)</em></dt>
<dd><p>Set <var>rop</var> to the fractional part of <var>op</var>, having the same sign as
<var>op</var>, rounded in the direction <var>rnd</var> (unlike in <code>mpfr_rint</code>,
<var>rnd</var> affects only how the exact fractional part is rounded, not how
the fractional part is generated).
</p></dd></dl>
<dl>
<dt><a name="index-mpfr_005fmodf"></a>Function: <em>int</em> <strong>mpfr_modf</strong> <em>(mpfr_t <var>iop</var>, mpfr_t <var>fop</var>, mpfr_t <var>op</var>, mpfr_rnd_t <var>rnd</var>)</em></dt>
<dd><p>Set simultaneously <var>iop</var> to the integral part of <var>op</var> and <var>fop</var> to
the fractional part of <var>op</var>, rounded in the direction <var>rnd</var> with the
corresponding precision of <var>iop</var> and <var>fop</var> (equivalent to
<code>mpfr_trunc(<var>iop</var>, <var>op</var>, <var>rnd</var>)</code> and
<code>mpfr_frac(<var>fop</var>, <var>op</var>, <var>rnd</var>)</code>). The variables <var>iop</var> and
<var>fop</var> must be different. Return 0 iff both results are exact (see
<code>mpfr_sin_cos</code> for a more detailed description of the return value).
</p></dd></dl>
<dl>
<dt><a name="index-mpfr_005ffmod"></a>Function: <em>int</em> <strong>mpfr_fmod</strong> <em>(mpfr_t <var>r</var>, mpfr_t <var>x</var>, mpfr_t <var>y</var>, mpfr_rnd_t <var>rnd</var>)</em></dt>
<dt><a name="index-mpfr_005fremainder"></a>Function: <em>int</em> <strong>mpfr_remainder</strong> <em>(mpfr_t <var>r</var>, mpfr_t <var>x</var>, mpfr_t <var>y</var>, mpfr_rnd_t <var>rnd</var>)</em></dt>
<dt><a name="index-mpfr_005fremquo"></a>Function: <em>int</em> <strong>mpfr_remquo</strong> <em>(mpfr_t <var>r</var>, long* <var>q</var>, mpfr_t <var>x</var>, mpfr_t <var>y</var>, mpfr_rnd_t <var>rnd</var>)</em></dt>
<dd><p>Set <var>r</var> to the value of <em><var>x</var> - <var>n</var><var>y</var></em>, rounded
according to the direction <var>rnd</var>, where <var>n</var> is the integer quotient
of <var>x</var> divided by <var>y</var>, defined as follows: <var>n</var> is rounded
toward zero for <code>mpfr_fmod</code>, and to the nearest integer (ties rounded
to even) for <code>mpfr_remainder</code> and <code>mpfr_remquo</code>.
</p>
<p>Special values are handled as described in Section F.9.7.1 of
the ISO C99 standard:
If <var>x</var> is infinite or <var>y</var> is zero, <var>r</var> is NaN.
If <var>y</var> is infinite and <var>x</var> is finite, <var>r</var> is <var>x</var> rounded
to the precision of <var>r</var>.
If <var>r</var> is zero, it has the sign of <var>x</var>.
The return value is the ternary value corresponding to <var>r</var>.
</p>
<p>Additionally, <code>mpfr_remquo</code> stores
the low significant bits from the quotient <var>n</var> in <var>*q</var>
(more precisely the number of bits in a <code>long</code> minus one),
with the sign of <var>x</var> divided by <var>y</var>
(except if those low bits are all zero, in which case zero is returned).
Note that <var>x</var> may be so large in magnitude relative to <var>y</var> that an
exact representation of the quotient is not practical.
The <code>mpfr_remainder</code> and <code>mpfr_remquo</code> functions are useful for
additive argument reduction.
</p></dd></dl>
<dl>
<dt><a name="index-mpfr_005finteger_005fp"></a>Function: <em>int</em> <strong>mpfr_integer_p</strong> <em>(mpfr_t <var>op</var>)</em></dt>
<dd><p>Return non-zero iff <var>op</var> is an integer.
</p></dd></dl>
<hr>
<div class="header">
<p>
Next: <a href="Rounding-Related-Functions.html#Rounding-Related-Functions" accesskey="n" rel="next">Rounding Related Functions</a>, Previous: <a href="Formatted-Output-Functions.html#Formatted-Output-Functions" accesskey="p" rel="prev">Formatted Output Functions</a>, Up: <a href="MPFR-Interface.html#MPFR-Interface" accesskey="u" rel="up">MPFR Interface</a> &nbsp; [<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>