[Oberon] Modulus on negative number

Chris Burrows chris at cfbsoftware.com
Wed Feb 15 08:51:43 CET 2017


> -----Original Message-----
> From: Oberon [mailto:oberon-bounces at lists.inf.ethz.ch] On Behalf Of
> Srinivas Nayak
> Sent: Wednesday, 15 February 2017 1:51 PM
> To: ETH Oberon and related systems
> Subject: [Oberon] Modulus on negative number
> 
> Dear All,
> 
> Recently I come across modulus on a negative number.
> Will it produce a negative number or positive?
> Someone says both are correct! http://stackoverflow.com/a/4403556
> Which one is mathematically correct?
> Surprisingly different languages calculate it differently even!
> What is Oberon's way?
> 

"Oberon's way" is illustrated in Section 7.1 The Type INTEGER
In "Programming in Oberon (a Tutorial)", 2014. by N Wirth:

=================================================================
  DIV division
  MOD modulus (remainder)

Integer division is denoted by DIV. If we define the integer 
quotient and the modulus of x and y by

  q = x DIV y, r = x MOD y

then q and r are related by the equation x = q*y + r and by 
the constraint 0 <= r < y. For example
  
  15 DIV 4 = 3 
  15 MOD 4 = 3 
  
     15 = 3*4 + 3

  (-15) DIV 4 = -4 
  (-15) MOD 4 = 1 

    -15 = (-4)*4 + 1

=================================================================

In case the above excerpt  gets scrambled in transit you can 
download the whole document from:

https://www.inf.ethz.ch/personal/wirth/Oberon/index.html

Regards,
Chris Burrows
CFB Software
http://www.astrobe.com





More information about the Oberon mailing list