C++ double Keyword
The double
keyword in C++ is used to declare variables of type double-precision floating-point. It is a built-in data type that can store decimal numbers with greater precision and a wider range compared to the float
type. The double
type is commonly used in scientific calculations and when higher precision is required for numeric data.
The size of a double
typically depends on the system and compiler but is generally 8 bytes (64 bits) on most modern systems.
Syntax
</>
Copy
double variable_name = value;
- double
- The keyword that specifies a double-precision floating-point variable.
- variable_name
- The name of the variable being declared.
- value
- An optional initial value for the variable. It can be a decimal number or integer.
Examples
Example 1: Declaring and Initializing a double
Variable
In this example, we will learn how to declare and initialize a double
variable.
</>
Copy
#include <iostream>
using namespace std;
int main() {
double pi = 3.14159; // Declare and initialize a double variable
cout << "Value of pi: " << pi << endl;
return 0;
}
Output:
Value of pi: 3.14159
Explanation:
- The variable
pi
is declared with the typedouble
and initialized to the value3.14159
. - The
cout
statement prints the value ofpi
to the console.
Example 2: Performing Arithmetic with double
Variables
In this example, we will learn how to do arithmetic operations using double
variables.
</>
Copy
#include <iostream>
using namespace std;
int main() {
double num1 = 5.5, num2 = 2.2;
double sum = num1 + num2;
double product = num1 * num2;
cout << "Sum: " << sum << endl;
cout << "Product: " << product << endl;
return 0;
}
Output:
Sum: 7.7
Product: 12.1
Explanation:
- The variables
num1
andnum2
are declared and initialized with decimal values. - Arithmetic operations are performed, and the results are stored in
sum
andproduct
. - The results are printed using
cout
.
Example 3: Comparing Precision of double
and float
In this example, we will show the precision difference between float
and double
types.
</>
Copy
#include <iostream>
#include <iomanip> // For std::setprecision
using namespace std;
int main() {
float floatVal = 3.14159265358979323846f; // Single-precision floating-point
double doubleVal = 3.14159265358979323846; // Double-precision floating-point
cout << fixed << setprecision(20);
cout << "Float value: " << floatVal << endl;
cout << "Double value: " << doubleVal << endl;
return 0;
}
Output:
Float value: 3.14159274101257324219
Double value: 3.14159265358979311600
Explanation:
- The
float
type has less precision, so its representation deviates after 6-7 decimal places. - The
double
type has higher precision, accurately representing values up to 15-16 decimal places. - The output shows the difference in precision between the two types.
Key Points about double
Keyword
- The
double
keyword is used to declare double-precision floating-point variables. - A
double
variable typically occupies 8 bytes (64 bits) in memory. - The precision of
double
is higher than that offloat
, making it suitable for scientific calculations. - The range of
double
is much wider, allowing it to store very large or very small values. - Use the
double
type when precision and accuracy are critical.