Mod(negative, positive) gives wrong result in my opinion

Report problems with documented functionality
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Mod(negative, positive) gives wrong result in my opinion

26 Oct 2013, 00:05

http://en.wikipedia.org/wiki/Modular_arithmetic
as per the link above

MsgBox % mod(2,5) = MsgBox % mod(-3,5)

but they aren't

Simple explanation:
if we draw the number line the multiples of 5 are ......-15, -10, -5, 0, 5, 10, 15
so that Mod(-3,5) should return 2 because -5 + 2 = -3

So i wish MsgBox % mod(-3,5) resulted in 2 and not -3
edited and added:
Mod is something related to remainder and remainder can never be negative, i stumbled upon this while trying to learn by helping @dmg in his post on the other forum
John ... you working ?
gregster
Posts: 9114
Joined: 30 Sep 2013, 06:48

Re: Mod(negative, positive) gives wrong result in my opinion

26 Oct 2013, 00:55

remainder can never be negative
Not true.
But it can get a bit tricky, if dividend OR divisor are negative. There are different ways how programming languages define modulo operations and deal with it: " in C++ a negative number will be returned if the first argument is negative, and in Python a negative number will be returned if the second argument is negative" (http://en.wikipedia.org/wiki/Modular_ar ... Remainders)
See also http://en.wikipedia.org/wiki/Modulo_operation

In AHK Mod(-3,5) is -3 because AHK docs say "The sign of the result is always the same as the sign of the first parameter"
Therefore -3 : 5 = 0, rest -3, check: ( 5 * 0 ) -3 = 0-3 = -3
And Mod(3, -5) hence leads to 3: (-5) = 0, rest 3, -> -5*0 + 3 = 0+3 = 3

In another programming language, it could be like you expected (second parameter dtermines the sign of the result)
-3 : 5 = -1, rest 2 and (5 * (-1)) + 2 = -5 + 2 = -3

It's also true. Some programming languages have different functions for these two cases, some restrict themselves to one case. It depends on how you define modulo operations.
Last edited by gregster on 26 Oct 2013, 01:09, edited 1 time in total.
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: Mod(negative, positive) gives wrong result in my opinion

26 Oct 2013, 01:03

@gregstar
I just realised that whatever is given in maths does not mean the same would be implemented in all computer languages exactly.
learning stuff :) on the go :geek:
John ... you working ?
gregster
Posts: 9114
Joined: 30 Sep 2013, 06:48

Re: Mod(negative, positive) gives wrong result in my opinion

26 Oct 2013, 01:11

Very true. Especially, if you try to port a program to another language, it can get really ugly, if you are not aware of that....
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: Mod(negative, positive) gives wrong result in my opinion

26 Oct 2013, 01:18

All i know is a little bit like 2-3% of autohotkey

:)
John ... you working ?
User avatar
smorgasbord
Posts: 493
Joined: 30 Sep 2013, 09:34

Re: Mod(negative, positive) gives wrong result in my opinion

26 Oct 2013, 01:27

@gregstar
http://mathforum.org/library/drmath/view/52343.html

some heavy debate seems to be there on the same issue

may be they made a Rem() function :geek:
John ... you working ?
gregster
Posts: 9114
Joined: 30 Sep 2013, 06:48

Re: Mod(negative, positive) gives wrong result in my opinion

26 Oct 2013, 01:54

So Lotus 1-2-3 and Excel used different Modulos? I never noticed... For some time, I had to use them both parallely, back in the nineties. But probably I didn't use many modulo operations back then, anyway...
Thanks for the link to that background info (not especially about Lotus and Excel, but about how these differences originated) and this quote
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."

Very true!
GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Re: Mod(negative, positive) gives wrong result in my opinion

17 Dec 2022, 03:32

I might be quite a bit late to the party (just a bit off of a decade), but I was doing something related to Day of the Week calculation and this was the reason for my interest in this topic. I whipped up a function to resolve this issue but beware, this does call the native Mod() function.

Code: Select all

cMod(dividend, divisor)
{
    if(dividend > 0)
    {
        Return Mod(dividend, divisor)
    }
    else if(dividend < 0)
    {
        While(dividend < 0)
        {
            dividend += divisor
        }
        return Mod(dividend, divisor)
    }
    else
    {
        Return 0
    }
}

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 13 guests