WebApp Sec mailing list archives
Re: modulo question
From: <warnings () envisagement com>
Date: Wed, 20 Apr 2005 13:26:00 -0700
Generally speaking, the modulus operator is defined as a mapping from the integers to a modular ring of numbers. Hence, the result of any operation should be a nonnegative value ranging from zero to n-1 in canonical form. That said, what your chosen example here is poor simply because +1 and -1 are congruent modulo 2 (i.e, they are the same value). Hence using 1 and 3 would yield more insightful results as -2 and +1 are congruent modulo 3. Below is a quick example from PHP. 1% 3 == 1 -1% 3 == -1 1%-3 == 1 -1%-3 == -1 Note that +1 and -1 are not congruent modulo 3 so we gain more insight into what is happening. Here (and in your C++ example) the sign is a function of the operand (+-1) but not the modulus (3). That said, the developers have chosen different ways to deal with negativevalues here, and the Python results worry me. But without seeing the results using something like the numbers above, it is hard to tell what is happening.Also, did you intentionally include two minus signs below?
For a clear and simple mathematical definition, follow the link below. http://mathworld.wolfram.com/Congruence.html In essence, any pair of intergers, a and b, are said to be congruent modulo some integer m if the value a-b is an integral multiple of m. To get a solid grasp on the properties of modular arithmetic I would suggest reading an introductory text on "abstract algebra" at either the senior or graduate level. But I will not recommend that unless you have been trained to read such texts. As for applications using negative values in the modulus, I cannot come up with any off the top of my head, and coding I favor not doing so as your example illustrates the potentially inconsistent implementations of it (that and I was trained to think of modular arithmetic in terms of positive integers). hope this helps, Sean
Current thread:
- modulo question martin (Apr 18)
- Re: modulo question Michael Vergoz (Apr 20)
- Re: modulo question Skip Carter (Apr 20)
- Re: modulo question Scovetta Labs (Apr 20)
- Re: modulo question Federico CastaƱeda (Apr 20)
- Re: modulo question Kelly John Rose (Apr 20)
- Re: modulo question warnings (Apr 21)