>Ian Bennett <.....PICLISTKILLspam.....MITVMA.MIT.EDU> wrote:
>
>> I am currently using a 16c84 for a project and I need to be able to
>> divide an integer number by a floating point number and end up with
>> a floating point result to two decimal places. I.e. 457 / 4.35 =
>> 105.06.
>>
>> The first integer number will never exceed 16384. The second
>> floating point number will always be in the range .01 to 255.
Andy Warren Wrote:
{Quote hidden}>Ian:
>
>I wouldn't bother with floating-point numbers here... If I were you,
>I'd store both numbers as integers, with the divisor multiplied by
>100 to move the decimal point over two places.
>
>To perform the division, I'd take the dividend and multiply it by
>100, then divide it by the already-multiplied-by-100 divisor
>using an integer 24-bit/16-bit divide routine.
>
>The quotient will be 100 times too large, of course... If you don't
>want to leave it that way, you can divide it by 100 to get the real
>answer.
Actually, an easier way to do it is to multiply by the fraction of 256/65356
and then take off the 256/65356.
ie, for the above example, 4.35 can be represented as:
256 / 4.35 = 59
= 0x03B
457 * ( 256 / 4.35 ) / 256
= 457 * 59 / 256
= 0x01C9 * 0x03B / 256 ; Putting the Numbers in Hex
= 0x06953 / 0x0100
Division by 256 is pretty easy (just cut off the last two hex digits),
giving you the result:
0x069
= 105
For better accuracy, divide 4.35 into a higher number. Note this algorithm
only really works when the divisor is known.
myke
"Some people say that foreign cars handle best, while others say domestic.
For my money, nothing handles as well as a rental car." - P.J. O'Rourke