Math concepts

Floating point numbers: how they work and why 0.1 + 0.2 ≠ 0.3

How the sign, exponent and significand fit together, single vs double precision in one table, 0.1 converted to binary bit by bit, special values, and practice questions with answers.

12 min readBy Hira Noor, mathematicianUpdated
The short answerFloating point numbers are how computers store real numbers: each value is kept as a sign, a significand (the digits) and an exponent, like scientific notation in base 2. For example, 6.25 is stored as 1.5625×221.5625 \times 2^2. The format holds a fixed number of binary digits, so most decimal fractions, including 0.1, can only be stored approximately. That is why 0.1 + 0.2 gives 0.30000000000000004 in most programming languages.

Want to see it for yourself?

Convert 0.1 to binary in the number base converter and watch the 0011 pattern repeat. The scientific calculator shows 0.1 + 0.2 as exactly 3/10, because it reads typed decimals as fractions instead of binary floating point.

What is a floating point number?

A floating point number is a number written as significand × base^exponent, with a fixed number of digits in the significand. Scientific notation already works this way: the speed of light is 2.998 × 10⁸ m/s, and a hydrogen atom is about 1.1 × 10⁻¹⁰ m across. The point “floats” because the exponent moves it, so one format covers both huge and tiny values with the same number of significant digits.

Computers use base 2 instead of base 10. In the IEEE 754 standard, which nearly all hardware follows, a floating point number has three fields:

Sign bit
0 for positive, 1 for negative.
Exponent
The power of 2, stored with a bias added so it is never negative: 127 in single precision, 1023 in double precision.
Significand (mantissa)
The binary digits after the leading 1. A normal number always starts with 1 in binary, so that 1 is implied and not stored, which buys one free bit of precision.

The value of a normal floating point number is then

x=(−1)sign×1.fraction2×2exponent−biasx = (-1)^{\text{sign}} \times 1.\text{fraction}_2 \times 2^{\,\text{exponent} - \text{bias}}

Single vs double precision: the IEEE 754 formats

The two formats you meet most often are single precision (binary32, float in C, C++ and Java) and double precision (binary64, double in C, C++ and Java, and the only number type in JavaScript; Python’s float is a double too). Spreadsheets such as Excel also calculate in double precision.

IEEE 754 single and double precision compared
PropertySingle precision (binary32)Double precision (binary64)
Total bits3264
Sign / exponent / fraction bits1 / 8 / 231 / 11 / 52
Significant bits (with the implied 1)2453
Exponent bias1271023
Decimal digits of precisionabout 7 (6 to 9)about 15 to 17
Machine epsilon (gap above 1)2−23≈1.19×10−72^{-23} \approx 1.19 \times 10^{-7}2−52≈2.22×10−162^{-52} \approx 2.22 \times 10^{-16}
Largest finite value≈3.40×1038\approx 3.40 \times 10^{38}≈1.80×10308\approx 1.80 \times 10^{308}
Smallest normal positive value2−126≈1.18×10−382^{-126} \approx 1.18 \times 10^{-38}2−1022≈2.23×10−3082^{-1022} \approx 2.23 \times 10^{-308}
Smallest subnormal positive value2−149≈1.4×10−452^{-149} \approx 1.4 \times 10^{-45}2−1074≈4.9×10−3242^{-1074} \approx 4.9 \times 10^{-324}
Every integer exact up to224=16,777,2162^{24} = 16{,}777{,}216253=9,007,199,254,740,9922^{53} = 9{,}007{,}199{,}254{,}740{,}992

