
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Precision of floating point numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())
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.
To avoid memory overflow in such cases the compiler set a precision limit to the number. For float values in C++, the 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.
In this article, we will discuss about various built-in method to find the precision of floating point numbers.
floor() Method
The 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. This function belongs to <math.h> header file.
Example
In this example, we use floor() function for the given number to get the rounded value.
#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; }
The above code produces the following result:
Values are : Number 1 : 0 Number 2 : 234 Number 3 : -4 Number 4 : -1
ceil() Method
The 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. This function belongs to <math.h> header file.
Example
In this example, we use ceil() function to accept the floating value and convert it into nearest rounded integer value.
#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; }
The above code produces the following result:
Values are : Number 1 : 1 Number 2 : 235 Number 3 : -3 Number 4 : -0
round() Method
The round() function refer to 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. This function belongs to <math.h> header file.
Example
Following is the example of round() function in which float values are passes and convert into round value (integer form).
#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; }
The above code produces the following result:
Values are : Number 1 : 1 Number 2 : 235 Number 3 : -3 Number 4 : -0
setprecision() Method
The setprecision() function returns the value of flaot which is correct upto n decimal places. N is the parameter passed to the setprecision function. The function belongs to <iomanip> header file. For the functioning, it uses fixed.
Example
In this example, we show the usage of setprecision() function.
#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; return 0; }
The above code produces the following result:
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
trunc() Method
In C++, trunc() function is used to remove the fractional part of a floating-point number of the given number. It only removes the decimal part, it does not round up or down. For round value, you can consider all the above function (floor(), ceil(), and round()) which was demonstrated to this article.
#include<iostream> #include<cmath> using namespace std; int main() { double x = 1.411, y = -2.789; cout << "trunc(1.411) = " << trunc(x) << endl; cout << "trunc(-2.789) = " << trunc(y) << endl; return 0; }
The above code produces the following result:
trunc(1.411) = 1 trunc(-2.789) = -2