×

Search anything:

Rounding Functions in C / C++

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Header file math.h contains a bunch of functions to make mathematical operations and transformations. These functions include trigonometric functions, hyperbolic functions, logarithmic functions, power functions, rounding functions and some other functions (like max, min, abs,etc.).

In this article, we will focus on the different rounding functions provided in C and C++.

The rounding functions which are being used most commonly are as follows:

  • floor
  • ceil
  • round
  • trunc
  • fmod

Following table summarizes the difference between the different functions:

x floor(x) ceil(x) round(x) trunc(x)
0.5 0.0 1.0 1.0 0.0
3.3 3.0 4.0 3.0 3.0
-3.3 -4.0 -3.0 -3.0 -3.0
3.8 3.0 4.0 4.0 3.0
-3.8 -4.0 -3.0 -4.0 -3.0

floor

  1. floor(x) : This function maps the input to the greatest preceding integer and returns the largest integer that is smaller than or equal to x.

C++ Code:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    float x = 5.5;
    float y = -5.5;

    cout<<"floor of "<<x<<"is : "<<floor(x)<<endl;
    cout<<"floor of "<<y<<"is : "<<floor(y)<<endl;
}

Output:

floor of 5.5 is : 5
floor of -5.5 is : -6

ceil

  1. ceil(x) : This function maps the input to the least succeeding integer and returns the smallest integer that is larger than or equal to x.

C++ Code:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    float x = 5.5;
    float y = -5.5;

    cout<<"ceil of "<<x<<"is : "<<ceil(x)<<endl;
    cout<<"ceil of "<<y<<"is : "<<ceil(y)<<endl;
}

Output:

ceil of 5.5 is : 6
ceil of -5.5 is : -5

round

  1. round(x) : This function is used to round off the input to the nearest integral value with halfway cases rounded away from zero.

C++ code:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    float x = 3.6;
    float y = 3.3;
    float z = -3.3;

    cout<<"Nearest Integer to "<<x<<"is : "<<round(x)<<endl;
    cout<<"Nearest Integer to "<<y<<"is : "<<round(y)<<endl;
    cout<<"Nearest Integer to "<<z<<"is : "<<round(z)<<endl;
}

Output:

Nearest Integer to 3.6 is : 4
Nearest Integer to 3.3 is : 3
Nearest Integer to -3.3 is : -3
  1. trunc(x) : This function is used to round off the input nearest to the zero. Value retured by trunc(x) always has the magnitude less than x.

It always follows:

trunc(x)<=x

C++ code:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    float x = 3.6;
    float y = 3.3;
    float z = -3.3;

    cout<<"trunc of "<<x<<"is : "<<trunc(x)<<endl;
    cout<<"trunc of "<<y<<"is : "<<trunc(y)<<endl;
    cout<<"trunc of "<<z<<"is : "<<trunc(z)<<endl;
}

Output:

Nearest Integer to 3.6 is : 3
Nearest Integer to 3.3 is : 3
Nearest Integer to -3.3 is : -3

fmod

  1. fmod(x,y) : This function is used to get the float point remainder when x is divided by y.
fmod(x,y) = x - ( trunc(x/y) * y)

Here, trunc(x/y) gives the truncated value when x is divided by y (rounded towards zero).

C++ code:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    float x = 9.2;
    float y = 2.0;
    float z = 3.0;

    cout<<"fmod of "<<x<<"and"<<y<<" is : "<<round(x)<<endl;
    cout<<"fmod of "<<x<<"and"<<z<<" is : "<<round(x)<<endl;
}

Output:

fmod of 9.2 and 2.0 is : 1.2
fmod of 9.2 and 3.0 is : 0.2

Read more:

With this article at OpenGenus, you have a good idea of using different rounding functions in C/ C++. Enjoy.

Rounding Functions in C / C++
Share this