MOD function with negative numbers

Walter Murray wmurray at midtown.net
Thu Mar 2 01:04:39 CST 2006


There is no universally accepted definition of the mod function.  Each
language and application should define how mod is calculated when one or
both of the operands are negative.  If you look at the definitions in
the Pascal standard, the COBOL standard, the C standard, and the
BASIC/3000 language definition, I think you'll find four different
approaches to defining mod.  

Read the documentation!  Sometimes you have to take the documentation
with a grain of salt.  For example, the on-line help for the version of
Microsoft Excel that I am using reads, "The result has the same sign as
divisor."  (When they write "divisor", they mean the second argument.)
This is not always true.  Consider =mod(14,-7).  The divisor is
negative; the result is not.

Walter  

Walter J. Murray


-----Original Message-----
From: powerh-l-bounces+wmurray=midtown.net at lists.sowder.com
[mailto:powerh-l-bounces+wmurray=midtown.net at lists.sowder.com] On Behalf
Of roger32909 at bellsouth.net
Sent: Wednesday, March 01, 2006 1:36 PM
To: powerh-l at lists.sowder.com
Subject: MOD function with negative numbers

If I use the mod function in Excel, say =mod(-1,7), the result is 6.

If I google "-1 modulo 7", the result is 6.

But if I use the PH mod function for MOD(-1,7) the result is -1

$ QUIZ

Q U I Z   (7.10.G1)
Copyright 1998 COGNOS INC. (ALPHA)
Licensed PH-AXP-RUNTIME 

> DEFINE D-1 = MOD(1,7)
> DEFINE D-2 = MOD(2,7)
> DEFINE D-3 = MOD(3,7)
> DEFINE D-4 = MOD(4,7)
> DEFINE D-5 = MOD(5,7)
> DEFINE D-6 = MOD(6,7)
> DEFINE D-7 = MOD(7,7)
> DEFINE D-1N = MOD(-1,7)
>  DEFINE D-2N = MOD(-2,7)
>  DEFINE D-3N = MOD(-3,7)
>  DEFINE D-4N = MOD(-4,7)
>  DEFINE D-5N = MOD(-5,7)
>  DEFINE D-6N = MOD(-6,7)
>  DEFINE D-7N = MOD(-7,7)
> SAVE TEST-MOD
> REPO ALL
> GO
03/01/06                                       System            PAGE
1

    D-1      D-2      D-3      D-4      D-5      D-6      D-7      D-1N

    D-2N     D-3N     D-4N     D-5N     D-6N     D-7N

        1        2        3        4        5        6        0       -1
       -2       -3       -4       -5       -6        0

Are there two different modular arithmetic systems for negative numbers?
Or am I doing something wrong?

-- 
= = = = = = = = = = = = = = = = = = = = = = = = = = = =
Mailing list: powerh-l at lists.sowder.com
Subscribe: "subscribe" in message body to
powerh-l-request at lists.sowder.com
Unsubscribe: "unsubscribe <password>" in message body to
powerh-l-request at lists.sowder.com
http://lists.sowder.com/mailman/listinfo/powerh-l
This list is closed, thus to post to the list you must be a subscriber.



More information about the powerh-l mailing list