CodeCogs - An iteractive open source Numerical library Welcome... Login
CodeCogs
shopping cart
OSXWindowsLinux
Search CodeCogs
Numerical Pages

Valid RSS

CMath.h

round

About | FAQ | Contact
About FAQ Contact

Round to integral value, regardless of rounding direction

viewed 222574 times


+View other versions (2)
Contents -

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 1:
#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;
}
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).
Last Modified: 2009-04-23 08:26:37     Page Rendered: 2010-03-13 20:25:29

Page Comments

mrozik\′s Photo
17 Oct 09, 8:03AM
The above example is misleading as 12+0.1+0.1+0.1+0.1+0.1 < 12.5 !

You should try the following example:

#include <cmath>
#include <cstdio>
 
int main() {
    double y = 12.0;
    for (int i=0; i<5; i++) y+=0.1;
    printf("round of %.1lf is  %.1lf\n", y, round(y));

double x = 12.5; printf("round of %.1lf is %.1lf\n", x, round(x));

return 0; }

output:
round of 12.5 is  12.0
round of 12.5 is  13.0
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 .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. round(x) == floor(x+0.5).

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


Valid CSS!   Valid XHTML 1.0 Transitional