MOD function with negative numbers

roger32909 at bellsouth.net roger32909 at bellsouth.net
Fri Mar 3 08:42:41 CST 2006


Thank you, Joe, Peter & Walter.

Further searching on the internet shows that there are indeed at least two different interpretations.  The people or company writing the language "define it for their own use".

For example:

"There are basically two models, reasonably distinguished in Ada terms as Remainder and Mod; the C++ "%" operator is really Remainder, not Mod, despite what it's often called.  Actually, its behavior for negative numbers is not even defined officially; like many things in C, it's left to be processor-dependent because C does not define how a processor should handle integer division. Just by chance, all compilers I know truncate integers toward zero, and therefore treat "%" as remainder, following the precedent of FORTRAN. As _C: A Reference Manual_, by Harbison and Steele, says,  "For maximum portability, programs should therefore avoid depending on the behavior of the remainder operator when applied to negative integral operands."

- Doctor Peterson, The Math Forum
  http://mathforum.org/dr.math/   

"I think the provision of two formulations as in Ada is reasonable, and would not try to prove that one is invalid. The fact is that there is no definition of "integer division" in math; rather, we divide and apply the greatest integer function or some variation of it to the resulting rational number, depending 
on our needs. Integer division is a formulation of computer languages, and they define it for their own use."

- Doctor Peterson, The Math Forum
  http://mathforum.org/dr.math/   

In the documentation, an example using a negative number would make the function's behavior more clear.

Anyhow, the workaround for people who may have a similar problem of translating a formula between the PowerHouse model and the Excel model is along these lines which does not change the result for positive numbers, but does provide same result as Excel for negative numbers:

$ quiz

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

> DEFINE D1 = MOD(1+7,7)
> DEFINE D2 = MOD(2+7,7)
> DEFINE D3 = MOD(3+7,7)
> DEFINE D4 = MOD(4+7,7)
> DEFINE D5 = MOD(5+7,7)
> DEFINE D6 = MOD(6+7,7)
> DEFINE D7 = MOD(7+7,7)
> DEFINE D1N = MOD(-1+7,7)
> DEFINE D2N = MOD(-2+7,7)
> DEFINE D3N = MOD(-3+7,7)
> DEFINE D4N = MOD(-4+7,7)
> DEFINE D5N = MOD(-5+7,7)
> DEFINE D6N = MOD(-6+7,7)
> DEFINE D7N = MOD(-7+7,7)
> REPO ALL
> 
> go
03/03/06             				 System            PAGE   1

    D1       D2        D3        D4        D5        D6        D7
    D1N     D2N     D3N     D4N     D5N     D6N     D7N

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




More information about the powerh-l mailing list