Nigel Goodwin said:
It probably took you so long because it's NOT! - shifted integer routines work on integer numbers, floating point routines don't. They convert the numbers to scientific notation (mantissa and exponent), where the exponents have ONE digit before the decimal point, and all other digits are after the decimal point. So the actual routine works with non-integer numbers, complete with their well known inaccuracies.
Floating point routines do work on integers.
FP numbers don't have to have any digits before the decimal point.
Maybe you can explain what the mantissa is if it is not an integer?
Let's try an example using 16E8 notation - 16 bit mantissa and 8 bit exponent.
Lets do 12345 * PI
PI = 11001001 00010000 E2
12345 = 11000000 11100100 E14
Note that the mantissa is simply the original number converted to an integer and the exponent is a count of how far from the right the decimal place should be. If you don't believe me then try converting the binary representation of PI to decimal and divide by 16384 (2^16/2^2).
Multiply the two mantissas together to get 10010111 01111111
Add the exponents to get 16
As the decimal point starts before the answer and needs to be shifted 16 places right then the answer is simply the mantissa converted to decimal - 38783. (excel gives the answer as 38782.96131)
This can even be done with 8 bit numbers. Obviously with greater error.
PI = 11001001 E2
12345 = 11000000 E14
Answer = 10010111 E16
Decimal = 38656
Or with bigger numbers.
PI = 11001001 00010000 E2
123450 = 11110001 00011101 E17
Multiply the two mantissas together to get 10111101 01011110
Add the exponents to get 19
Shift it the 19 places required = 101 11101010 11110000
Convert to decimal to get 387824.
Note, the inaccuracies are caused by converting the original value into an integer. Also, there is not a more accurate way to do this with 16*16 multiply.
Mike.
Edit, clarification.