Re: intdiv
- Posted by bill May 13, 2012
- 1531 views
Re GCD, a clean method is:
public function gcd(atom p, atom q) -- Both arguments must be positive. p = abs(p) q = abs(q) -- Strip off any fractional part. p = floor(p) q = floor(q) -- Ensure that 'p' is not smaller than 'q' if p < q then atom r = p p = q q = r end if -- Repeat until remainder 0 while q do atom r = remainder(p,q) p = q q = r end while return p end function
It is equivalent to your code barring typos.
GCD(0,x)=x is essential.
It's implicit in the function.
What you say about indiv being useful is correct
in so far as you are dealing with positive numbers.
My point is that applying indiv to negative numbers
is a category error.
The function you want for your tubing example is ceil(x/y).
You still need to ensure it is applied to positive x,y.
integer division:
-9 \ 4 = -2 9 \ 4 = 2Cheers