Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Types and Literals

C# float and double floating-point types

Reference for C# float and double: precision, ranges, NaN and infinity, comparing with tolerance, literal suffixes, rounding errors and formatting.

float (32 bits, System.Single) and double (64 bits, System.Double) store approximate real numbers in IEEE 754 binary format. They cover an enormous range and are fast, which makes them the default for measurements, graphics, statistics and science. The price is that most decimal fractions such as 0.1 cannot be stored exactly, so results carry tiny rounding errors.

A literal with a decimal point or exponent is a double unless you add the suffix f or F. Use double by default; choose float for large arrays where memory or GPU formats matter. For money, use decimal.

C#
double d = 3.14159;
float f = 3.14159f;
double big = 1.5e10;

Try it

C# Precision and rounding error

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# NaN, infinity and formatting

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

C#
float  f = 1.5f;        // System.Single, 32 bits, about 6-9 significant digits
double d = 1.5;         // System.Double, 64 bits, about 15-17 significant digits
double e = 6.022e23;    // exponent notation
double s = 1_000.5;     // digit separators
Property float double
Size 4 bytes 8 bytes
Approximate range about ยฑ3.4 x 10^38 about ยฑ1.8 x 10^308
Smallest positive value float.Epsilon (about 1.4 x 10^-45) double.Epsilon (about 4.9 x 10^-324)
Reliable decimal digits 6 to 7 15
Literal suffix f or F none (or d or D)

Special values are NaN, PositiveInfinity and NegativeInfinity, available as constants (double.NaN) and tested with IsNaN, IsInfinity, IsFinite.

Conversions. int and long convert implicitly to float and double (possibly losing low digits); float to double is implicit; the reverse and any conversion to an integer type are explicit. Casting NaN or an out-of-range value to an integer gives a result that has varied between runtimes and processors; check the range first instead of relying on it.

Errors / undefined behavior. Floating-point arithmetic never throws: overflow gives infinity and invalid operations give NaN. checked has no effect on it.

Usage notes

  • Never compare with == after calculations. Compare Math.Abs(a - b) < tolerance, or use double.Epsilon only as the smallest denormal, which is not a sensible tolerance.
  • NaN is never equal to anything, including itself. Use double.IsNaN. Sorting a list that contains NaN with Sort() still works because CompareTo orders NaN first.
  • Mixing float and double promotes to double. A float variable assigned a double expression is a compile error until you cast.
  • Integer division trap. 1 / 2 is 0. Make one operand floating-point: 1 / 2.0 or 1.0 / 2.
  • Culture. ToString() and Parse use the current culture; on our runner that is the invariant culture. Pass CultureInfo.InvariantCulture for data formats.
  • Round-trip. ToString("R") or the default ToString() in modern .NET gives the shortest string that parses back to the same value.
  • Money. Do not use them for currency; sums of cents drift. Use decimal or integer cents.
  • Speed. double math is as fast as float on modern 64-bit CPUs; float only wins in memory use and SIMD width.
  • Half. System.Half is a 16-bit float for machine-learning data; it has no keyword.