- 0
Notifications Mark All Read
- Login
- Get Prime
Precision of floating point literals in CPP
Floating Point Literals in C++
While working with decimal type of data, it is very important to maintain the precision of the data, from preventing any mistakes. In C++ programming if we want to print a decimal value we use float and double data, which the the decimal value upto a certain point because of the restriction of their size. For example:
The decimal equivalent of 4/3 = 1.33333333333333333333333…
It will go upto infinity, and storing this data, will be a waste of memory, hence this will be displayed as
- 1.333333 or
- 1.3333
Depending upon the datatype that we use.
Use of Floating Point Literals
Floating Point Literals are used in C++ to represent different variations of a decimal data, there are mainly 5 different types of floating point literals in CPP, which have different uses and different precision, they are -:
- floor()
- ceil()
- round()
- trunc()
- setprecision()
Floor() – It is used for representing the closest smaller integer, to the decimal value. For Example-: 1.35 = 1
Ceil() – It is used for representing the closest larger integer, to the decimal value. For Example-: 1.35 = 2
Round() – It is used for representing the closest integer, to the decimal value. For Example-: 1.35 = 1 ; 1.65 = 2
Trunc() – It is used for removing the decimal values, after the decimal point. For Example-: 1.35 = 1
Setprecision() – It is used for representing the decimal value upto a certain decimal point.
Let’s see a CPP Code for the use of Floating point literals
CPP Code for using Floating Point Literals
#include<bits/stdc++.h> using namespace std; int main() { double x = 1.411, y = -1.500; cout << "Using Floor()" << floor(x) << endl; cout << "Using Floor()"<< floor(y) << endl; cout << "Using Ceil()"<< ceil(x) << endl; cout << "Using Ceil()"<< ceil(y) << endl; cout << "Using Round()"<< round(x) << endl; cout << "Using Round()"<< round(y) << endl; cout << "Using Trunc()"<< trunc(x) << endl; cout << "Using Trunc()"<< trunc(y) << endl; cout << "Using Setprecision()"<< fixed << setprecision(0)<< x << endl; cout << "Using Setprecision()"<< fixed << setprecision(1)<< x << endl; cout << "Using Setprecision()"<< fixed << setprecision(2)<< x << endl; cout << "Using Setprecision()"<< fixed << setprecision(3)<< x << endl; cout << "Using Setprecision()"<< fixed << setprecision(4)<< x << endl; cout << "Using Setprecision()"<< fixed << setprecision(0)<< y << endl; cout << "Using Setprecision()"<< fixed << setprecision(1)<< y << endl; cout << "Using Setprecision()"<< fixed << setprecision(2)<< y << endl; cout << "Using Setprecision()"<< fixed << setprecision(3)<< y << endl; cout << "Using Setprecision()"<< fixed << setprecision(4)<< y << endl; return 0; }
Output
Using Floor() 1
Using Floor() -2
Using Ceil() 2
Using Ceil() -1
Using Round() 1
Using Round() -2
Using Trunc() 1
Using Trunc() -1
Using Setprecision() 1
Using Setprecision() 1.4
Using Setprecision() 1.41
Using Setprecision() 1.411
Using Setprecision() 1.4110
Using Setprecision() -2
Using Setprecision() -1.5
Using Setprecision() -1.50
Using Setprecision() -1.500
Using Setprecision() -1.5000
Login/Signup to comment