|[13 earlier articles]|
|Re: Rounding with Div and Mod operators firstname.lastname@example.org (Peter Wilson) (1999-05-20)|
|Re: Rounding with Div and Mod operators email@example.com (1999-05-21)|
|Re: Rounding with Div and Mod operators firstname.lastname@example.org (John I. Moore, Jr.) (1999-05-22)|
|Re: Rounding with Div and Mod operators email@example.com (1999-05-22)|
|Re: Rounding with Div and Mod operators firstname.lastname@example.org (Barton) (1999-05-22)|
|Re: Rounding with Div and Mod operators email@example.com (Barton) (1999-05-27)|
|Re: Rounding with Div and Mod operators firstname.lastname@example.org (Jonathan Barker) (1999-05-27)|
|From:||"Jonathan Barker" <email@example.com>|
|Date:||27 May 1999 23:22:38 -0400|
|Organization:||Deja.com - Share what you know. Learn what you don't.|
> As a mathematician I have often found the behaviour of div and mod
> operators a little annoying (at least in C, Pascal etc). Number
> theorists universally use the following standard definition of integer
> If n and d are integers and d is non zero, the quotient q = (n div d)
> and remainder r = (n mod d) are defined by the equation n = qd + r
> with the constraint 0 <= r <= d-1. Thus the remainder is always a
> positive number between 0 and d-1 inclusive. Strictly speaking r
> identifies the equivalence class of n in the integers modulo d.
Of course, what I meant to say was 0 <= r <= |d|-1. Thanks to
William for prompting me to spot this error.
While I have the chance, I should apologise for a slightly premature
posting. I had not really considered the possibility of negative d
in detail. In the context I had in mind, number theory is not
concerned with negative values of d. However, I stand by my
original post, at least when d is positive. I would propose
1 n = qd + r
2 0 <= r <= d-1 when d>0
3 0 >= r >= -d+1 when d<0
1 and 2 make the mod operator behave in a manner which I consider to
be consistent with the theory. 3 'reflects' the arithemtic
through 0 when the sign of d reverses. This is an aesthetic
However, some may object to 2 on the grounds that the heuristics
(-n) mod d = n mod (-d) = -(n mod d)
are then not satisfied. I would stress through that there is no
reason to expect these to hold. [The integers do not form a field
under the usual arithmetic operations].
Again, apologies for the errors and badly thought out opinion.
Return to the
Search the comp.compilers archives again.