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.
Given below are few libraries and methods which are used to provide precision to floating point numbers in C++:
Floor rounds off the given value to the closest integer which is less than the given value.
1 1 1 -2 -2 -2
Ceil rounds off the given value to the closest integer which is more than the given value.
2 2 2 -1 -1 -1
Trunc rounds removes digits after decimal point.
1 1 1 -1 -1 -1
Rounds given number to the closest integer.
1 2 2 -1 -2 -2
Setprecision when used along with ‘fixed’ provides precision to floating point numbers correct to decimal numbers mentioned in the brackets of the setprecison.
3 -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
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 exists other methods too to provide precision to floating point numbers. The above mentioned are few of the most commonly used methods to provide precision to floating point numbers during competitive coding.
This article is contributed by Aditya Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Write a one line C function to round floating point numbers
- Can we use % operator on floating point numbers?
- C Program to Multiply two Floating Point Numbers
- Problem in comparing Floating point numbers and how to compare them correctly?
- Ceil and Floor functions in C++
- Finding Floor and Ceil of a Sorted Array using C++ STL
- How to count set bits in a floating point number in C?
- Floating Point Operations & Associativity in C, C++ and Java
- Convert a floating point number to string in C
- Rounding Floating Point Number To two Decimal Places in C and C++
- C++ Floating Point Manipulation (fmod(), remainder(), remquo() ... in cmath)
- trunc() , truncf() , truncl() in C language
- Setting decimal precision in C
- Measure execution time with high precision in C/C++
- sizeof() for Floating Constant in C
- Rotation of a point about another point in C++
- round() in C++
- Round to next smaller multiple of 8
- Round to next greater multiple of 8
- Puzzle | 3 cuts to cut round cake into 8 equal pieces