Go to the first, previous, next, last section, table of contents.


Rounding and Remainder Functions

The functions listed here perform operations such as rounding, truncation, and remainder in division of floating point numbers. Some of these functions convert floating point numbers to integer values. They are all declared in `math.h'.

You can also convert floating-point numbers to integers simply by casting them to int. This discards the fractional part, effectively rounding towards zero. However, this only works if the result can actually be represented as an int---for very large numbers, this is impossible. The functions listed here return the result as a double instead to get around this problem.

Function: double ceil (double x)
The ceil function rounds x upwards to the nearest integer, returning that value as a double. Thus, ceil (1.5) is 2.0.

Function: double floor (double x)
The ceil function rounds x downwards to the nearest integer, returning that value as a double. Thus, floor (1.5) is 1.0 and floor (-1.5) is -2.0.

Function: double rint (double x)
This function rounds x to an integer value according to the current rounding mode. See section Floating Point Parameters, for information about the various rounding modes. The default rounding mode is to round to the nearest integer; some machines support other modes, but round-to-nearest is always used unless you explicit select another.

Function: double modf (double value, double *integer-part)
This function breaks the argument value into an integer part and a fractional part (between -1 and 1, exclusive). Their sum equals value. Each of the parts has the same sign as value, so the rounding of the integer part is towards zero.

modf stores the integer part in *integer-part, and returns the fractional part. For example, modf (2.5, &intpart) returns 0.5 and stores 2.0 into intpart.

Function: double fmod (double numerator, double denominator)
This function computes the remainder from the division of numerator by denominator. Specifically, the return value is numerator - n * denominator, where n is the quotient of numerator divided by denominator, rounded towards zero to an integer. Thus, fmod (6.5, 2.3) returns 1.9, which is 6.5 minus 4.6.

The result has the same sign as the numerator and has magnitude less than the magnitude of the denominator.

If denominator is zero, fmod fails and sets errno to EDOM.

Function: double drem (double numerator, double denominator)
The function drem is like fmod except that it rounds the internal quotient n to the nearest integer instead of towards zero to an integer. For example, drem (6.5, 2.3) returns -0.4, which is 6.5 minus 6.9.

The absolute value of the result is less than or equal to half the absolute value of the denominator. The difference between fmod (numerator, denominator) and drem (numerator, denominator) is always either denominator, minus denominator, or zero.

If denominator is zero, drem fails and sets errno to EDOM.


Go to the first, previous, next, last section, table of contents.