Precision of floating point numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())

C++Server Side ProgrammingProgramming

Precision of floating point numbers is the accuracy upto which a floating point number can hold the values after decimal.

For example 10/6 = 1.6666666… these have recurring decimals which can take infinite memory spaces to be stored.

So to avoid memory overflow in such cases the compiler set a precision limit to the number. For float values in C++ this precision is set to 6-7 digit after that if the decimal recurs it will discard the value.

So, to avoid any major losses when this discarding takes place there are methods and libraries that support the precision is float values. Here, we will discuss them,

floor()

floor() function is round down function that round the number to nearest integer values which is less than the number.

It always returns an integer value which is one less than the integer part of the float number.

Library: math.h

Example

 Live Demo

#include<iostream>
#include<math.h>
using namespace std;
int main() {
   float number1 = 0.435 , number2 = 234.2342, number3 = -3.31132, number4 = -0.432;
   cout<<"Values are : \n";
   cout<<"Number 1 : "<<floor(number1)<<endl;
   cout<<"Number 2 : "<<floor(number2)<<endl;
   cout<<"Number 3 : "<<floor(number3)<<endl;
   cout<<"Number 4 : "<<floor(number4)<<endl;
   return 0;
}

Output

Values are :
Number 1 : 0
Number 2 : 234
Number 3 : -4
Number 4 : -1

ceil()

ceil() function is rounding function that rounds the number to nearest integer values which is greater than the number.

It always returns an integer value which is one more than the integer part of the float number.

Library: math.h

Example

 Live Demo

#include<iostream>
#include<math.h>
using namespace std;
int main() {
   float number1 = 0.435 , number2 = 234.2342, number3 = -3.31132, number4 = -0.432;
   cout<<"Values are : \n";
   cout<<"Number 1 : "<<ceil(number1)<<endl;
   cout<<"Number 2 : "<<ceil(number2)<<endl;
   cout<<"Number 3 : "<<ceil(number3)<<endl;
   cout<<"Number 4 : "<<ceil(number4)<<endl;
   return 0;
}

Output

Values are :
Number 1 : 1
Number 2 : 235
Number 3 : -3
Number 4 : -0

round()

round() function is rounding function that rounds the number to nearest integer values which can be greater or smaller than the number.

It always returns an integer value which can one more/less than the integer part of the float number.

Library: math.h

Example

 Live Demo

#include<iostream>
#include<math.h>
using namespace std;
int main() {
   float number1 = 0.435 , number2 = 234.5612, number3 = -3.31132, number4 = -0.9132;
   cout<<"Values are : \n";
   cout<<"Number 1 : "<<ceil(number1)<<endl;
   cout<<"Number 2 : "<<ceil(number2)<<endl;
   cout<<"Number 3 : "<<ceil(number3)<<endl;
   cout<<"Number 4 : "<<ceil(number4)<<endl;
   return 0;
}

Output

Values are :
Number 1 : 1
Number 2 : 235
Number 3 : -3
Number 4 : -0

setprecision() 

The setprecision() function returns the value of flaot which is correct upto n decimal places. N is the parameter passed to the setprecission function.

For the functioning, it uses fixed.

Library: iomanip

Example

 Live Demo

#include<iostream>
#include<iomanip>
using namespace std;
int main() {
   float number1 = 0.435 , number2 = 234.5612, number3 = -3.31132, number4 = -0.9132;
   cout<<"Nubmer 1 correct upto 0 decimals "<<fixed<<setprecision(0)<<number1<<endl;
   cout<<"Nubmer 2 correct upto 1 decimals "<<fixed<<setprecision(1)<<number2<<endl;
   cout<<"Nubmer 3 correct upto 4 decimals "<<fixed<<setprecision(4)<<number3<<endl;
   cout<<"Nubmer 4 correct upto 3 decimals "<<fixed<<setprecision(3)<<number4<<endl;
}

Output

Nubmer 1 correct upto 0 decimals 0
Nubmer 2 correct upto 1 decimals 234.6
Nubmer 3 correct upto 4 decimals -3.3113
Nubmer 4 correct upto 3 decimals -0.913
raja
Published on 04-Feb-2020 07:32:18
Advertisements