EMZETT.
Login

double (Floating-Point Number)

In short: A data type for numbers with decimal places (floating-point numbers) with double precision — the name “double” refers to the doubled memory size compared to the single-precision float.

In more detail: Floating-point numbers aren’t stored exactly internally, but approximately in binary — this can lead to small rounding errors in calculations (e.g. 0.1 + 0.2 doesn’t always give exactly 0.3). For monetary amounts, where exact precision matters, a special decimal type is therefore often used instead of double.

In Depth

0.1 + 0.2 == 0.3   # False! The result is actually 0.30000000000000004

This famous example surprises almost every beginner. The reason lies in the nature of binary floating-point representation (following the IEEE 754 standard, which practically all languages use for double/float): just as 1/3 can’t be represented exactly as a finite decimal number in the decimal system (0.333…), some decimal fractions like 0.1 can’t be represented exactly in the binary system — the stored value is always a very close, but not exactly identical, approximation.

These tiny rounding errors normally don’t add up noticeably and barely matter for scientific or graphical calculations. They become critical in two typical situations:

# DANGEROUS: direct equality comparison of two floating-point numbers
if computed_value == expected_value:   # can be wrongly False, due to rounding errors
 
# BETTER: comparison with a tolerance range
if abs(computed_value - expected_value) < 0.0001:

And for monetary amounts, where exact precision is legally and practically mandatory — a rounding error of a few cents that adds up over millions of transactions isn’t an acceptable risk. This is why financial systems almost never use double directly for monetary amounts, but instead either a special, exact decimal type (e.g. Java’s BigDecimal, Python’s Decimal) or consistently calculate in the smallest unit as an integer (cents instead of euros), to completely avoid rounding problems.

double offers roughly double the number of significant decimal places compared to the single-precision float (about 15-17 instead of 6-7), but also needs twice as much memory for it (64 instead of 32 bits) — in most modern languages and applications, double is therefore the default, float is used especially where memory or speed advantages for huge amounts of data (e.g. graphics programming) justify the loss of precision.

See also: byte, Type Casting