Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Types and Literals

Floating-point precision

Reference for floating-point precision pitfalls in C#: binary representation, 0.1 + 0.2, comparing with tolerance, accumulation, decimal and exact-rounding choices.

Computers store float and double values in binary. Fractions whose denominator is a power of two (0.5, 0.25, 0.125) are exact; most decimal fractions (0.1, 0.2, 0.3) are not, and are stored as the nearest representable number. Arithmetic then rounds again. The result is small errors that surprise people who expect decimal arithmetic.

This page explains where the errors come from and the patterns that keep them under control: comparing with tolerances, summing carefully, choosing decimal for money, and formatting for display.

C#
Console.WriteLine(0.1 + 0.2 == 0.3);   // False

Try it

C# Why 0.1 + 0.2 is not 0.3

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Accumulation, tolerance and rounding

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

There is no special syntax; the relevant tools are types, formats and helpers.

Tool Purpose
double, float Binary IEEE 754 values. About 15-17 and 6-9 significant decimal digits.
decimal 28-29 digit base-10 floating type; exact for decimal fractions.
ToString("R") / default Shortest text that parses back to the same value.
ToString("G17"), ToString("G9") Enough digits to show the exact stored value for double and float.
Math.Round(x, digits, mode) Rounds; default is MidpointRounding.ToEven.
Math.BitDecrement, Math.BitIncrement, Math.FusedMultiplyAdd Neighbor values and a more accurate multiply-add.
double.IsNaN, IsInfinity, IsFinite Detect special values.

Errors. Floating-point operations never throw. Overflow yields infinity, invalid operations like 0.0 / 0.0 yield NaN, and results too small for the type underflow to zero (or a subnormal).

Usage notes

  • Compare with a tolerance, not with ==. Use an absolute tolerance near zero and a relative one for large values. double.Epsilon is the smallest subnormal, not a general-purpose tolerance.
  • Round for display, not for storage. Format with ToString("F2") and keep full precision internally. Rounding at each step compounds error.
  • Use decimal for money and other decimal quantities with exact fractions. It is much slower than double and has a smaller range, so it is wrong for scientific work. Alternatively store integer cents.
  • Sum small numbers first or use pairwise or Kahan summation when adding many values of different sizes. LINQ's Sum for double is a plain loop.
  • float and double do not mix well. Converting 0.1f to double gives 0.10000000149011612. Choose one type for a calculation.
  • Integers are exact up to 2^53 in a double (9,007,199,254,740,992); beyond that, not every integer is representable.
  • Midpoint rounding. Math.Round(2.5) is 2 and Math.Round(3.5) is 4 (to even). Pass MidpointRounding.AwayFromZero for schoolbook rounding. A decimal-looking midpoint such as 2.675 is not exactly a midpoint as a double, so do not expect it to follow either rule; use decimal when the digits matter.
  • -0.0 exists. It equals 0.0 in comparisons, but double.IsNegative tells them apart, and 1 / -0.0 is negative infinity.
  • Library functions such as Math.Sin and Math.Pow may differ in the last digit between platforms, so avoid exact comparisons of their results.