Short answer
In C and C++, double is a floating-point type. It stores numbers with a decimal point, such as 3.14 or -0.5, and very large or very small values written in scientific notation. On most common systems a double takes 8 bytes and gives you roughly 15 significant decimal digits.
Use double when you need fractions or measurements, such as a temperature, a length or the result of a square root. For whole numbers, int is usually the better choice.
Declaring and assigning doubles
double temperature = 21.5;
double tax_rate = 0.08;
double distance = 2.5e3; // 2500.0, written in scientific notation
A literal with a decimal point or an exponent is a double by default. Add f to make it a float (2.5f), or L to make it a long double (2.5L).
If you’d rather the compiler pick the type, auto deduces double from a literal:
auto ratio = 0.75; // ratio is a double
Doing math with doubles
When one operand is a double, the arithmetic is done in floating point. This matters most with division:
#include <iostream>
int main() {
int a = 7;
int b = 2;
std::cout << a / b << '\n'; // 3: integer division drops the remainder
std::cout << a / 2.0 << '\n'; // 3.5: the 2.0 makes the division floating point
}
The common functions in <cmath>, such as std::sqrt, std::pow and std::fabs, work with double values and return double values:
#include <cmath>
#include <iostream>
int main() {
std::cout << std::sqrt(2.0) << '\n'; // 1.41421
}
Printing and reading doubles
std::cout shows a double with up to six significant digits by default and leaves off trailing zeros. To choose the number of decimal places, use std::fixed and std::setprecision:
#include <iomanip>
#include <iostream>
int main() {
double third = 1.0 / 3.0;
std::cout << third << '\n'; // 0.333333
std::cout << 2.0 << '\n'; // 2
std::cout << std::fixed << std::setprecision(2)
<< third << '\n'; // 0.33
}
Reading a value works the same way as reading an int:
#include <iostream>
int main() {
double radius = 0.0;
std::cout << "Enter a radius: ";
std::cin >> radius;
if (!std::cin) {
std::cout << "That was not a number.\n";
}
}
If the input isn’t a valid number, the stream enters a failed state, and the check above catches it.
How much precision you get
The C++ standard doesn’t fix the size of double. It does require that double have at least as much precision as float. On most desktop, laptop and server systems, double uses the IEEE 754 double-precision format, which is 64 bits. That gives roughly 15 reliable decimal digits.
To see the size on your own machine, use sizeof:
#include <iostream>
int main() {
std::cout << sizeof(double) << '\n'; // usually 8
}
For how that operator works in more detail, see the sizeof function in c.
A double can’t store every decimal fraction exactly. In binary, a value such as 0.1 repeats forever, so the computer stores the closest value it can. Most of the time the difference is invisible, but it causes the surprises below.
Floating-point surprises
Because each stored value is an approximation, results that look exact on paper may not be exact in code:
#include <iomanip>
#include <iostream>
int main() {
double sum = 0.1 + 0.2;
std::cout << std::setprecision(17) << sum << '\n'; // 0.30000000000000004
std::cout << (sum == 0.3) << '\n'; // 0, meaning false
}
A few habits follow from this:
- Avoid
==on doubles unless you have a specific reason. Check whether two values are close enough instead:
#include <cmath>
bool nearly_equal(double a, double b, double tolerance) {
return std::fabs(a - b) < tolerance;
}
- Choose the tolerance to fit your numbers. A tolerance that works for values near 1 can be too strict for values in the millions.
- Be careful with money. A common recommendation is to store amounts as whole cents in an
intorlong long, or to use a decimal library, rather than adouble.
double, float and long double
C++ has three floating-point types: float, double and long double. double is one of the primitive data types in c, along with int and char.
floatusually takes 4 bytes and holds about 6 or 7 significant digits. It uses less memory but is less precise.doubleusually takes 8 bytes and holds about 15 significant digits. It is the default for decimal literals and the usual choice in most code.long doubleis at least as precise asdouble. Its exact size and precision depend on the compiler and platform.
Start with double unless you have a clear reason to use float, such as a very large array where memory matters.
double in C
C has the same double type. The main difference is input and output. C uses printf and scanf with format specifiers, not cout and cin:
#include <stdio.h>
int main(void) {
double value = 3.14159;
printf("%f\n", value); // 3.141590
printf("%.2f\n", value); // 3.14
return 0;
}
To read a double with scanf, use %lf, not %f, and pass the address of the variable: scanf("%lf", &value);.
Key takeaways
doublestores decimal numbers, usually in 8 bytes, with about 15 reliable digits.- Arithmetic is floating point when one operand is a
double, so7 / 2.0is 3.5. - Expect small rounding errors, and avoid
==when comparing doubles. - Use
%fto print a double withprintf, and%lfto read one withscanf.