Searching \ for 'Math Divide Routines' in subject line. ()
Help us get a faster server
FAQ page: www.piclist.com/techref/method/math.htm?key=math
Search entire site for: 'Math Divide Routines'.

Truncated match.
'Math Divide Routines'
1997\03\12@055246 by

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.

I have looked at the Math routines on the MicroChip docs CD but I can't seem
to get them to work properly. The documentation does not make it clear (to
me anyway) how I need to pass the numbers to the routines.

Does anyone know of any better documented routines or can point me in the
right direction.

Ian Bennett

-------------------------------------------------------------------------------
Inside Information Limited - http:\\http://www.inside.co.uk
Email: ianinside.co.uk - Tel: +44 1703 55 65 64 - Fax: +44 1703 32 22 18
-------------------------------------------------------------------------------

Ian Bennett <PICLISTMITVMA.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.
>
> I have looked at the Math routines on the MicroChip docs CD but I
> can't seem to get them to work properly. The documentation does not
> make it clear (to me anyway) how I need to pass the numbers to the
> routines.

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

-Andy

=== Andrew Warren - fastfwdix.netcom.com
=== Fast Forward Engineering - Vista, California
===
=== www.geocities.com/SiliconValley/2499/fund.html

>Ian Bennett <PICLISTMITVMA.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}

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

I wrote:

> 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

and myke predko <PICLISTMITVMA.MIT.EDU> replied:

{Quote hidden}

Myke:

remainder modulo 256... Ian needed an integer result and a
remainder modulo 100.

If you look at what's required to convert your remainder to the
form he wanted, I think you'll discover that the method I
proposed is a little faster.

> Note this algorithm only really works when the divisor is known.

Which is another reason for the method I suggested... Ian's
original message implied that both his dividend and his divisor
were variable.

-Andy

=== Andrew Warren - fastfwdix.netcom.com
=== Fast Forward Engineering - Vista, California
===