August 12, 2009
C Snippet #2
Bob Stout
Rounding floating-point values
Q: HOW DO I... round floating-point values in C?
A: This header file Snippet contains macros to round floating-point values in two different manners. The iround() macro rounds the value to the nearest integer. The fround() macro rounds the value to a specified number of decimal points.
/*
** rounding macros by Dave Knapp
*/
/* round to integer */
#define iround(x) floor((x)+0.5)
/* round number n to d decimal points */
#define fround(n,d) (floor((n)/pow(.1,(d))+.5)*pow(.1,(d)))
Update to C Snippet #2
/**************************************************/
/**** round.h ****/
/*
** rounding macros by Dave Knapp, Thad Smith, Jon Strayer, & Bob Stout
*/
#ifndef ROUND__H
#define ROUND__H
#include <math.h>
#if defined(__cplusplus) && __cplusplus
/*
** Safe C++ inline versions
*/
/* round to integer */
template<class T> inline int iround(T x)
{
// This needs to be modified if passing long doubles
return (int)floor(double(x) + 0.5);
}
/* round number n to d decimal points */
inline double fround(double n, unsigned d)
{
return floor(n * pow(10., d) + .5) / pow(10., d);
}
#else // C
/*
** NOTE: These C macro versions are unsafe since arguments are referenced
** more than once.
**
** Avoid using these with expression arguments to be safe.
*/
/*
** round to integer
*/
#define iround(x) ((int)floor((double)(x) + 0.5))
/*
** round number n to d decimal points
*/
#define fround(n,d) (floor((n)*pow(10.,(d))+.5)/pow(10.,(d)))
#endif // C/C++
#endif /* ROUND__H */
/**********************************************/
/**** rndtst.c ****/
/*
** Demo/test main() function for ROUND.H
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "round.h"
int main(void)
{
float fx[] = {-5/3.0, -4/3.0, -2/3.0, -1/3.0, 1/3.0, 2/3.0, 4/3.0, 5/3.0};
double dx[] = {-5/3.0, -4/3.0, -2/3.0, -1/3.0, 1/3.0, 2/3.0, 4/3.0, 5/3.0};
double phi = (1 + sqrt(5.0)) / 2;
int i;
for (i = 0; i < 8; ++i)
printf("%d: float: iround(%.6g) = %d\n", i+1, fx[i], iround(fx[i]));
for (i = 0; i < 8; ++i)
printf("%d: double: iround(%.12g) = %d\n", i+1, dx[i], iround(dx[i]));
for (i = 1; i < 5; i++)
{
int x = i * 2;
printf("fround(phi, %d) = %.9g\n", x, fround(phi, x));
}
return EXIT_SUCCESS;
}
/************************************************/
/**** rndtst.out ****/
1: float: iround(-1.66667) = -2
2: float: iround(-1.33333) = -1
3: float: iround(-0.666667) = -1
4: float: iround(-0.333333) = 0
5: float: iround(0.333333) = 0
6: float: iround(0.666667) = 1
7: float: iround(1.33333) = 1
8: float: iround(1.66667) = 2
1: double: iround(-1.66666666667) = -2
2: double: iround(-1.33333333333) = -1
3: double: iround(-0.666666666667) = -1
4: double: iround(-0.333333333333) = 0
5: double: iround(0.333333333333) = 0
6: double: iround(0.666666666667) = 1
7: double: iround(1.33333333333) = 1
8: double: iround(1.66666666667) = 2
fround(phi, 2) = 1.62
fround(phi, 4) = 1.618
fround(phi, 6) = 1.618034
More C Snippets
All the code in C Snippets is either public domain or freeware and may therefore freely be used by the C programming community without restrictions. In most cases, if the original author is someone other than myself he or she will be identified. Thanks to all who have contributed to this collection over the years. I hope Dr. Dobb's readers will find these useful.
--Bob Stout
rbs@snippets.org