Re: Rounding with Div and Mod operators

"Jonathan Barker" <ucapjab@ucl.ac.uk>
27 May 1999 23:22:38 -0400

          From comp.compilers

Related articles
[13 earlier articles]
Re: Rounding with Div and Mod operators peter.r.wilson@boeing.com (Peter Wilson) (1999-05-20)
Re: Rounding with Div and Mod operators mfinney@lynchburg.net (1999-05-21)
Re: Rounding with Div and Mod operators jmoore@softmoore.com (John I. Moore, Jr.) (1999-05-22)
Re: Rounding with Div and Mod operators lassehp@imv.au.dk (1999-05-22)
Re: Rounding with Div and Mod operators r_barton1@hotmail.com (Barton) (1999-05-22)
Re: Rounding with Div and Mod operators r_barton1@hotmail.com (Barton) (1999-05-27)
Re: Rounding with Div and Mod operators ucapjab@ucl.ac.uk (Jonathan Barker) (1999-05-27)
| List of all articles for this month |

From: "Jonathan Barker" <ucapjab@ucl.ac.uk>
Newsgroups: comp.compilers
Date: 27 May 1999 23:22:38 -0400
Organization: Deja.com - Share what you know. Learn what you don't.
References: 99-05-039 99-05-043
Keywords: arithmetic

Dear All


I wrote:
> 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
> division:


> 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
decision.


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.


Regards


Jonathan


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.