Comparisons Development

Float vs Double vs Decimal

Single-precision, double-precision and decimal number types compared: bits, digits of precision, range, the 0.1 + 0.2 problem and when to use each, with values taken from NumPy and Python.

Last reviewed:

AdSense Placeholder
Slot: header_reference_page
On this page

All three store numbers with a fixed number of digits, but float and double are binary (IEEE 754) while decimal types work in base 10. Inspect any value with the IEEE 754 Float Converter; the theory is in How Floating-Point Numbers Work and Floating-Point Number.

The types side by side

float (binary32) double (binary64) decimal (e.g. decimal128)
Total bits 32 64 128
Layout 1 sign, 8 exponent, 23 fraction 1 sign, 11 exponent, 52 fraction Sign, exponent, up to 34 decimal digits
Precision About 7 decimal digits About 15-16 decimal digits 34 decimal digits
Largest finite value 3.4028235 × 10^38 1.7976931348623157 × 10^308 9.999… × 10^6144
Machine epsilon 1.19 × 10^-7 2.22 × 10^-16 10^-33
Whole numbers exact up to 16,777,216 (2^24) 9,007,199,254,740,992 (2^53) All with up to 34 digits
Represents 0.1 exactly No No Yes

The classic surprise

  • In double precision 0.1 + 0.2 is 0.30000000000000004, and the nearest float to 0.1 is 0.10000000149011612.
  • Above 2^24, a float cannot tell neighbouring whole numbers apart: 16777216 + 1 is still 16777216.
  • In decimal arithmetic 0.1 + 0.2 is exactly 0.3 (Python decimal).
  • Compare floats with a tolerance, never ==.

Which to use

  • Graphics, audio, machine learning and games: float, where memory and speed matter more than the last digit.
  • Science, statistics and general numeric code: double.
  • Money, invoices, tax and anything people check by hand: decimal (or integer cents).
  • Identifiers and large counters: integers, never floating point.

Try these tools

See also

  • Guide How Floating-Point Numbers Work
    How computers store decimals as a sign, exponent and fraction (IEEE 754), why 0.1 is not exact, what rounding, subnormals.
  • Glossary Floating-Point Number
    A floating-point number stores a real number as a sign, an exponent and a fraction, like scientific notation in binary.
  • Glossary Scientific notation
    Scientific notation writes a number as a coefficient between 1 and 10 times a power of ten.
  • Glossary Significant figures
    Significant figures are the digits in a number that carry meaning about its precision, starting from the first non-zero digit.
  • Cheat sheet Binary, Octal & Hex Table Cheat Sheet
    0 to 31 in decimal, binary, octal and hex, the powers of two, hand-conversion shortcuts and the Base32 alphabet.

Frequently Asked Questions

Neither 0.1 nor 0.2 can be stored exactly in binary, just as 1/3 cannot be written exactly in decimal. Their stored values add up to 0.30000000000000004 in double precision. A decimal type stores them exactly and gives 0.3.

Use a decimal type, or integers counting the smallest unit such as cents. Binary floating point cannot represent most cent amounts exactly.

AdSense Placeholder
Slot: footer_leaderboard