Computer Science Canada

mod function

Author:  mercuryy [ Mon Jun 20, 2005 4:03 pm ]
Post subject:  mod function

I am a bit confused about how to predict the output using the mod function for negative number.
for example. How do you calculate

-2 mod 5
-5 mod 2
2 mod -5

etc. in turing without actually running the program?[/b]

Author:  Delos [ Mon Jun 20, 2005 6:20 pm ]
Post subject: 

It is no different from looking at a positive number. Since 'mod' returns the remainder of a division, say you test 5 mod 2. You'll get 1. -5 mod 2? You'll still get 1. Don't think about it too hard - but suffice it to say that "remainder of negative one" and "remainder of positive one" still both mean that there 1 remaining.

Author:  mercuryy [ Mon Jun 20, 2005 6:36 pm ]
Post subject: 

what about -2 mod 5 and 2 mod 5. -2 mod 5 is obviously 2, so why does -2 mod 5 equals to 3?

Author:  MysticVegeta [ Mon Jun 20, 2005 6:49 pm ]
Post subject: 

Let me simplify it down for you, mod really is ->
code:
var num : int := 5
var num2 : int := -2
put num - ((num div num2)*num2)


: