If your exponent is decimal (i.e. it represents 10^X), you can precisely represent 0.1 -- however, most floating point formats use binary exponents (i.e. they represent 2^X). Since there are no integers X
and Y
such that Y * (2 ^ X) = 0.1
, you cannot precisely represent 0.1 in most floating point formats.
Some languages have types with both exponents. In C#, for example, there is a data type aptly named decimal
which is a floating point format with a decimal exponent so it will support storing a number like 0.1, although it has other uncommon properties: The decimal
type can distinguish between 0.1
and 0.10
, and it is always true that x + 1 != x
for all values of x
.
For most common purposes, though, C# also has the float
and double
floating point types that cannot precisely store 0.1 because they use a binary exponent (as defined in IEEE-754). The binary floating point types use less storage, are faster because they are easier to implement, and have more operations defined on them. In general decimal
is only used for financial values where the exact representation of all decimal values is important and the storage, speed, and range of operations are not.