DIV, MOD, /, REM (M2,O2) 
Author Message
 DIV, MOD, /, REM (M2,O2)

Hi,

concerning DIV, MOD, /  and REM in ISO Modula-2 and in Oberon-2:

I'm quoting from section C.5 in

APPENDIX C: RESTRICTIONS, EXTENSIONS and CLARIFICATIONS

of ModulaWare's H2O V1.21 VAX/VMS Oberon-2 User's Guide, Feb-1993

written by G~unter Dotzel

  --->
  C.5 Integer Operators DIV and MOD

  Clarification and discussion: Both DIV and MOD for integers are
  implemented according to the semantics defined in the Oberon-2
  Report. Constant expressions are also evaluated according to this
  semantics.

  The native VAX instruction DIV and EDIV have different semantics,
  i.e. the semantics of ISO Modula-2's "/" and "REM" with integer
  operands.  This is a problem for negative dividends only. For
  example

  -31 VAX-DIV 10 = -3 (= -4 according to the Oberon-2 Report)

  -31 VAX-MOD 10 = -1 (= 9 according to the Oberon-2 Report)

  Integer division and modulus with negative dividends are rarely
  needed. So, for efficiency reason, the DIV and MOD operators are a
  likely canditates for an implementation restriction on processors
  without semantically conforming DIV and MOD instructions. But the
  goal with the H2O implementation was to be as close as possible
  semantically compatible with ETH-implementations of Oberon-2. Code
  is generated to check and correct the result for negative dividends
  at run-time for the DIV and MOD operators.

  One possible solution to resolve the efficiency problem of the
  additional run-time check would be to take the ISO Modula-2
  solution. In a future release of H2O, additional operator symbols
  "/" and "REM" could be used to select the semantics of the VAX
  instructions;  "DIV" and "MOD" would still select the Oberon-2
  Report semantics, implemented either as run-time system calls or
  (as currently) in-line as sequence of instructions generated by the
  compiler.

  But the introduction of new operators is a language extension which
  must be first be discussed with the Oberon-2 language designers at
  ETH-Z~urich. The problem with the integration of ISO Modula-2's
  solution in Oberon-2 is that Oberon-2 uses the "/" to indicate real
  number divison. So if i and j are both integers, i and j would
  automatically be converted to real in the expressions  (i / j). So
  either the implicit mechanism of "/" has to be disabled for both
  operands being integer (note, there is no such feature for the
  other overloaded arithmetic operators "+", "-", and "*" which apply
  to integers and reals in Oberon-2) or another operator symbol has
  to be introduced, which serves the same purpose as ISO Modula-2's
  "/" for integers.

  The semantics of Oberon-2's DIV and MOD  is specified by the
  function procedures ODIV and OMOD written in Oberon-2 and listed
  below. The implementation of ODIV and OMOD checks for negative
  dividends and uses DIV and MOD only with positive dividends and
  hence doesn't depend on the semantical differences described above,
  but doesn't check for overflow:

  (**
    y > 0
    x  = ODIV(x,y)*y + OMOD(x,y)
    0 <= OMOD(x,y) < y
  **)

  PROCEDURE OMOD(dividend, divisor: LONGINT): LONGINT;
  VAR rem: LONGINT;
  BEGIN
    IF dividend < 0 THEN
      rem:=(-dividend) MOD divisor;
      IF rem # 0 THEN rem := divisor-rem;
      END;
      RETURN rem;
    ELSE
      RETURN dividend MOD divisor
    END;
  END OMOD;

  PROCEDURE ODIV(dividend, divisor: LONGINT): LONGINT;
  VAR rem,quo: LONGINT;
  BEGIN
    IF dividend < 0 THEN
      rem:=(-dividend) MOD divisor;
      quo:=(-dividend) DIV divisor;
      IF rem # 0 THEN INC(quo);
      END;
      RETURN -quo;
    ELSE
      RETURN dividend DIV divisor
    END;
  END ODIV;

<---

Guenter Dotzel
ModulaWare GmbH, Wilhelmstr. 17A, D-W 8520 Erlangen/F.R.Germany
Tel. +49 (9131) 208395, Fax +49 (9131) 28205.




Mon, 07 Aug 1995 15:37:40 GMT  
 DIV, MOD, /, REM (M2,O2)

