Next: Remainder Functions, Previous: Normalization Functions, Up: Arithmetic 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.

— Function: double **ceil** (`double x`)

— Function: floatceilf(float x)

— Function: long doubleceill(long double x)

These functions round

xupwards to the nearest integer, returning that value as a`double`

. Thus,`ceil (1.5)`

is`2.0`

.

— Function: double **floor** (`double x`)

— Function: floatfloorf(float x)

— Function: long doublefloorl(long double x)

These functions round

xdownwards 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 **trunc** (`double x`)

— Function: floattruncf(float x)

— Function: long doubletruncl(long double x)

The

`trunc`

functions roundxtowards zero to the nearest integer (returned in floating-point format). Thus,`trunc (1.5)`

is`1.0`

and`trunc (-1.5)`

is`-1.0`

.

— Function: double **rint** (`double x`)

— Function: floatrintf(float x)

— Function: long doublerintl(long double x)

These functions round

xto 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

xwas not initially an integer, these functions raise the inexact exception.

— Function: double **nearbyint** (`double x`)

— Function: floatnearbyintf(float x)

— Function: long doublenearbyintl(long double x)

These functions return the same value as the

`rint`

functions, but do not raise the inexact exception ifxis not an integer.

— Function: double **round** (`double x`)

— Function: floatroundf(float x)

— Function: long doubleroundl(long double x)

These functions are similar to

`rint`

, but they round halfway cases away from zero instead of to the nearest even integer.

— Function: long int **lrint** (`double x`)

— Function: long intlrintf(float x)

— Function: long intlrintl(long double x)

These functions are just like

`rint`

, but they return a`long int`

instead of a floating-point number.

— Function: long long int **llrint** (`double x`)

— Function: long long intllrintf(float x)

— Function: long long intllrintl(long double x)

These functions are just like

`rint`

, but they return a`long long int`

instead of a floating-point number.

— Function: long int **lround** (`double x`)

— Function: long intlroundf(float x)

— Function: long intlroundl(long double x)

These functions are just like

`round`

, but they return a`long int`

instead of a floating-point number.

— Function: long long int **llround** (`double x`)

— Function: long long intllroundf(float x)

— Function: long long intllroundl(long double x)

These functions are just like

`round`

, but they return a`long long int`

instead of a floating-point number.

— Function: double **modf** (`double value, double *integer-part`)

— Function: floatmodff(float value, float *integer-part)

— Function: long doublemodfl(long double value, long double *integer-part)

These functions break the argument

valueinto an integer part and a fractional part (between`-1`

and`1`

, exclusive). Their sum equalsvalue. Each of the parts has the same sign asvalue, 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`

.