Precision of Floating Point Numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())
Last Updated :
02 Nov, 2022
The decimal equivalent of 1/3 is 0.33333333333333…. An infinite length number would require infinite memory to store, and we typically have 4 or 8 bytes. Therefore, Floating point numbers store only a certain number of significant digits, and the rest are lost. The precision of a floating-point number defines how many significant digits it can represent without information loss. When outputting floating-point numbers, cout has a default precision of 6 and it truncates anything after that. Below are a few libraries and methods which are used to provide precision to floating-point numbers in C++:
1. floor() Method
Floor rounds off the given value to the closest integer which is less than the given value. It is defined in the <cmath> header file.
CPP
// C++ program to demonstrate working of floor()
// in C/C++
#include <bits/stdc++.h>
using namespace std;
// Driver Code
int main()
{
double x = 1.411, y = 1.500, z = 1.711;
cout << floor(x) << endl;
cout << floor(y) << endl;
cout << floor(z) << endl;
double a = -1.411, b = -1.500, c = -1.611;
cout << floor(a) << endl;
cout << floor(b) << endl;
cout << floor(c) << endl;
return 0;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
2. ceil() Method
Ceil rounds off the given value to the closest integer which is more than the given value. It is defined in the <cmath> header file.
CPP
// C++ program to demonstrate working of ceil()
// in C/C++
#include <bits/stdc++.h>
using namespace std;
// Driver Code
int main()
{
double x = 1.411, y = 1.500, z = 1.611;
cout << ceil(x) << endl;
cout << ceil(y) << endl;
cout << ceil(z) << endl;
double a = -1.411, b = -1.500, c = -1.611;
cout << ceil(a) << endl;
cout << ceil(b) << endl;
cout << ceil(c) << endl;
return 0;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
3. trunc() Method
Trunc rounds remove digits after the decimal point. It is defined in the <cmath> header file.
CPP
// C++ program to demonstrate working of trunc()
// in C/C++
#include <bits/stdc++.h>
using namespace std;
// Driver Code
int main()
{
double x = 1.411, y = 1.500, z = 1.611;
cout << trunc(x) << endl;
cout << trunc(y) << endl;
cout << trunc(z) << endl;
double a = -1.411, b = -1.500, c = -1.611;
cout << trunc(a) << endl;
cout << trunc(b) << endl;
cout << trunc(c) << endl;
return 0;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
4. round()
Rounds gave numbers to the closest integer. It is defined in the header files: <cmath> and <ctgmath>.
CPP
// C++ program to demonstrate working of round()
// in C/C++
#include <bits/stdc++.h>
using namespace std;
// Driver Code
int main()
{
double x = 1.411, y = 1.500, z = 1.611;
cout << round(x) << endl;
cout << round(y) << endl;
cout << round(z) << endl;
double a = -1.411, b = -1.500, c = -1.611;
cout << round(a) << endl;
cout << round(b) << endl;
cout << round(c) << endl;
return 0;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
5. setprecision()
Setprecision when used along with 'fixed' provides precision to floating-point numbers correct to decimal numbers mentioned in the brackets of the setprecision. It is defined in header file <iomanip>.
CPP
// C++ program to demonstrate
// working of setprecision()
// in C/C++
#include <bits/stdc++.h>
using namespace std;
// Driver Code
int main()
{
double pi = 3.14159, npi = -3.14159;
cout << fixed << setprecision(0) << pi << " " << npi
<< endl;
cout << fixed << setprecision(1) << pi << " " << npi
<< endl;
cout << fixed << setprecision(2) << pi << " " << npi
<< endl;
cout << fixed << setprecision(3) << pi << " " << npi
<< endl;
cout << fixed << setprecision(4) << pi << " " << npi
<< endl;
cout << fixed << setprecision(5) << pi << " " << npi
<< endl;
cout << fixed << setprecision(6) << pi << " " << npi
<< endl;
return 0;
}
Output3 -3
3.1 -3.1
3.14 -3.14
3.142 -3.142
3.1416 -3.1416
3.14159 -3.14159
3.141590 -3.141590
Time Complexity: O(1)
Auxiliary Space: O(1)
Note: When the value mentioned in the setprecision() exceeds the number of floating point digits in the original number then 0 is appended to floating point digit to match the precision mentioned by the user.
There exist other methods too to provide precision to floating-point numbers. The above mentioned are a few of the most commonly used methods to provide precision to floating-point numbers during competitive coding.
Similar Reads
Rounding Floating Point Number To two Decimal Places in C and C++ How to round off a floating point value to two places. For example, 5.567 should become 5.57 and 5.534 should become 5.53 First Method:- Using Float precision C++ #include<bits/stdc++.h> using namespace std; int main() { float var = 37.66666; // Directly print the number with .2f precision cou
2 min read
Write a one line C function to round floating point numbers Algorithm: roundNo(num) 1. If num is positive then add 0.5. 2. Else subtract 0.5. 3. Type cast the result to int and return. Example: num = 1.67, (int) num + 0.5 = (int)2.17 = 2 num = -1.67, (int) num - 0.5 = -(int)2.17 = -2 Implementation: c /* Program for rounding floating point numbers */ # inclu
1 min read
Problem in comparing Floating point numbers and how to compare them correctly? In this article, we will see what is the problem in comparing floating-point numbers and we will discuss the correct way to compare two floating-point numbers. What is the problem in comparing Floating-Point Numbers usually?Let us first compare two floating-point numbers with the help of relational
7 min read
Finding Floor and Ceil of a Sorted Array using C++ STL Given a sorted array, the task is to find the floor and ceil of given numbers using STL.Examples: Input: arr[] = {1, 2, 4, 7, 11, 12, 23, 30, 32}, values[] = { 1, 3, 5, 7, 20, 24 } Output: Floor Values: 1 2 4 7 12 23 Ceil values: 1 4 7 7 23 30 In case of floor(): lower_bound() method os STL will be
3 min read
iomanip setprecision() function in C++ with Examples The setprecision() method of iomanip library in C++ is used to set the ios library floating point precision based on the precision specified as the parameter to this method. Syntax: setprecision(int n) Parameters: This method accepts n as a parameter which is the integer argument corresponding to wh
2 min read