Recover Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttp://yahoo.com/
ComputingCMath.h

round

Round to integral value, regardless of rounding direction
+ View other versions (3)

Interface

#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)

Description

The round functions return the integral value nearest to 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.

Example:

Example - Round to integral value, regardless of rounding direction
Workings
#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;
}
Solution
Output:
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

Special Values

round ( ±0 ) returns ±0.

round ( ±∞ ) returns ±∞.

The round functions may, but are not required to, raise the inexact floating-point exception for non-integer numeric arguments.

The lround and llround functions need not raise the inexact floating-point exception for non-integer arguments that round to within the range of the return type.

See Also

abs, fabs, ceil, floor

Standards

The round, lround and llround functions conform to ISO/IEC 9899:1999(E).
 

Page Comments

greentrust\′s Photo
11 Jan 12, 10:08PM
A nice undocumented bennie for Arduino
Was happy to find out (accidently) that round() works very nicely in the Arduino world. It's not documented, but it works. http://arduino.cc/en/Reference/HomePage
tiborh\′s Photo
10 Jan 09, 8:54PM
(2 replies)
an error and a warning
Has anyone actually tried to compile the example code above? Well, I tried and here's what came out:

round_codecogs.c: In function 'main': round_codecogs.c:6: error: 'for' loop initial declaration used outside C99 mode round_codecogs.c:7: warning: incompatible implicit declaration of built-in function 'round'

(using (in Kubuntu 8.10): gcc -lm -o <progname> <progname>.c)

The error is easy to solve: the variable "a" should not be defined inside the "for" loop. But I have no idea how one can make the warning disappear. (Of course, I can modify the problem line:

printf("round of %.1lf is %d\n", a, (int)(a+0.5));

to avoid the problem.)
john\′s Photo
11 Jan 09, 11:07PM
Hi, just tried myself under OSX. With gcc, I get errors also.

But isn't gcc for compiling straight c, not c++. So try use: g++, thus:
g++ test.cpp -lm -o test
works perfectly. Getting exact same results as those illustrated.
tiborh\′s Photo
11 Jan 09, 11:35PM
Yes, you're right. It works very well with g++. I just did not realise that the code snippet was meant to be c++ and not c (in which I was working when I checked on this function).
john\′s Photo
15 Nov 07, 1:11PM
(1 reply)
Does this work on all platforms?
will\′s Photo
15 Nov 07, 5:24PM
Round doesn't work with Microsoft Visual Studio, and I suspect it probably doesn't work with many other Windows C++ compilers (i.e. I can't find it in the Borland C manual).

Simple though to replicate with the floor command, i.e. <tt>round(x) == floor(x+0.5)</tt>.

Heres an example for MS Visual Studio, for a Win32 console project:
#include "stdafx.h"
#include <math.h>
#include <stdio.h>
 
int _tmain(int argc, _TCHAR* argv[])
{
  for(double a=12;a<13;a+=0.1)
    printf("round of  %.1lf is  %.1lf\n", a, floor(a+0.5));
  return 0;
}
 
 Format Excel Equations

  You must login to leave a messge