Precision of floating point numbers in C++
The floating-point numbers can be represented using double, float and long double in C++.
Let’s see what’s the difference between using float and double?
The C++ provides us with several built-in data types that can be used for storing floating-point numbers in memory, including Float and Double.
The primary difference that we can figure out between these data types that we are considering here is that, compared to Float variables, using the Double variables we can store numbers with much larger magnitude (the part lying to the left of the decimal point)and also with more finer details (that is more precision digits to the right of the decimal point, known as the number’s precision).
In simple words as we can guess from the name itself that precision of Double is 2 times the precision of Floats.
In general, the double has 15 precision bits and the float has 7 precision bits.
Now let’s take the fraction 1/3 into consideration for understanding the concept. we can represent the number as 0.3333333333333333333… here it is the case with 3’s going out to infinity.
Here we can notice that after the division the number we are with would be close to 0.33333333333333…. we can note that the 3 would be repeated infinite times.
So to represent such a large number in our machines we would have to use a large number of bits to store the number, but we have limited addressing capacity of our machines.
So, the number of bits is fixed to address the issue with the division. Thus Floats and Double are properly exploited by the programmers.
So in simple words, the precision of a floating-point number defines how accurately we can represent a number without any information loss related to that data.
Now a good thing about the precision is that we can also use the precision by over-riding as per our need. Thus we can set the precision using setprecision. And this is present inside the iomanip header file.
The output of the program is also defined in the end.
So, let’s see an example of how it’s done.
#include <iostream> #include <iomanip> using namespace std; int main() { double x = 1.98765432d; cout << "Here we are printing up to 5 decimal places: " << setprecision(5) << x << endl; cout << "Here we are printing up to 7 decimal places: " << setprecision(7) << x << endl; cout << "Here we are printing up to 3 decimal places: " << setprecision(3) << x << endl; }
So, we get the result in this way:-
Here we are printing up to 5 decimal places: 1.9877 Here we are printing up to 7 decimal places: 1.987654 Here we are printing up to 3 decimal places: 1.99
So, we get the result in this way:-
Here we are printing up to 5 decimal places: 1.9877
Here we are printing up to 7 decimal places: 1.987654
Here we are printing up to 3 decimal places: 1.99
Other functions that can be used for precision and calculations are:-
- Floor() Function
- It rounds off the value to the lowest nearest integer.
- Ceil() Function
- It rounds off the value to the Largest nearest integer.
- round() Function
- Its rounds of the value to the nearest integer can be smaller or greater.
Now let’s take example code and that would clear the above usage of functions:-
The output of the program is also defined in the end.
#include <bits/stdc++.h> using namespace std; int main() { float mynumber1 = 0.345 , mynumber2 = 7334.9864, mynumber3 = -11.67894; cout<<"Values are : \n"; cout<<"Floor Number 1 : "<<floor(mynumber1)<<endl; cout<<"Ceil Number 2 : "<<ceil(mynumber2)<<endl; cout<<"Rounding of Number 3 : "<<round(mynumber3)<<endl; return 0; }
Values are : Floor Number 1 : 0 Ceil Number 2 : 7335 Rounding of Number 3 : -12
Leave a Reply