Re: Rounding with Div and Mod operators

mfinney@lynchburg.net (Michael Lee Finney)
21 May 1999 02:25:50 -0400

          From comp.compilers

Related articles
[8 earlier articles]
Re: Rounding with Div and Mod operators johan.persson@mbox319.swipnet.se (Johan Persson) (1999-05-16)
Re: Rounding with Div and Mod operators genew@shuswap.net (1999-05-20)
Re: Rounding with Div and Mod operators sofkam@rpi.edu (1999-05-20)
Re: Rounding with Div and Mod operators drh@microsoft.com (Dave Hanson) (1999-05-20)
Re: Rounding with Div and Mod operators anton@mips.complang.tuwien.ac.at (1999-05-20)
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: mfinney@lynchburg.net (Michael Lee Finney)
Newsgroups: comp.compilers
Date: 21 May 1999 02:25:50 -0400
Organization: Lynchburg.net (lynchburg.net)
References: 99-05-039 99-05-042
Keywords: arithmetic

wclodius@aol.com says...
> I'm writing a compiler for a new language which is to include the
> integer division and integer remainder operators (div and mod). I have
> some questions about the way these operators do rounding with negative
> operands and would appreciate any feedback.


I notice that several people have mentioned the "two" forms of
div/mod. I few years back I compared this on quite a few languages
and found the *three* definitions in actual use (and, no, it was so
long ago that I don't remember which definition was used by which
language). The three definitions are (using |x| to mean absolute
value of x and sgn(x) to mean the sign of x, -1, 0, +1)...


1. sgn(x % y) = sgn(x)
0 <= |x % y| <= |y|


2. sgn(x % y) = sgn(y)
0 <= |x % y| <= |y|


3. 0 <= x % y
0 <= x % y < |y|


I believe that there are quite a few more ways in which it could be
defined from a theoretical point of view, but these are the only ones
I have seen in practice.
--
Michael Lee Finney
michael.finney@acm.org


Post a followup to this message

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