# Mod(-3, 12) returns -1 and not 9

## Recommended Posts

Hi devs,

`MsgBox(0x40, '-3 % 12 =', Mod(-3, 12))`

The above code outputs -1. When I Mod() a number by 12, I expect the output to be between 0 and 11, which is what happens with the Perl modulus % operator. I therefore expect 9 to be the response here.

I would not expect Mod(-3, 12) to return -1 under any circumstances however.

##### Share on other sites

Um very weird, it outputted -3 for me.....

Very weird, Looks like this is a good issue, Thanks for bringing it up

AutoIt Smith

P.S I have learned a new function yay!!

Edit 1 : WAIT a second, maybe you don't get this function. Is divides the first parameter by the second and returns the remainder. So, if you reverse the digits, 12 / -3 = -4 with no remainder to the return is 0. I do not understand why it returned -1 or 3 for you or me though. It should return -0.25 according to how the function works. Just a thought.

Edit 2 : It returned -3 for me because the divisor was bigger then the dividend which was smaller, so it returned the dividend which was -3. It most likely returned -1 for you because you do not have the current updated beta or the command Failed. Look under the help file parameters and returns, it explains it.

Edited by AutoIt Smith

##### Share on other sites

Grumble grumble grumble... Now that I try it again, it's returning -3. SciTE automatically saves the script between runs so now I'm not so sure what happened.

I would expect either 9 or -3 to be returned, so perhaps I'm wasting everyone's time...

It makes sense to return -3 after all, since -3 / 12 = 0 rem. -3. I wonder why Perl's modulus operator operates in a different fashion (-3 / 12 = -1 rem. 9)...?

Edit 1 : WAIT a second, maybe you don't get this function. Is divides the first parameter by the second and returns the remainder. So, if you reverse the digits, 12 / -3 = -4 with no remainder to the return is 0. I do not understand why it returned -1 or 3 for you or me though. It should return -0.25 according to how the function works. Just a thought.

Hmm... I don't believe that Mod() would ever return a floating-point value like that if you pass it two integers...

##### Share on other sites

It makes sense to return -3 after all, since -3 / 12 = 0 rem. -3. I wonder why Perl's modulus operator operates in a different fashion (-3 / 12 = -1 rem. 9)...?

Mathematically speaking, the AutoIt MOD function is working correctly and Perl is not. I looks like Perl is returning the difference of the absolute values of the two numerals.

- Kevin

##### Share on other sites

Would you say Google is wrong?

That would be the first time Google is wrong

And Calculator.com says 9 too (check the scientific calculator).

##### Share on other sites

Would you say Google is wrong?

That would be the first time Google is wrong

And Calculator.com says -9 too (check the scientific calculator).

Wow, I never knew google was a calculator too... Thanks

FootbaG

##### Share on other sites

it was 9 by the way. I guess I was sleeping while typing...

##### Share on other sites

This has been covered before, I believe. The modulus operator in C and thus in C++ is wrong from a mathematical standpoint if I remember right. It was pointed out once before that mathematical modulus produces a different result than AutoIt's Mod() function. It appears that virtually every programming language gets it "wrong" which means most use the C way of doing things.

You may wish to search the forum (Try Bug Reports and its sub-forums first) for more on this. My memory on the subject is perhaps a bit fuzzy since I didn't much care (and still don't). I'm sure there is also information on the internet about this, too. I would try searching the Usenet groups comp.lang.c, comp.lang.c++ or comp.lang.std.c++ as I'm sure the topic has been discussed on their before, too.

##### Share on other sites

This has been covered before, I believe. The modulus operator in C and thus in C++ is wrong from a mathematical standpoint if I remember right. It was pointed out once before that mathematical modulus produces a different result than AutoIt's Mod() function. It appears that virtually every programming language gets it "wrong" which means most use the C way of doing things.

You may wish to search the forum (Try Bug Reports and its sub-forums first) for more on this. My memory on the subject is perhaps a bit fuzzy since I didn't much care (and still don't). I'm sure there is also information on the internet about this, too. I would try searching the Usenet groups comp.lang.c, comp.lang.c++ or comp.lang.std.c++ as I'm sure the topic has been discussed on their before, too.

So then, shouldn't AutoIt do it right? After all, AutoIt has never tried to be similar to C before, right? :-)

I don't think that many people would rely on this "wrong" behaviour for their scripts so hopefully it would not break many old scripts....

Angel

##### Share on other sites

So then, shouldn't AutoIt do it right?

If AutoIt does it "right" from a mathematicl standpoint, it will be wrong when compared to virtually every other programming language in existence.

After all, AutoIt has never tried to be similar to C before, right? :-)

AutoIt is implemented in C++ which means we inherit things like this unless we want to re-invent the built-in C++ operators.

I don't think that many people would rely on this "wrong" behaviour for their scripts so hopefully it would not break many old scripts....

The entire world is relying on this "wrong" behavior. The electric you are using is provided to you courtesy of a broken modulo operator. The operating system you are using currently is relying on it. If your automobile has an on-board electronics system, chances are, it's relying on the wrong behavior. Get the point; do you really want to suggest we make AutoIt "right" so we can be different than the rest of the world?

##### Share on other sites

Hi,

You don't mean a Windows computer?....

Excel and Windows are OK? [Excel 97 "9"]

The operating system you are using currently is relying on it.

For interest, Windows inbuilt calculator has scientific mode; gives "-3"

randall Edited by randallc

##### Share on other sites

The last time I checked, Windows was written in C, so yes, Windows relies on the "incorrect" behavior. This is irrespective of any application which may do the correct mathematical behavior. Its not difficult to write a mod function, it's just difficult to do so efficiently (division is a very expensive operation).

The relevant portion of the article CyberSlug linked to is towards the bottom. Specifically, the post with this date is most useful:

Date: 07/05/2001 at 16:17:04

From: Doctor Peterson

##### Share on other sites

The entire world is relying on this "wrong" behavior. The electric you are using is provided to you courtesy of a broken modulo operator. The operating system you are using currently is relying on it. If your automobile has an on-board electronics system, chances are, it's relying on the wrong behavior. Get the point; do you really want to suggest we make AutoIt "right" so we can be different than the rest of the world?

Valik, I get your point, but I work with DSP code for mobile phones and I can assure that when we need to do a modulo operation, we do it right! I'd bet that any electrical or telecomunications engineer worth its salt does not rely on an incorrect C implementation for the implementation of its algorithms. C has many implementation issues. Savvy programmers work around them, they do not just live with them.

I guess that the best solution would be to create a new _Mod or _MathMod UDF that could go on the Math library which would have the right behaviour, and in the help page of Mod there could be a reference to it. That would make everyone happy, isn't it?

Cheers,

Angel

##### Share on other sites

Not all uses for mod are mathematical in nature. Sometimes you just want to know the result and don't really care if it's right or wrong, just what you expect and that it will be consistently what you expect. If it's necessary to have a true mod operation which expresses congruence, then you're right, people are going to use a "correct" version. If all that is important is expressing the remainder, people are going to use the built-in operator. The problem stems when somebody needs to express congruence but use the built-in operator which will not express congruence under certain circumstances.

##### Share on other sites

If MOD returns a negative result, add the base to get the mathematically accurate value.

```\$m = Mod(-45, 4)
if \$m < 0 then
\$m += 4
endif```

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

##### Share on other sites

_Mod or _MathMod UDF that could go on the Math library

Hi, Thanks to valik et al.

Yes, i gather "ada" has a "REM" command for just that, as well as "MOD"

Best, Randall

## Create an account

Register a new account

×

• Wiki

• Back

• Git