What is C++ double data type?
- The double is among one of the available data types in C++ like float, int, char, etc.
- The double data type allows storing bigger floating point numbers (decimal numbers) than the float data type.
- It takes eight bytes (64 bits) in the memory whereas float takes four bytes.
- Generally, it’s precision is 15 digits.
How to declare a double variable in C++?
You may declare double variables in these ways:
double var;
double var, var2, var3;
double var = 500.987678767;
double var = -500.877878787;
double var = 500;
float var = 87; //Declaring a float type variable without suffix f or F is taken as double type as well.
C++ Program for declaring and using double type variables
In this C++ program, we will create a few double-type variables and display their values by using cout. Have a look:
#include<iostream> using namespace std; int main(){ // Declaring and assigning float variables double d1 = 30.0; double d2 = -30.5; double d3 = 25.994524; double d4 = 4E-6; //An Exponential Number //Displaying Double variable values cout << "First Double Variable Value = " << d1 << endl; cout << "Second Double Variable Value = " << d2 << endl; cout << "Third Double Variable Value = " << d3 << endl; cout << "Fourth Double Variable Value = " << d4 << endl; return 1; }
Output:
Second Double Variable Value = -30.5
Third Double Variable Value = 25.9945
Fourth Double Variable Value = 4e-06
Check the memory size of double variables example
As mentioned earlier, a double variable takes eight bytes in memory. The example below shows the actual size by using the sizeof() function.
See the program and its output below:
#include<iostream> using namespace std; int main(){ // Declaring and assigning float variables double d1 = 30.0; double d2 = -30.5; double d3 = 25.994524; double d4 = 4E-6; //An Exponential Number //Displaying Double variable values cout << "First Double Variable Size = " << sizeof(d1) << endl; cout << "Second Double Variable Size = " << sizeof(d2) << endl; cout << "Third Double Variable Size = " << sizeof(d3) << endl; cout << "Fourth Double Variable Size = " << sizeof(d4) << endl; return 1; }
Output:
Second Double Variable Size = 8
Third Double Variable Size = 8
Fourth Double Variable Size = 8
Checking the minimum and maximum limits of the double type variables example
The example below shows using the numeric_limits to check the minimum and maximum limits of the double-type variables. You have to include the <limits> in the header.
#include <iostream> #include <limits> using namespace std; int main(){ cout << "The Max and Min Double Limit:" << endl; cout << "min: " << numeric_limits<double>::min() << endl; cout << "max: " << numeric_limits<double>::max() << "\n\n"; cout << "The Max and Min Float Limit:" << endl; cout << "min: " << numeric_limits<float>::min() << endl; cout << "max: " << numeric_limits<float>::max() << "\n\n"; cout << "The Max and Min Long Double Limit:" << endl; cout << "min: " << numeric_limits<long double>::min() << endl; cout << "max: " << numeric_limits<long double>::max() << "\n\n"; return 1; }
Output:
Checking and comparing the precision of double types
The following example checks the precision of double-type variables and compares it with other floating type data types like float and long double.
#include<iostream> #include<cstdlib> using namespace std; int main(){ // Declaring and assigning floating types variables double dbl1 = 50.8451425421425; float flt1 = 5.8451425421425f; long double ld1 = 5.8451425421425L; //Displaying variable size cout << "Double Variable = " << dbl1 << endl; cout << "Float Variable = " << flt1 << endl; cout << "Long Double Variable = " << ld1 << endl; return 1; }
Output:
Float Variable = 5.84514
Long Double Variable = 5.84514
Now let us use the setprecision() function and see the result
#include<iostream> #include <iomanip> #include<cstdlib> using namespace std; int main(){ // Declaring and assigning floating types variables double dbl1 = 50.8451425421425; float flt1 = 5.8451425421425f; long double ld1 = 5.8451425421425L; //Setting precision cout << setprecision(11); //Displaying variable size cout << "Double Variable = " << dbl1 << endl; cout << "Float Variable = " << flt1 << endl; cout << "Long Double Variable = " << ld1 << endl; return 1; }
Result:
Float Variable = 5.8451423645
Long Double Variable = 5.8451425421
You saw the values for double and long double are displayed correctly with the 11 precision setting. However, the float is showing a garbage value after 7.