Integer division

rex ballard rb at ccivax.UUCP
Tue Feb 11 13:37:37 AEST 1986


As I recall, FORTH-83 has adopted the 'theorists' approach.
so that
A B SWAP OVER /MOD ROT * +
gives the original value of A
translated that gives
A B SWAP	( Stack = B A )
OVER		( Stack = B A B )
/MOD		( Stack = A%B A/B B ) (A/B is floored)
ROT		( Stack = A/B B A%B )
*		( Stack = (A/B)*B A%B )
+		( Stack = (A/B)*B+A%B ) (=A)

This was a deliberate break from FORTH-79 which would have required
the 'twiddle test'.  Appearently, most applications either were
using the unsigned versions, the positive case, or had rewritten the
primitive.

the example (taken from the draft standard of May 83)

	dividend	divisor		remainder	quotient
	10		 7		3		1
	-10		 7		4		-2
	10		 -7		-4		-2
	-10		 -7		-3		1

Is this any better?
I guess religion (because thats the way we always do it) isn't everything.



More information about the Comp.lang.c mailing list