piclist 2017\09\06\130037a >
Thread: Division routines.
www.piclist.com/techref/microchip/math/index.htm?key=division
flavicon
face BY : smplx email (remove spam text)

part 1 994 bytes content-type:text/plain; charset="utf-8" (decoded base64)



On Wed, 6 Sep 2017, Walter Banks wrote:

> I depends how much you know about the numbers. This works as long as the
> MSbit of x is 0.
> No magic in this and not particularly fast.
>
> w..
>
> //
> // divide x/y and return a fraction
> // x < y always
> //
> // reference in 16 bits
> //
>
> uint16_t div_fract (uint16_t x,y)
>   {
>     char count = 16 ; // size of result in bits
>     do
>      {
>       x <<= 1;
>       if (x >= y)
>        {
>          x -= y;
>          x |= 1;
>        }
>      }
>     while(--count);
>    return x;
>   }

Hi Walter, I don't understand how the above is supposed to work.
Surely the "x |= 1" is wrong? Don't you need a seperate variable build the
result in?

On a mid range PIC the "(x >= y)" would need to be performed as
"(x - y >= 0)" so why not compute "x = x - y" and compare with 0?

Regards
Sergio Masci
part 2 197 bytes content-type:text/plain; name="ATT00001.txt"
(decoded base64)

--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
mailman.mit.edu/mailman/listinfo/piclist
<alpine.LNX.2.00.1709061856350.25052@tin.allotrope.net>

In reply to: <8accdb9a-51a1-ac1d-b1e0-77bd70cc4325@bytecraft.com>
See also: www.piclist.com/techref/microchip/math/index.htm?key=division
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) Division routines.

month overview.

new search...