Wednesday, June 27, 2012

Rounding Functions in ANSI C

To use rounding functions in ANSI C, you will need to include the mathematics header:
#include<math.h>

The ANSI C mathematical library provides support for rounding (rounding-up, rounding down, rounding to the nearest integer) and truncating real numbers.
Example:
#include<stdio.h>
#include<math.h>

int main(int argc, char** argv)
{
 double nr1 = 3.23;
 double nr2 = 3.53;
 double nr3 = -3.23;
 double nr4 = -3.53;
 double r1=0.0, r2=0.0, r3=0.0, r4=0.0;
 int i1 = 0, i2 = 0, i3 = 0, i4 = 0;

 /*
  * Rounds up the number using the formula
  * r = [x]+1
  * where [x] represents the integer part of x
  */
 r1 = ceil(nr1);
 r2 = ceil(nr2);
 r3 = ceil(nr3);
 r4 = ceil(nr4);
 printf("Ceil  : %.2f->%.2f   %.2f->%.2f   %.2f->%.2f   %.2f->%.2f\n",
   nr1,r1,nr2,r2,nr3,r3,nr4,r4);

 /*
  * Rounds down the number using the formula
  * r = [x] - 1
  * where [x] represents the integer part of x
  */
 r1 = floor(nr1);
 r2 = floor(nr2);
 r3 = floor(nr3);
 r4 = floor(nr4);
 printf("Floor : %.2f->%.2f   %.2f->%.2f   %.2f->%.2f   %.2f->%.2f\n",
   nr1,r1,nr2,r2,nr3,r3,nr4,r4);
 /*
  * Truncates the number using the formula
  * r = [x]
  * where [x] represents the integer part of x
  */
 r1 = trunc(nr1);
 r2 = trunc(nr2);
 r3 = trunc(nr3);
 r4 = trunc(nr4);
 printf("Trunc : %.2f->%.2f   %.2f->%.2f   %.2f->%.2f   %.2f->%.2f\n",
   nr1,r1,nr2,r2,nr3,r3,nr4,r4);

 /*
  * Rounds the number using the formula
  * r = [x+0.5]
  * where [x] represents the integer part of x
  */
 r1 = round(nr1);
 r2 = round(nr2);
 r3 = round(nr3);
 r4 = round(nr4);
 printf("Round : %.2f->%.2f   %.2f->%.2f   %.2f->%.2f   %.2f->%.2f\n",
   nr1,r1,nr2,r2,nr3,r3,nr4,r4);

 /*
  * Rounds the number using the formula
  * r = [x+0.5]
  * where [x] represents the integer part of x
  */
 i1 = rint(nr1);
 i2 = rint(nr2);
 i3 = rint(nr3);
 i4 = rint(nr4);
 printf("Rint  : %.2f->%4d   %.2f->%4d   %.2f->%4d    %.2f->%4d\n",
   nr1,i1,nr2,i2,nr3,i3,nr4,i4);
 return 0;
}
/*
Ceil  : 3.23->4.00   3.53->4.00   -3.23->-3.00   -3.53->-3.00
Floor : 3.23->3.00   3.53->3.00   -3.23->-4.00   -3.53->-4.00
Trunc : 3.23->3.00   3.53->3.00   -3.23->-3.00   -3.53->-3.00
Round : 3.23->3.00   3.53->4.00   -3.23->-3.00   -3.53->-4.00
Rint  : 3.23->   3   3.53->   4   -3.23->  -3    -3.53->  -4
 */
Note:
All compilers compliant with C99 should have functions that allow you to use the float and long double data type in the same way we used double.

All functions that operate with float will have have the same name as their double counterparts plus the -f suffix
Example: float roundf(float arg)

All functions that operate with long double will have have the same name as their double counterparts plus the -l suffix
Example: long double ceill(long double arg)

The function of rint also variants according to the returned type:
Exemple: long lrintf(float arg), long long llrintl(long double arg)

Mathematical notions:

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger