[Gambas-user] Modulo operator returns wrong results

paulwheeler paulwheeler at ...546...
Sat Sep 28 21:47:35 CEST 2013


Gentlemen,

I have never heard of the mod operator being used the way you did in
your problem. However, because Jesus mentioned his calculator having a
mod function, I went to "http://web2.0calc.com/" and did the problem. I
was stunned to see that the result is 797!  It appears to me that you
are using "mod" the same way as the mathematics problem of 800 -3 =  797.

Where did you learn this definition of "mod"? I am really curious.

I am not a "C" programmer, but have used "mod" in other languages. My
experience has alway been the same as the definition given at the
beginning of the article Benoit referred to. see:
"http://en.wikipedia.org/wiki/Modulo_operation"

The definition I know is this one: "... the modulo operation finds the
remainder of division of one number by another."

Even the "Common pitfalls" section of that article is talking about a
dividend which implies division.

When I did a search for "Mod operator", wikipedia redirected me to the
modulus article.

When I did a search for "C Language operators", I found "Modulo" under
Arithmetic operators. see:
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B   When I clicked
on "Modulo" it also redirected me to the modulus article. Note that the
"Modulo" operator is just after the division operator.

When I did a search for "Visual Basic Language operators", I found "Mod
Operator" under Arithmetic operators. see:
http://msdn.microsoft.com/en-us/library/se0w9esz.aspx, where it gives
this definition: "Divides two numbers and returns only the remainder."

Their example (under "Result") again gives the same definition and then
gives an example: "The result is the remainder after number1 is divided
by number2. For example, the expression 14 Mod 4 evaluates to 2."

http://www.tutorialspoint.com/python/python_basic_operators.htm
%	Modulus - Divides left hand operand by right hand operand and returns
remainder

However, when one of the numbers is negative, then the remainder is not
what you would expect with both being positive.


One very confused programmer,

Paul

On 09/24/2013 06:05 PM, Benoît Minisini wrote:
> Le 25/09/2013 02:55, Jesus a écrit :
>> El 25/09/13 02:42, Benoît Minisini escribió:
>>> Le 25/09/2013 01:23, Jesus a écrit :
>>>> I've been racking my brain with a simple problem and I've discovered
>>>> something weird with "Mod" operator, returning wrong results.
>>>>
>>>> For example, -3 Mod 800 gives -3, and it should be 797.
>>>>
>>>> It's only me or someone else has wrong results?
>>>>
>>>
>>> See
>>> http://en.wikipedia.org/wiki/Modulo_operation#Common_pitfalls
>>>
>>> -3 Mod 800 = -(3 Mod 800)
>>>
>>> This is not the mathematical modulus operator.
>>>
>>
>> What is it, then? In Python, Modulus operator is % and gives the correct
>> results. Also the calculator program has a Mod function which gives the
>> expected result.
>>
>> Anyway I've made a function that mimics the Python modulus, so not a big
>> problem.
>>
>> Thanks
>>
> 
> I don't know Python, but the 'Mod' Gambas operator follow the behaviour 
> of the '%' C operator. Maybe it was not a good idea.
> 
> Regards,
> 




More information about the User mailing list