Floating point number example: storing −6.25

  1. Write it in binary

    6 = 110₂ and 0.25 = 0.01₂, so 6.25 = 110.01₂.

  2. Normalize

    Move the point until one 1 sits before it: 110.012=1.10012×22110.01_2 = 1.1001_2 \times 2^2.

  3. Sign

    The number is negative, so the sign bit is 1.

  4. Exponent

    Add the double-precision bias: 2 + 1023 = 1025 = 10000000001₂ (11 bits).

  5. Fraction

    Drop the leading 1 and keep the rest, 1001, padded with zeros to 52 bits.

  6. Result

    The 64 bits are 1 10000000001 1001000…0, which is C019000000000000 in hex. In single precision the same steps use bias 127 (exponent 129 = 10000001₂) and a 23-bit fraction, giving C0C80000. Because 6.25 = 25/4 has a power of 2 as its denominator, it is stored exactly.

Why 0.1 has no exact binary form

In decimal, a fraction in lowest terms terminates only if its denominator has no prime factors other than 2 and 5. That is why 1/4 = 0.25 terminates while 1/3 = 0.333… repeats forever. Binary follows the same rule with the single prime 2: a fraction terminates in binary only if its denominator is a power of 2. So 0.5, 0.25 and 0.375 = 3/8 are exact, but 0.1 = 1/10 is not, because 10 = 2 × 5.

To convert a decimal fraction to binary, multiply it by 2 again and again; each whole-number part is the next binary digit:

  1. 0.1 × 2 = 0.2

    Digit 0, carry on with 0.2.

  2. 0.2 × 2 = 0.4

    Digit 0, carry on with 0.4.

  3. 0.4 × 2 = 0.8

    Digit 0, carry on with 0.8.

  4. 0.8 × 2 = 1.6

    Digit 1, carry on with 0.6.

  5. 0.6 × 2 = 1.2

    Digit 1, carry on with 0.2, which already appeared in step 2. From here the digits 0011 repeat forever.

0.110=0.00011‾2=0.000110011001100110011…20.1_{10} = 0.0\overline{0011}_2 = 0.000110011001100110011\ldots_2

A double keeps 53 significant bits and rounds the rest. Normalized, 0.1=1.1001100110011…2×2−40.1 = 1.1001100110011\ldots_2 \times 2^{-4}, so the stored bits are:

IEEE 754 double precision bit layout storing 0.1: sign bit 0, exponent bits 01111111011, and 52 fraction bits of repeating 1001 rounded up at the end
The 64 bits of 0.1 in double precision: 1 sign bit, 11 exponent bits and 52 fraction bits.
sign      0
exponent  01111111011               (1019 − 1023 = −4)
fraction  1001100110011001100110011001100110011001100110011010
hex       3FB999999999999A
0.1 in IEEE 754 double precision. The repeating 1001 is rounded up at the end to …1010, so the stored value is slightly more than 0.1.

Converted back to decimal, the value actually stored for 0.1 is

0.10000000000000000555111512312578270211815834045410156250.1000000000000000055511151231257827021181583404541015625

Why 0.1 + 0.2 is not 0.3

The value 0.2 is also stored slightly high. When the two are added, the exact sum of the stored values is rounded to the nearest double, which is 0.3000000000000000444…. The double nearest to 0.3, however, is 0.2999999999999999888…. These are neighboring doubles, one step apart (hex 3FD3333333333334 and 3FD3333333333333), so 0.1 + 0.2 == 0.3 is false, and printing the sum with 17 significant digits shows 0.30000000000000004.

This happens in every language that uses double precision, including JavaScript, Python, Java, C, C++ and C#. It is not a bug in the language or the processor: it is the correctly rounded result of adding two numbers that were already approximations.

Machine epsilon: how accurate floating point is

The gap between 1 and the next larger double is called machine epsilon:

ε=2−52≈2.220446049250313×10−16\varepsilon = 2^{-52} \approx 2.220446049250313 \times 10^{-16}

IEEE 754 requires the basic operations (addition, subtraction, multiplication, division and square root) to be correctly rounded: the result must be the exact answer rounded to the nearest representable number. Each single operation therefore has a relative error of at most ε/2, about 1.1 × 10⁻¹⁶. That is like measuring the distance from New York to Los Angeles to better than a nanometer.

