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.
double d = 3.14159; float f = 3.14159f; double big = 1.5e10;
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
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. CompareMath.Abs(a - b) < tolerance, or usedouble.Epsilononly 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 withSort()still works becauseCompareToorders NaN first. - Mixing
floatanddoublepromotes todouble. Afloatvariable assigned adoubleexpression is a compile error until you cast. - Integer division trap.
1 / 2is0. Make one operand floating-point:1 / 2.0or1.0 / 2. - Culture.
ToString()andParseuse the current culture; on our runner that is the invariant culture. PassCultureInfo.InvariantCulturefor data formats. - Round-trip.
ToString("R")or the defaultToString()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
decimalor integer cents. - Speed.
doublemath is as fast asfloaton modern 64-bit CPUs;floatonly wins in memory use and SIMD width. - Half.
System.Halfis a 16-bit float for machine-learning data; it has no keyword.