[Gambas-user] Modulo operator returns wrong results

Jesus ea7dfh at ...2382...
Sat Sep 28 23:33:45 CEST 2013


El 28/09/13 21:47, paulwheeler escribió:
> 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

Hi Paul et all

When working with some video games, there are a feature called 
wraparound or screen wrap. It consists on objects leaving one side of 
the screen that immediately reappear on the opposite side, maintaining 
speed and trajectory. For this task usually modular arithmetic is used, 
involving the Mod operator.

As I said before, on Python at least, it works as expected with negative 
numbers, and not only integers.

Lets imagine a spaceship in a drawing area 800px wide that reaches past 
the left edge of the screen. Just before it passes, say at pixel 2 in X 
coordinates, the operation would be:

2 Mod 800 = 2

So, nothing has changed in its position. But when the ship is at 
negative coordinates, say again -2:

-2 Mod 800 = 798

That's exactly the opposite side of the drawing area, which is what I 
had expected. If -2 would be returned instead, it won't work.

However this is not an issue for me, since this can be solved this way:

Private Sub fmod(Dd As Float, d As Float) As Float

     If Dd < 0 Then
         Return Abs((Frac(Dd / d)) * d - d)
     Else
         Return (Frac(Dd / d)) * d
     Endif

End

So no need to modify gambas at all, ;-)

Regards

----
Jesus Guardon




More information about the User mailing list