Searching \ for 'Math Divide Routines' in subject line. ()
Make payments with PayPal - it's fast, free and secure! 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.
PICList Thread
'Math Divide Routines'
1997\03\12@055246 by Ian Bennett

flavicon
face
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: spam_OUTianTakeThisOuTspaminside.co.uk - Tel: +44 1703 55 65 64 - Fax: +44 1703 32 22 18
-------------------------------------------------------------------------------

1997\03\12@062815 by Andrew Warren

face
flavicon
face
Ian Bennett <.....PICLISTKILLspamspam@spam@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.
>
> 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
answer.

-Andy

=== Andrew Warren - fastfwdspamKILLspamix.netcom.com
=== Fast Forward Engineering - Vista, California
===
=== Custodian of the PICLIST Fund -- For more info, see:
=== www.geocities.com/SiliconValley/2499/fund.html

1997\03\12@092557 by myke predko

flavicon
face
>Ian Bennett <.....PICLISTKILLspamspam.....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}

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

1997\03\12@152221 by Andrew Warren

face
flavicon
face
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
> the real answer.

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

{Quote hidden}

   Myke:

   Good advice, but your method gives an integer result and a
   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 - fastfwdspamspam_OUTix.netcom.com
=== Fast Forward Engineering - Vista, California
===
=== Custodian of the PICLIST Fund -- For more info, see:
=== www.geocities.com/SiliconValley/2499/fund.html

More... (looser matching)
- Last day of these posts
- In 1997 , 1998 only
- Today
- New search...