Node:Rounding Functions, Next:, Previous:Normalization Functions, Up:Arithmetic Functions



Rounding Functions

The functions listed here perform operations such as rounding and truncation of floating-point values. 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.

double ceil (double x) Function
float ceilf (float x) Function
long double ceill (long double x) Function
These functions round x upwards to the nearest integer, returning that value as a double. Thus, ceil (1.5) is 2.0.

double floor (double x) Function
float floorf (float x) Function
long double floorl (long double x) Function
These functions round 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.

double trunc (double x) Function
float truncf (float x) Function
long double truncl (long double x) Function
The trunc functions round x towards zero to the nearest integer (returned in floating-point format). Thus, trunc (1.5) is 1.0 and trunc (-1.5) is -1.0.

double rint (double x) Function
float rintf (float x) Function
long double rintl (long double x) Function
These functions round x to an integer value according to the current rounding mode. See 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 explicitly select another.

If x was not initially an integer, these functions raise the inexact exception.

double nearbyint (double x) Function
float nearbyintf (float x) Function
long double nearbyintl (long double x) Function
These functions return the same value as the rint functions, but do not raise the inexact exception if x is not an integer.

double round (double x) Function
float roundf (float x) Function
long double roundl (long double x) Function
These functions are similar to rint, but they round halfway cases away from zero instead of to the nearest even integer.

long int lrint (double x) Function
long int lrintf (float x) Function
long int lrintl (long double x) Function
These functions are just like rint, but they return a long int instead of a floating-point number.

long long int llrint (double x) Function
long long int llrintf (float x) Function
long long int llrintl (long double x) Function
These functions are just like rint, but they return a long long int instead of a floating-point number.

long int lround (double x) Function
long int lroundf (float x) Function
long int lroundl (long double x) Function
These functions are just like round, but they return a long int instead of a floating-point number.

long long int llround (double x) Function
long long int llroundf (float x) Function
long long int llroundl (long double x) Function
These functions are just like round, but they return a long long int instead of a floating-point number.

double modf (double value, double *integer-part) Function
float modff (float value, float *integer-part) Function
long double modfl (long double value, long double *integer-part) Function
These functions break 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, and the integer part is always rounded toward 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.