Next: , Previous: , Up: MPFR Interface   [Index]


5.10 Integer and Remainder Related Functions

Function: int mpfr_rint (mpfr_t rop, mpfr_t op, mpfr_rnd_t rnd)
Function: int mpfr_ceil (mpfr_t rop, mpfr_t op)
Function: int mpfr_floor (mpfr_t rop, mpfr_t op)
Function: int mpfr_round (mpfr_t rop, mpfr_t op)
Function: int mpfr_trunc (mpfr_t rop, mpfr_t op)

Set rop to op rounded to an integer. mpfr_rint rounds to the nearest representable integer in the given direction rnd, mpfr_ceil rounds to the next higher or equal representable integer, mpfr_floor to the next lower or equal representable integer, mpfr_round to the nearest representable integer, rounding halfway cases away from zero (as in the roundTiesToAway mode of IEEE 754-2008), and mpfr_trunc to the next representable integer toward zero.

The returned value is zero when the result is exact, positive when it is greater than the original value of op, and negative when it is smaller. More precisely, the returned value is 0 when op is an integer representable in rop, 1 or −1 when op is an integer that is not representable in rop, 2 or −2 when op is not an integer.

When op is NaN, the NaN flag is set as usual. In the other cases, the inexact flag is set when rop differs from op, following the ISO C99 rule for the rint 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 mpfr_rint_* functions instead (however it is not possible to round to nearest with the even rounding rule yet).

Note that mpfr_round is different from mpfr_rint 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 mpfr_rint 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.)

Function: int mpfr_rint_ceil (mpfr_t rop, mpfr_t op, mpfr_rnd_t rnd)
Function: int mpfr_rint_floor (mpfr_t rop, mpfr_t op, mpfr_rnd_t rnd)
Function: int mpfr_rint_round (mpfr_t rop, mpfr_t op, mpfr_rnd_t rnd)
Function: int mpfr_rint_trunc (mpfr_t rop, mpfr_t op, mpfr_rnd_t rnd)

Set rop to op rounded to an integer. mpfr_rint_ceil rounds to the next higher or equal integer, mpfr_rint_floor to the next lower or equal integer, mpfr_rint_round to the nearest integer, rounding halfway cases away from zero, and mpfr_rint_trunc to the next integer toward zero. If the result is not representable, it is rounded in the direction rnd. 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).

Contrary to mpfr_rint, those functions do perform a double rounding: first op 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 rnd. 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.

For example, mpfr_rint_round 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.

Function: int mpfr_frac (mpfr_t rop, mpfr_t op, mpfr_rnd_t rnd)

Set rop to the fractional part of op, having the same sign as op, rounded in the direction rnd (unlike in mpfr_rint, rnd affects only how the exact fractional part is rounded, not how the fractional part is generated).

Function: int mpfr_modf (mpfr_t iop, mpfr_t fop, mpfr_t op, mpfr_rnd_t rnd)

Set simultaneously iop to the integral part of op and fop to the fractional part of op, rounded in the direction rnd with the corresponding precision of iop and fop (equivalent to mpfr_trunc(iop, op, rnd) and mpfr_frac(fop, op, rnd)). The variables iop and fop must be different. Return 0 iff both results are exact (see mpfr_sin_cos for a more detailed description of the return value).

Function: int mpfr_fmod (mpfr_t r, mpfr_t x, mpfr_t y, mpfr_rnd_t rnd)
Function: int mpfr_remainder (mpfr_t r, mpfr_t x, mpfr_t y, mpfr_rnd_t rnd)
Function: int mpfr_remquo (mpfr_t r, long* q, mpfr_t x, mpfr_t y, mpfr_rnd_t rnd)

Set r to the value of x - ny, rounded according to the direction rnd, where n is the integer quotient of x divided by y, defined as follows: n is rounded toward zero for mpfr_fmod, and to the nearest integer (ties rounded to even) for mpfr_remainder and mpfr_remquo.

Special values are handled as described in Section F.9.7.1 of the ISO C99 standard: If x is infinite or y is zero, r is NaN. If y is infinite and x is finite, r is x rounded to the precision of r. If r is zero, it has the sign of x. The return value is the ternary value corresponding to r.

Additionally, mpfr_remquo stores the low significant bits from the quotient n in *q (more precisely the number of bits in a long minus one), with the sign of x divided by y (except if those low bits are all zero, in which case zero is returned). Note that x may be so large in magnitude relative to y that an exact representation of the quotient is not practical. The mpfr_remainder and mpfr_remquo functions are useful for additive argument reduction.

Function: int mpfr_integer_p (mpfr_t op)

Return non-zero iff op is an integer.


Next: , Previous: , Up: MPFR Interface   [Index]