Quote:
Guenter Dotzel writes:
>   The semantics of Oberon-2's DIV and MOD  is specified by the
>   function procedures ODIV and OMOD written in Oberon-2 and listed
>   below. The implementation of ODIV and OMOD checks for negative
>   dividends and uses DIV and MOD only with positive dividends and
>   hence doesn't depend on the semantical differences described above,
>   but doesn't check for overflow:

>   (**
>     y > 0
>     x  = ODIV(x,y)*y + OMOD(x,y)
>     0 <= OMOD(x,y) < y
>   **)

There are many natural applications that require DIV and MOD to be
implemented correctly, i.e. according to the definition extant in
mathematics for well over 100 years.  The above quoted definition,
where (x Mod y) is always non-negative, is that correct definition.  
I can pull my number theory books and give an exact citation if
required.  Off the top of my head, Neal McCoy is one such reference.

The two natural applications available to physical scientists and
engineers are the strip-chart recorder with wraparound and the
clock.  Not suprisingly, the introduction to the equivalence groups
in mathematics sometimes calls them the clock numbers.

In a clock simulation or a wrap-around strip chart with 12 levels,
it is natural to desire 12+1 becomes 1.  This is equivalent to 0+1
becomes 1.  We would also have 12+1-2 (hours) becomes 11 hours.  
There is not legal answer of -1 hours, either in the clock or the
stripchart.

Professor Wirth offers us a correct definition and example in the
distribution file Oberon2.Report.Text (Sparc.2.6).  I quote without
permission:

Quote:
> 8.2.2 Arithmetic operators

> +  sum
> -  difference
> *  product
> /  real quotient
> DIV        integer quotient
> MOD        modulus

> The operators +, -, *, and / apply to operands of numeric types. The
> type of the result is the type of that operand which includes the
> type of the other operand, except for division (/), where the result
> is the smallest real type which includes both operand types. When
> used as monadic operators, - denotes sign inversion and + denotes
> the identity operation. The operators DIV and MOD apply to integer
> operands only. They are related by the following formulas defined
> for any x and positive divisors y:

> x = (x DIV y) * y + (x MOD y)
> 0 <= (x MOD y) < y

> Examples:
> x  y       x DIV y x MOD y
> 5  3       1       2
> -5 3       -2      1

Almost all hardware designers make this symmetrical around zero,
that is, they implement (-x) MOD y as -(x MOD y).  This is contrary
to well established definitions, i.e. wrong.  Please tell your EE
undergraduates this.  I cannot believe that it is advantageous to
make a program wrong to make it faster.  We don't re-define
multiplication when we work with a 6502.  Although in principle
I cannot see any problem with extensions that allow a correct program
to function correctly, as yours would, I also cannot see any reason to
add changes specifically to allow an incorrect program to be written.

To change Wirth's Einstein quote: "Make it as fast as possible, but not
faster."  (Relatively speaking... :-)

If there is any (natural) application to use a negative definition of
Mod, I'd be more inclined to stay silent about this. If anyone knows
of one, I'd appreciate knowing of it.

--
Aubrey McIntosh  /  Chemistry  /  University of Texas  /  Austin, TX 78712
..another Gaelic learner...



Tue, 08 Aug 1995 09:11:27 GMT  
 DIV, MOD, /, REM (M2,O2)
From the implementor's viewpoint, the Oberon(-2) definitions also allow
consistent implementation of multiplication and division by powers of
two with shifting and masking. DIV 4 always means arithmetic-shift-right by
two, and works for 2's complement negative integers, while MOD 4 always
means AND with -3.

Michael Franz



Wed, 09 Aug 1995 01:01:24 GMT  
 DIV, MOD, /, REM (M2,O2)

Quote:

> while MOD 4 always
>means AND with -3.

  Don't you mean AND with +3?


Wed, 09 Aug 1995 12:52:43 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Query: numeric_std: div,rem,mod

2. SM/REM FM/MOD and UM/MOD on a 32bit cell ix86 Forth

3. MOD, REM, et al

4. function REM or MOD

5. mod vs. rem

6. Need Help with Division/MOD/REM

7. Difference between REM and MOD

8. DIV and MOD

9. MOD/DIV in standard Pascal

10. DIV and MOD

11. 3/486 M2/O2 Demo

12. 3/486 M2/O2 Demo

 

 
Powered by phpBB® Forum Software