Gaps between floating point numbers grow with their size. Between 1 and 2 the step is 2−522^{-52}; between 1024 and 2048 it is 2−422^{-42}; above 2532^{53} it is 2, so odd integers can no longer be stored. The relative precision stays the same, about 16 significant digits, wherever the number is. The trouble is never one operation on its own. It is what later operations do with the small errors already present.

Special values: infinity, NaN, overflow and underflow

The largest and smallest exponent codes are reserved for values that are not ordinary numbers, so a calculation can keep going instead of crashing:

IEEE 754 special values and when they appear
ValueHow it is storedExample in double precision
Zero (±0)Exponent and fraction all 0s; the sign bit gives +0 or −0−1e-200 × 1e-200 gives −0
Subnormal numbersExponent all 0s, fraction not 0; no implied leading 1Values between 4.9×10−3244.9 \times 10^{-324} and 2.2×10−3082.2 \times 10^{-308}, with fewer significant digits
Infinity (±∞)Exponent all 1s, fraction 01e308 × 10 gives Infinity; 1/0 gives Infinity in most languages
NaN (not a number)Exponent all 1s, fraction not 00/0, ∞ − ∞ or the real square root of −1; NaN is not equal even to itself

Overflow means a result is too large for the format and becomes infinity. Underflow means a result is too small: it first loses precision as a subnormal, then rounds to zero. Half of the smallest subnormal double, 5e-324 / 2, is 0. On a handheld calculator, “underflow” or a result of 0 for something like 10−200×10−20010^{-200} \times 10^{-200} means the true answer is below the smallest number the calculator can show, not that it is exactly zero.

How calculators hide the noise

A calculator that showed all 17 digits would display rounding residue constantly. Instead, calculators carry more digits internally than they display, typically showing 10 to 12 significant digits. Rounded to 10 significant digits, 0.30000000000000004 is simply 0.3. Many handheld calculators also work in decimal internally, so 0.1 is exact on them in the first place.

The ScientifiCalc scientific calculator does both of these things and one more. Its default Norm format shows up to 10 significant digits, and in its default Exact when possible mode, decimals you type are read as exact fractions. 0.1 + 0.2 is computed as 1/10 + 2/10 = 3/10 exactly, so no floating-point error occurs at all. Double precision is used only when a result cannot stay rational, for example after the square root of a non-square, a logarithm or a trigonometric function. The methodology page lists the precision of each kind of result.

Catastrophic cancellation: when rounding errors grow

Subtracting two nearly equal numbers cancels their leading digits, which were correct, and leaves the trailing digits, which hold the rounding error. The relative error of the difference can be enormous even though each input was accurate to about 16 digits.

Example 1: an expression that collapses to zero

The expression 1−cos⁡xx2\frac{1 - \cos x}{x^2} approaches 1/2 as x approaches 0. Evaluate it in double precision at x=10−8x = 10^{-8} and the result is 0. The true value of cos(10−8)\cos(10^{-8}) is 1−5×10−171 - 5 \times 10^{-17}, which lies less than half a step below 1 (doubles just below 1 are about 1.1×10−161.1 \times 10^{-16} apart), so the stored cosine is exactly 1 and the numerator becomes 0.

The cure is to rewrite the formula so that no close numbers are subtracted. With the identity 1−cos⁡x=2sin2(x/2)1 - \cos x = 2\sin^2(x/2),

1−cos⁡xx2=2sin2(x/2)x2\frac{1 - \cos x}{x^2} = \frac{2\sin^2(x/2)}{x^2}

and the right-hand form evaluates to 0.5 at x=10−8x = 10^{-8}, correct to full precision.

Example 2: the quadratic formula

