An 8-digit base 10 representation of the number 1234.567 = 1.234567 * 103 would be 31234567. The 8-digit base 10 representation can be broken up into two parts. The first digit is the exponent, in this case 3. The following 7 digits are the significant digits, or mantissa. Notice that this representation assumes that the decimal place for the mantissa is after the first digit.
The advantage to representing numbers using floating points is that it is possible to represent a much larger range of numbers. Fixed point notation when limited to a certain number of digits must always assume that the decimal is in the same place. For example if an 8-digit base 10 fixed point representation assumes that there are four digits to the left and four digits to the right of the decimal point then the largest number that could be represented is 99999999 = 9999.9999. With the floating point representation used above the largest number that could be represented is 99999999 = 9.999999 * 109 = 9,999,999,000.
A problem with fixed point representation is that fewer numbers between the maximum representable number can be represented. For example 9,999,999,000-1 = 9,999,998,999 = 9.999998999 * 109. Except that the mantissa in this case has more than 7 digits, so it would have to be rounded to 9.999999 * 109 in order to be stored in an 8-digit base 10 floating point number. This leads to the peculiar behavior where 9,999,999,000 - 1 = 9,999,999,000.
Floating point arithmetic is similar to scientific notation arithmetic. For example in order to add 1.234567 * 103 and 7654321*102 the operand with the lower base is converted to the same base as the other operand and the mantissas are added. So, 1.234567 * 103 + 7.654321*102 is equivalent to 1.234567 * 103 + 0.7654321*103, which is:
1.2345670 * 103 +0.7654321 * 103 --------------- 1.9999991 * 103Notice that the mantissa of the answer is 8 digits, which is larger than our 8-digit representation allows, so the actual sum, 1999.9991, would be rounded to 1999.999. In this case the answer is off by just one-ten-thousandth. But there are other cases when strange behavior can occur. For example, when two numbers of much different size are added:
1.234567 * 103 => 0.0000001234567 * 109 +7.654321 * 109 => +7.6543210000000 * 109 -------------- ------------------ 7.6543211234567 * 107When 7.6543211234567 * 109 is rounded to account for the 7 digit mantissa, it becomes 7.654321 * 109, which is one of the operands.
Floating point multiplication is also very similar to scientific notation multiplication, mantissa are multiplied while exponents are added. For example:
1.234567 * 103 *7.654321 * 103 -------------- 9.449772114007 * 106Which, when rounded to account for the 7 digit mantissa is 9.449772 * 106.
Rational numbers are represented in binary in the same way as decimal numbers. For example, the 8-digit fixed point decimal number 0003.7500 is equivalent to 3*100+7*10-1+5*10-2 = 3.0+0.7+0.05. This same number in 8-bit fixed point binary would be 0011.1100, which is equivalent to 21+20+2-1+2-2 = 3.75. This same number in 8-bit binary floating point notation would be 00111110. The first three digits are the exponent and the last five are the mantissa. So, in binary scientific notation it is equivalent to 1.111 * 21 = 1.875*2 = 3.75.
Final note, because rational numbers in binary are a sum of 2's raised to some power, only numbers with a denominator that is a sum of powers of two can be represented. For example 3.75 = 21+20+2-1+2-2, which is a sum of 2's raised to some power. 0.1 = 2-4+2-5+2-8+2-9..., which is an infinite sum that can not be represented with a finite number of bits, so it would be rounded. This is why often when you are writing code if you enter a literal double value it will print a very long sequence of digits that are close to the number you entered, but not the same.
Many computers use IEEE Floating Point format to represent floating point numbers. Numbers are converted to binary scientific notation with the following number of bits used to represent the exponent and the mantissa.