double, float and decimal
Three kinds of number
C# has three fractional types, and two of them are a compromise:
- double is a 64-bit binary floating-point number: 15 to 17 significant decimal digits, a huge range, fast, and it cannot store 0.1 exactly.
- float is the 32-bit version: about 7 significant digits, used for graphics and for data that is already in float.
- decimal is a 128-bit decimal number: 28 to 29 significant digits, exact for the fractions written with a decimal point, and slower.
The m suffix makes a literal a decimal, and f a float. A literal with a point and no suffix is a double.
Binary rounding
A double stores the number as a sum of powers of two, and 0.1 is not one of them: the nearest double is a little larger than 0.1. Adding two of them shows the error:
Console.WriteLine(0.1 + 0.2);
Console.WriteLine(0.1m + 0.2m);
Console.WriteLine(0.1f + 0.2f);
Console.WriteLine(1.0 / 3.0);
Console.WriteLine(1m / 3m);
Console.WriteLine(decimal.Round(1m / 3m, 4));
dotnet run
0.30000000000000004
0.3
0.3
0.3333333333333333
0.3333333333333333333333333333
0.3333
The first line is the famous one: the sum of two doubles is not 0.3, it is
0.30000000000000004, and a comparison
0.1 + 0.2 == 0.3 is
false. The float sum prints 0.3 because a
float carries too few digits to show the error, not
because it has none.
A program that compares doubles therefore compares a difference with a
tolerance: Math.Abs(a - b) < 1e-9. A program that
shows a number to a person rounds it at the last step, with a format or
Math.Round(x, digits).
decimal is exact
A decimal stores decimal digits, so 0.1 and 0.2 are exactly what they say, and their sum is exactly 0.3. That is why money is a decimal: a cent is a decimal digit, and a double would lose it.
decimal price = 19.99m;
decimal quantity = 3;
decimal total = price * quantity;
Console.WriteLine(total);
Console.WriteLine(total == 59.97m);
double sum = 0;
for (int i = 0; i < 10; i++)
{
sum += 0.1;
}
Console.WriteLine(sum);
Console.WriteLine(sum == 1.0);
dotnet run
59.97
True
0.9999999999999999
False
The multiplication happens to be exact in binary here, and that is the trap:
a wrong type sometimes prints the right answer. The sum of ten
0.1 doubles is where it shows, at
0.9999999999999999 rather than 1.0. A price is a decimal number, and the
type should say so.
decimal is not a universal replacement: it is slower,
its range ends at about 7.9e28, and the BCL functions for geometry and
physics take double. Use it where the digits are
decimal — money, prices, measurements with a fixed number of places.
Which one to use
| Type | Digits | Suffix | Use it for |
|---|---|---|---|
| double | 15-17 | none | science, geometry, time intervals, anything measured |
| float | 7 | f | graphics, large arrays of samples, an API that takes float |
| decimal | 28-29 | m | money, prices, exact decimal arithmetic |
The other special values of the floating types are double.NaN, double.PositiveInfinity and double.NegativeInfinity, which a division can produce and a comparison cannot see: NaN == NaN is false, and the test is double.IsNaN(x). A decimal has no such values and throws DivideByZeroException instead.
Exercises
- Print 0.1 + 0.2 == 0.3 and 0.1m + 0.2m == 0.3m. Which one is true?
- Compute 1.0 / 3.0 and print it with ToString("F10"). What does the last digit tell you?
- Divide 1.0 by 0.0 and print the result. Then divide 1m by 0m in a try and read the exception.
- Store a price in a double and add it a hundred times. Print the total with two decimals and compare it with the decimal total.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Why is 0.1 + 0.2 not 0.3 with double?
Answer
A double stores a binary fraction, and 0.1 has no exact binary form
The stored values are the nearest binary fractions, so their sum is the nearest binary fraction to 0.3, which is not 0.3.
-
Which type is exact for money?
Answer
decimal
decimal stores decimal digits, so a cent is exact; double and float store binary fractions and lose it.
-
What does this program print?
Console.WriteLine(0.1m + 0.2m); Console.WriteLine(0.1 + 0.2);Answer
0.3 / 0.30000000000000004
The first line adds two decimals exactly and the second two doubles, whose sum shows the binary error.
-
What does this program print?
Console.WriteLine(1m / 3m);Answer
0.3333333333333333333333333333
A decimal carries 28 digits, and the division fills them, which is more digits than a double prints.
-
How do you compare two doubles for equality in practice?
Answer
Compare the absolute difference with a small tolerance
Two computations of the same value may differ in the last bit, so a tolerance is the comparison that works.
-
What does this program print?
double sum = 0; for (int i = 0; i < 10; i++) { sum += 0.1; } Console.WriteLine(sum);Answer
0.9999999999999999
Each addition of the nearest double to 0.1 accumulates a small error, and ten of them land one bit below 1.
-
Which suffix makes a literal a decimal?
Answer
m
m is the decimal suffix, f is float, L is long, and a literal without a suffix and with a point is a double.
-
What does this program print?
Console.WriteLine(double.IsNaN(0.0 / 0.0)); Console.WriteLine(0.0 / 0.0 == 0.0 / 0.0);Answer
True / False
The quotient of two zero doubles is NaN, which is not equal to itself, so only IsNaN can find it.
-
Which type carries about 7 significant digits?
Answer
float
float is the 32-bit binary type with about 7 digits; double carries 15 to 17 and decimal 28 to 29.
-
What does this program print?
decimal total = 59.97m; Console.WriteLine(total.ToString("F1"));Answer
60.0
The format F1 rounds to one decimal place, and 59.97 rounds up to 60.0.
The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev