WebApp Sec mailing list archives

Re: modulo question


From: Kelly John Rose <mllists () ptbcanadian net>
Date: Wed, 20 Apr 2005 09:50:17 -0400

From a purely mathematical point of view that is right.

a = b (mod c) or a%c = b <==> c divides (a-b)
ie. -1 = 1 (mod 2) since 2 divides ((-1) - 1) = -2
and -1 = -1 (mod 2) since 2 divides ((-1) - (-1)) = 0

In a very twisted vein
-1 = 153 (mod 2) since 2 divides ((-1) - 153) = -154

But these very likely, these functions in python and C++ are defined to give the answer that lies within (c, -c)

so the results you are given are both correct results. The problem is that apparently C++ and Python define their modulos slightly differently. Now, I do not know the details of their implementations.

.....Kelly John Rose.....

martin wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello list,

I have slightly academic question about modulo. I sent this message here because I thing there are many of gurus which working with modulo operator.

I have tried in python and in C++ following:
python C++ 1%2 1 1
- -1%2     1          -1
1%-2    -1           1
- -1%-2   -1          -1

What is right?
Do you know some applications which using modulo with negative numbers?
Have you some references to definition (mathematical def. and properties) of modulo?

Thanks in advance,
Martin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQFCX4Bc9w5olJf0Oq8RAsvSAKCnvAdrdXBL/aqiSTCANjT2BKb7NACcCCel
7b+pYp0k/1hMQPOyAY2M5/4=
=zLC1
-----END PGP SIGNATURE-----


\


Current thread: