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.
Console.WriteLine(0.1 + 0.2 == 0.3); // False
Try it
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.
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.Epsilonis 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
decimalfor money and other decimal quantities with exact fractions. It is much slower thandoubleand 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
Sumfordoubleis a plain loop. floatanddoubledo not mix well. Converting0.1ftodoublegives 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)is2andMath.Round(3.5)is4(to even). PassMidpointRounding.AwayFromZerofor schoolbook rounding. A decimal-looking midpoint such as 2.675 is not exactly a midpoint as adouble, so do not expect it to follow either rule; usedecimalwhen the digits matter. -0.0exists. It equals0.0in comparisons, butdouble.IsNegativetells them apart, and1 / -0.0is negative infinity.- Library functions such as
Math.SinandMath.Powmay differ in the last digit between platforms, so avoid exact comparisons of their results.