Several rounding methods in C language

There are several rounding methods in C language:
1. Assign value directly to integer variable.
Such as: int i = 2.5; or i = (int) 2.5;
This method uses the fractional part to be rounded off.
2. The integer division operator "/" in C/C++ has a rounding function (int / int) , But the result of integer division on negative numbers is related to the C compiler used.
3. Use the floor function.
floor(x) returns the largest integer less than or equal to x.
Such as: floor(2.5) = 2 floor(-2.5) = -3 4, 4, use the ceil function.
ceil(x) returns the smallest integer greater than x.
Such as: ceil(2.5) = 3
ceil(-2.5) = -2

floor() is to round to negative infinity,
floor(-2.5) = -3;
ceil() is to round to positive infinity,
ceil(-2.5) = -2.

Guess you like

Origin blog.csdn.net/m0_53052839/article/details/111145400