Take x2+108x+1=0x^2 + 10^8 x + 1 = 0, whose small root is extremely close to −10−8-10^{-8}. The textbook formula x=−b+b2−4ac2ax = \frac{-b + \sqrt{b^2 - 4ac}}{2a} subtracts two numbers, 10810^8 and 1016−4\sqrt{10^{16} - 4}, that differ by only about 2 parts in 101610^{16}, and in double precision it returns about −7.45×10−9-7.45 \times 10^{-9}, roughly 25% wrong. The algebraically equivalent form

x=2c−b−b2−4acx = \frac{2c}{-b - \sqrt{b^2 - 4ac}}

adds the two large numbers instead of subtracting them and gives −1.0×10−8-1.0 \times 10^{-8}, correct to every digit shown. Numerical software uses rearrangements like this one routinely.

Other floating point rounding errors you will meet

  • Repeated addition. Adding 0.1 to a running total ten times gives 0.9999999999999999, not 1, because each addition rounds.
  • Order of operations. Floating point addition is not associative: (0.1 + 0.2) + 0.3 gives 0.6000000000000001, while 0.1 + (0.2 + 0.3) gives 0.6. Summing the same list in a different order can change the last digits.
  • Equality tests. A program that checks whether a computed value equals 0.3 may fail. Compare with a tolerance instead, such as |x − 0.3| < 10⁻⁹, or a tolerance scaled to the size of x.
  • Large integers. Doubles represent every integer exactly only up to 2⁵³ = 9,007,199,254,740,992. Beyond that, gaps appear: 2⁵³ + 1 cannot be stored and rounds to 2⁵³. ScientifiCalc keeps integer results exact with arbitrary-precision arithmetic, so 30! is shown in full as 265252859812191058636308480000000.
  • Money. Amounts such as 0.10 cannot be stored exactly in binary, so financial software usually counts in whole cents or uses decimal arithmetic. When you convert currency, round only the final amount to the smallest unit: whole cents for US dollars to euros, whole yen for US dollars to Japanese yen.
# Python
import math
from decimal import Decimal
0.1 + 0.2 == 0.3                                  # False
math.isclose(0.1 + 0.2, 0.3)                      # True (relative tolerance 1e-9)
Decimal('0.1') + Decimal('0.2') == Decimal('0.3')  # True (decimal arithmetic)

// JavaScript
Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON        // true
Comparing floating point numbers safely: use a tolerance, or a decimal type when the values are money.

Floating point vs integers vs decimal types

