Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

double — floating-point type

double is a standard floating-point type with at least as much precision as float. It stores values with fractional parts, but its precision is finite and it does not guarantee an exact representation of every decimal number.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

double name = value;

double is a built-in standard floating-point type. It provides at least as much precision as float, and long double provides at least as much precision as double. C++ does not guarantee that double occupies 64 bits or uses the IEEE 754 binary64 format.

Floating-point literals without a suffix, such as 2.5 and 1e3, have type double. The suffix f or F means float, while l or L means long double. The declaration double name = value; creates an object of type double and initializes it with the supplied value.

The destination variable's type does not change how an expression is evaluated beforehand. In the example, 5 / 2 performs integer division and produces 2, which is only then converted to double. The expression 5.0 / 2.0 performs floating-point division. On the tested implementation, the program prints 2.5 for this expression. In C++23, the accuracy of floating-point operations is implementation-defined, and the standard does not require a radix in which 2.5 is exactly representable.

Precision is finite: on binary implementations, values such as 0.1 have no exact representation. Therefore, do not assume that calculations preserve every decimal digit. Inspect implementation properties using std::numeric_limits<double> from <limits>, for example digits10 and is_iec559. The supplied source is an evolving standard draft, not a frozen C++23 publication.

Examples

#include <iostream>

int main() {
    double truncated = 5 / 2;
    double quotient = 5.0 / 2.0;

    std::cout << truncated << '\n';
    std::cout << quotient << '\n';
}

Result

2
2.5

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources