Re: Rounding with Div and Mod operators

"John I. Moore, Jr." <jmoore@softmoore.com>
22 May 1999 02:56:24 -0400

          From comp.compilers

Related articles
[9 earlier articles]
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: "John I. Moore, Jr." <jmoore@softmoore.com>
Newsgroups: comp.compilers
Date: 22 May 1999 02:56:24 -0400
Organization: SoftMoore Consulting
References: 99-05-039
Keywords: arithmetic



William Rayer wrote
>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:




I remember looking t this issue in the mid 1980's when I was
learning Ada. My opinion is that Ada was one of the first such
languages to make this precise. I tried three different versions of
Pascal on different computers and got three different answers.
For your information, I have included a copy of a handout that
I prepared for Ada.


___________________________________________________________


John I. Moore, Jr.
SoftMoore Consulting
16233 Monty Court
Rockville, MD 20853-1344


email: jmoore@softmoore.com
url: www.softmoore.com
phone: (301) 924-0680


_________________________________________________


                                      Integer Arithmetic in Ada




1. division - divide and truncate toward zero (throw away any
          fractional part)


2. rem - remainder associated with integer division
                - sign of A rem B is same as sign of A (dividend)
                - Definition: A rem B = A - B*(A/B)
                    where "/" means integer division in Ada


3. mod - modulus associated with integer division
                - sign of A mod B is same as sign of B (divisor)
                - Definition: A mod B = A - B*[A/B]
                    where "[A/B]" means divide out completely (not integer
                    division in Ada) and truncate toward negative infinity
                    (greatest integer less than or equal to A/B)


4. Examples:


        13/5 = 2 13 rem 5 = 3 13 mod 5 = 3


        13/(-5) = -2 13 rem (-5) = 3 13 mod (-5) = -2


        (-13)/5 = -2 (-13) rem 5 = -3 (-13) mod 5 = 2


        (-13)/(-5) = 2 (-13) rem (-5) = -3 (-13) mod (-5) = -3


5. Both rem and mod give the same result whenever the operands
          have the same sign. See LRM section 4.5.5 for additional
          information and examples. When in doubt, you probably want
          rem.


6. Explicit type conversion from real to integer always rounds:


        INTEGER(2.6) = 3 INTEGER(-2.6) = -3


        INTEGER(PI) = 3 INTEGER(-PI) = -3


Post a followup to this message

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