I’m having some trouble understanding why some figures can’t be represented with floating point number.
As we know, a normal float would have sign bit, exponent, and mantissa. Why can’t, for example, 0.1 be represented accurately in this system; the way I think of it would be that you put 10 (1010 in bin) to mantissa and -2 to the exponent. As far as I know, both numbers can be accurately represented in the mantissa and exponent. So why can’t we represent 0.1 accurately?
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
XandYsuch thatY * (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
decimalwhich 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: Thedecimaltype can distinguish between0.1and0.10, and it is always true thatx + 1 != xfor all values ofx.For most common purposes, though, C# also has the
floatanddoublefloating 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 generaldecimalis 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.