www.piclist.com/techref/microchip/math/index.htm?key=division

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

--

http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive

View/change your membership options at

mailman.mit.edu/mailman/listinfo/piclist

In reply to: <8accdb9a-51a1-ac1d-b1e0-77bd70cc4325@bytecraft.com>

See also: www.piclist.com/techref/microchip/math/index.htm?key=division