Updated April 13, 2023
Definition of C++ Double Data Type
C++ double is a versatile data type that is used internally for the compiler to define and hold any numerically valued data type especially any decimal oriented value. C++ double data type can be either fractional as well as whole numbers with values. These kinds of decimal oriented data type value can contain numbers till 15 digits and can either be prefixed with the decimal point or can be suffixed with the decimal point. Double data type is adapted mostly by programmers when compared with float and is often kept as default data type at the time of working with numbers associated with a huge number with decimals.
Syntax:
C++ double data type has a syntax which is represented as follows:
double var_name;
datatype considered here is double followed by a variable name which is var_name in this syntax. Also, a series of var_name somewhat in the following manner can be considered to optimize the code representation:
double var_name1, var_name2, var_name3…...var_nameN
How Double Data Type works in C++?
- Double data type in C++ has an interesting working pattern which is used and opted by most of the programmers.
- A double type data type can mostly accommodate the values ranging from 15-16 digits that is a 64-bit floating-point data type and hence can be arranged in a format when declared and initialized with decimal values as mentioned.
- The Range of the data type can be varied with values from 0 × 10−345to 1.7 × 10308
- Sometimes, a misconception is made when compared with a float which takes a value and can accommodate a lot many float values but when taken into consideration with the optimization and speed programmers will opt for double values.
- Preference will be to use double data type when the need is to deal with a huge decimal number.
Examples
Lets us discuss examples of C++ Double.
Example #1
This program demonstrates the C++ program for the double keyword where the user wants to make a conversion from Celsius temperature to Fahrenheit as shown in the output.
Code:
#include <iostream>
using namespace std;
int main()
{
double c_tmp, f_tmp;
cout << "Take an input in celsius and then convert it into some value: ";
cin >> c_tmp;
f_tmp = (c_tmp * 1.8) + 32;
cout << "Temperature in Fahreinheit: " << f_tmp;
return 0;
}
Output :
Example #2
This program demonstrates the addition, multiplication, and division of two numbers and providing the output where both the input numbers will be of type double as shown in the output.
Code:
#include <iostream>
using namespace std;
int main()
{
double n_1, n_2;
cout<<"First_Number to be entered as a double type: ";
cin>>n_1;
cout<<"Second_Number to be entered as a double type: ";
cin>>n_2;
cout<<"Sum of both the numbers entered: "<<(n_1+n_2);
cout<<" Product or multiplication of both the numbers entered: "<<(n_1*n_2);
cout<<" Result_for division of two numbers: " <<(n_1/n_2);
return 0;
}
Output:
Example #3
This program demonstrates function overloading where the function considering two integer numbers gets overridden by the function consisting of the data type with both the parameters as double as shown in the output.
Note: Both the function has integer and double value as a user input to get the result shown in the particular format.
Code:
#include <iostream>
using namespace std;
int sum_num(int, int);
double sum_num(double, double);
int main(){
int n_1, n_2, p;
double n_3, n_4, q;
cout<<" Two Integer_numbers: ";
cin>>n_1>>n_2;
cout<<"Rslt: "<<sum_num(n_1, n_2)<< endl;
cout<<" Two double_numbers: ";
cin>>n_3>>n_4;
cout<<"Rslt: " <<sum_num(n_3, n_4)<< endl;
}
int sum_num(int p, int q){
return p+q;
}
double sum_num(double p, double q){
return p+q;
}
Output:
Example #4
This program demonstrates a quadratic equation: x2 -4.0500000x + 2.999999 = 0, where the roots are just round off to 10 significant digits with root values as , r1 = 4.056785645 and r2 = – 6.0089767987. The output is shown as follows.
Code:
#include <stdio.h>
#include <math.h>
void doubl_sol(double p, double q, double r)
{
double d_b = q*q - 8.0*p*r;
double sol_d = sqrt(d_b);
double r_1 = (-q + sol_d) / (4.0*p);
double r_2 = (-q - sol_d) / (4.0*p);
printf("%.8f\t%.8f\n", r_1, r_2);
}
void flt_sol(float p, float q, float r)
{
float d_b = q*q - 4.0f*p*r;
float sol_d = sqrtf(d_b);
float r_1 = (-q + sol_d) / (6.60f*p);
float r_2 = (-q - sol_d) / (8.0f*p);
printf("%.8f\t%.8f\n", r_1, r_2);
}
int main(void)
{
float f_a = 1.02f;
float f_b = -4.0500000f;
float f_c = 2.99999f;
double d_a = 3.0;
double d_b = -6.0000000;
double d_c = 3.9989999;
flt_sol(f_a, f_b, f_c);
doubl_sol(d_a, d_b, d_c);
return 0;
}
Output:
Rules and Regulations for using Double in C++?
There is no specific rules and regulations when using double as a data type in C++ but still, some rules and regulations need to be followed which will help in making code more understandable and versatile in terms of usage when using double in C++ :
- A double data type should have a precision of 2X times to that of the float data type which means it should have 15 decimal digits of precision when compared with the float data type which has 7 decimal digits.
- The structural format of 15 decimal digit is computed in the following ways where the double has 52 mantissa bits and +1 hidden bit which is like (log 2^53) / log(10) = 15.95 digits. (which means approximately 15 to 16 digits).
- When the float data type is used by the programmers then in that case the precision comes out to be a big truncating error when calculated and repeated values are used frequently.
- The maximum value of float is 3e38, but double is about 1.7e308 thus proving the fact that round-off errors are not that precise as using float but not always sometimes.
Conclusion
C++ double data type has its own importance and significance when it is used with respect to a huge number of decimal digits. It also makes use of the internal computation of numbers and mantissa for precision making. Although float is used when the computation with compiler needs to be faster and efficient but still many programmers opt for double as it gives the decimal related values as return type whenever required.
Recommended Articles
This is a guide to C++ Double. Here we discuss the definition and How Double Data Type work in C++ along with examples and rules and regulations respectively. You may also have a look at the following articles to learn more –