Rounding with Div and Mod operators

"William Rayer" <william.rayer@virgin.net>
9 May 1999 18:47:49 -0400

          From comp.compilers

Related articles
Rounding with Div and Mod operators william.rayer@virgin.net (William Rayer) (1999-05-09)
Re: Rounding with Div and Mod operators wclodius@aol.com (1999-05-16)
Re: Rounding with Div and Mod operators ucapjab@ucl.ac.uk (Jonathan Barker) (1999-05-16)
Re: Rounding with Div and Mod operators nr@labrador.cs.virginia.edu (Norman Ramsey) (1999-05-16)
Re: Rounding with Div and Mod operators guerby@acm.org (Laurent Guerby) (1999-05-16)
Re: Rounding with Div and Mod operators anton@mips.complang.tuwien.ac.at (1999-05-16)
Re: Rounding with Div and Mod operators Scott.Daniels@Acm.Org (Scott.David.Daniels) (1999-05-16)
[13 later articles]
| List of all articles for this month |
From: "William Rayer" <william.rayer@virgin.net>
Newsgroups: comp.compilers
Date: 9 May 1999 18:47:49 -0400
Organization: Virgin News Service
Keywords: arithmetic, design

Dear Newsgroup


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.


As I understand it div and mod (please excuse the Pascal notation)
should always conform to a Fundamental Rule that for an integer
numerator (n) and integer divisor (d) with d <> 0:


    n = (n div d) * d + (n mod d)


What is interesting about this rule is there seem to be two ways of
rounding that satisfy it when n or d are negative - either we round
integers to the next lowest value or we round towards zero. The
following table shows some positive and negative values of n and d
under both systems:


-------------------------------------------------
                    (div rounds down (div rounds
                    to next lower int) towards zero)
  n d n div d n mod d n div d n mod d
-------------------------------------------------
  3 2 1 1 1 1
  2 2 1 0 1 0
  1 2 0 1 0 1
  0 2 0 0 0 0
-1 2 -1 1 0 -1
-2 2 -1 0 -1 0
-3 2 -2 1 -1 -1


  3 -2 -2 -1 -1 1
  2 -2 -1 0 -1 0
  1 -2 -1 -1 0 1
  0 -2 0 0 0 0
-1 -2 0 -1 0 -1
-2 -2 1 0 1 0
-3 -2 1 -1 1 -1
-------------------------------------------------


The middle two columns show a rounding system that goes to the next
lowest integer, and the rightmost two columns show a system that
rounds towards zero. The two systems are different when n and d have
different signs, as you can see by comparing the n div d columns. Both
systems meet the Fundamental Rule.


My question is: which rounding system is preferred and does it matter?
So far my thoughts and investigations have come up with the following:


(1) The middle columns looks neater as div goes in steps of two,
1,1,0,0,-1,-1 etc. Also mod looks neater as it does not suddenly
change sign as we pass through zero. Thus rounding to the lower
integer is best...


(2) Borland Pascal (Delphi v1) and Microsoft C (VC 1.5, 4) round
according to the righthand columns, ie towards zero. I think this is
because on the PC the default rounding of the Intel IDIV opcode is
towards zero. Thus for compatibility and performance on the PC
rounding towards zero is best...


(3) The language I'm developing also includes an "int" operator which
takes a real number operand and returns an integer which is the
operand rounded down to the next lowest integer (if the operand is an
exact integer value it is unaltered). Thus if I define div using next
lowest integer rounding, it is redundant as (n div d = int(n/d))
always. Thus rounding to the lower integer is redundant...


Any thoughts would be most welcome. So far the only relevant
information I have found on Usenet is a posting from 1994 which
discusses rounding in the context of Pascal and concluded the
Fundamental Rule only applied for n >= 0 and d > 0.


Sincerely
William Rayer
lingolanguage@hotmail.com
[This has been debated at length over the years, and I've found the
argument to be similar to the byte order argument in that there are
advantages and disadvantages to both, but none strong enough to tip
the scales firmly in favor of one or the other. For what it's worth,
Fortran 90 uses round toward zero. -John]



Post a followup to this message

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