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.
ceil
function rounds x upwards to the nearest integer, returning that value as a double
. Thus, ceil (1.5)
is 2.0
.
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
.
-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
.
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
.
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
.