Next: Rounding-Related Functions, Previous: Formatted Output Functions, 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_roundeven (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, and the other five functions behave in a similar way with some fixed rounding mode:

  • mpfr_ceil: to the next higher or equal representable integer (like mpfr_rint with MPFR_RNDU);
  • mpfr_floor to the next lower or equal representable integer (like mpfr_rint with MPFR_RNDD);
  • mpfr_round to the nearest representable integer, rounding halfway cases away from zero (as in the roundTiesToAway mode of IEEE 754);
  • mpfr_roundeven to the nearest representable integer, rounding halfway cases with the even-rounding rule (like mpfr_rint with MPFR_RNDN);
  • mpfr_trunc to the next representable integer toward zero (like mpfr_rint with MPFR_RNDZ).

When op is a zero or an infinity, set rop to the same value (with the same sign).

The return 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 return 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 of the mpfr_rint_* functions instead.

Note 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_roundeven (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: 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;
  • mpfr_rint_roundeven: to the nearest integer, rounding halfway cases to the nearest even integer;
  • mpfr_rint_trunc to the next integer toward zero.

If the result is not representable, it is rounded in the direction rnd. When op is a zero or an infinity, set rop to the same value (with the same sign). The return 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). When op is an integer or an infinity, set rop to zero with the same sign as op.

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_fmod_ui (mpfr_t r, mpfr_t x, unsigned long int y, mpfr_rnd_t rnd) ¶
Function: int mpfr_fmodquo (mpfr_t r, long int* q, 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 int* 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, mpfr_fmod_ui and mpfr_fmodquo, 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_fmodquo and mpfr_remquo store the low significant bits from the quotient n in *q (more precisely the number of bits in a long int minus one), with the sign of x divided by y (except if those low bits are all zero, in which case zero is returned). If the result is NaN, the value of *q is unspecified. 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: Rounding-Related Functions, Previous: Formatted Output Functions, Up: MPFR Interface   [Index]