Or login with:
#include <math.h>| double | round (double x) |
| long | roundl (long double x) |
| float | roundf (float x) |
| long | lround (double x) |
| long | lroundl (long double x) |
| long | lroundf (float x) |
| long | llround (double x) |
| long | llroundl (long double x) |
| long | llroundf (float x) |
x rounding half-way cases away from zero, regardless of the current rounding direction.
The lround and llround functions return the integral value nearest to x (rounding half-way cases away from zero, regardless of the current rounding direction) in the return formats specified. If the rounded value is outside the range of the return type, the numeric result is unspecified and the invalid floating-point exception is raised. A range error may occur if the magnitude of x is too large.
#include <stdio.h> #include <math.h> int main(void) { for(double a=12;a<13;a+=0.1) printf("round of %.1lf is %.1lf\n", a, round(a)); return 0; }
round of 12.0 is 12.0 round of 12.1 is 12.0 round of 12.2 is 12.0 round of 12.3 is 12.0 round of 12.4 is 12.0 round of 12.5 is 12.0 round of 12.6 is 13.0 round of 12.7 is 13.0 round of 12.8 is 13.0 round of 12.9 is 13.0 round of 13.0 is 13.0
You must login to leave a messge