Which number type to use
TypeStoresExact forUse it for
Integer (int)Whole numbers onlyEvery whole number in its rangeCounts, indexes, money in whole cents
Binary floating point (float, double)Sign, binary significand, exponentFractions with a power-of-2 denominator, such as 0.5 or 6.25Science, engineering, graphics, statistics
Decimal type (Python decimal, Java BigDecimal, C# decimal)Decimal digits and a scaleDecimal fractions such as 0.1 and 19.99Prices, accounting, tax
Exact fraction (rational)Integer numerator and denominatorEvery fraction, including 1/3Algebra, and exact calculator answers

In code, 2 is usually an integer and 2.0 a float, even though they are the same number. Both are stored exactly; the type only decides which rules later arithmetic follows.

When floating point error matters, and when it does not

For everyday calculations, floating-point error is far smaller than every other source of error. A length measured to the nearest millimeter over 10 m has a relative uncertainty of 10⁻⁴, about a trillion times larger than double-precision rounding. It does matter when:

  • you subtract nearly equal quantities, as in differences of large numbers, numerical derivatives with very small steps, or the variance shortcut x2‾−x¯2\overline{x^2} - \bar{x}^2;
  • you repeat an operation millions of times and the errors accumulate;
  • you test computed numbers for exact equality;
  • you need exact integers above 2⁵³ or exact decimal amounts.

In those cases, rearrange the formula, switch to exact arithmetic with fractions or integers, or compare with a tolerance. When the issue is simply how many digits to report, the significant figures calculator rounds a result to the precision your data supports, and exact vs decimal answers explains when to keep a result as a fraction.

Practice questions

  1. Is 0.375 stored exactly as a double?

    Yes. 0.375 = 3/8, and 8 is a power of 2, so in binary it is 0.011₂, which ends.

  2. Is 0.7 stored exactly?

    No. 0.7 = 7/10, and the factor 5 in 10 makes the binary expansion repeat, so it is rounded.

  3. What is the biased exponent of 1.0 in double precision?

    1.0 = 1.0 × 2⁰, so the stored exponent is 0 + 1023 = 1023, or 01111111111₂. The full double is 3FF0000000000000.

  4. Can a double store 9,007,199,254,740,993 exactly?

    No. That is 2⁵³ + 1, and above 2⁵³ doubles are 2 apart, so it rounds to 9,007,199,254,740,992.

  5. About how many significant digits does a 32-bit float keep?

    About 7. It has 24 significant bits, and 24log10⁡2≈7.224 \log_{10} 2 \approx 7.2. This is why float 0.1 prints as 0.100000001 at 9 digits.

Frequently asked questions

What is a floating point number, with an example?

A floating point number stores a value as a sign, a significand and an exponent, like scientific notation in base 2. For example, 6.25 = 110.01 in binary = 1.1001 × 2², so a double stores sign 0, exponent 2 + 1023 = 1025 and the fraction bits 1001 followed by zeros.

Is 0.1 + 0.2 = 0.30000000000000004 a bug?

No. It is the correctly rounded sum of the binary approximations of 0.1 and 0.2, exactly as the IEEE 754 standard specifies. Every language that uses double precision gives the same result.

Are floating point numbers real numbers?

Every finite floating point number is a real number, and in fact a rational one: a whole number times a power of 2. But there are only finitely many of them, so most real numbers, such as 0.1, 1/3, √2 and π, are not floating point numbers and are stored as the nearest one instead.

Is 2.0 an integer or a float?

As a number, 2.0 equals the integer 2. In most programming languages, writing the decimal point makes it a float (a double), while 2 without a point is an integer. Both are stored exactly, because 2 is a power of 2.

How many digits are in a floating point number?

A double-precision number keeps 53 significant bits, about 15 to 17 significant decimal digits: 15 digits always survive a round trip, and 17 are enough to identify any double. A single-precision float keeps 24 bits, about 7 digits.

Which has more precision, float or double?

Double. It has 53 significant bits against 24 for float, so it carries about 16 decimal digits instead of about 7, and a far wider range, up to about 1.8 × 10³⁰⁸ instead of 3.4 × 10³⁸.

How do you fix a floating point rounding error?

Compare numbers with a tolerance instead of ==, for example math.isclose in Python. Use integers (whole cents) or a decimal type for money, and rearrange formulas that subtract nearly equal numbers. To show a result, round it to the digits you need rather than printing all 17.

What does underflow mean on a calculator?

Underflow means the result is smaller in size than the smallest number the calculator can represent, so it is shown as 0 or flagged as an error. The true answer is not zero; it is just too small for the format, such as 10⁻²⁰⁰ × 10⁻²⁰⁰ in double precision.

Why does my calculator show 0.3?

It rounds the display to 10 to 12 significant digits, which hides the error in the 17th digit, and many calculators work in decimal internally. ScientifiCalc’s exact mode goes further and avoids the error entirely by adding 1/10 and 2/10 as fractions.

Try it yourself

Scientific Calculator

Type or tap expressions with exact fractions, memory, variables, constants and complex mode.

Hex to Decimal Converter

Hex to decimal with place-value steps, plus binary, octal and any base 2 to 36, fractions included.

Sig Fig Calculator

Count and round sig figs, or add, subtract, multiply and divide with the sig fig rules.

Rounding Calculator

Round to any decimal place, place value, sig fig or multiple, with 7 rules and the working shown.

Browse every article and quick tip in calculator tips and math concepts.