Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am unsure about how to simplify equations containing the modulo operator (%). Can this expresssion be simplified?

(((X - aw) % w) - w) % w

The values cannot be assumed to be integers.

share|cite|improve this question
All your variables take on integer values, I presume? Assuming they are, apparently you just have X % w... – J. M. Jul 17 '11 at 0:54
No, they can't be assumed to be integers. I will update the question. – Groky Jul 17 '11 at 0:54
What's the definition of your modulo operator? – Qiaochu Yuan Jul 17 '11 at 0:59
This comes from a C# program and so uses C#'s modulo operator: The modulus operator (%) computes the remainder after dividing its first operand by its second. ( – Groky Jul 17 '11 at 1:02
If you aren't assuming integers, then what's .9 % .1? – tomcuchta Jul 17 '11 at 1:21

In C#, the modulo operator gives a negative value if one of the arguements is negative, and positive otherwise. So 5%2==1, and -4%3==-1. To get the "standard" modulo answer, you just have to test if its negative, and if so, add the modulus (if you want a branchless if here, just add the modulus and then take the result modulo the modulus).

Given that, you expression reduces to what @eric said:

If X≥aw then we have ((X−aw)%w)−w, and if X < aw then we have ((X−aw)%w).

EDIT: Warning, working with floats/doubles introduces error into your calculations. You may find it a better idea to use decimal as it preserves the decimal digits that you are working with.

share|cite|improve this answer
Some editing needed here on the display; and if $X$ ... what? – Gerry Myerson Jul 17 '11 at 3:00
Apologies, there was a weird (to me) formmating issue (the '<' hid the rest of the line. – soandos Jul 17 '11 at 3:04

Stranger things happen if the numbers are negative. Suppose they are positive. If $X\geq aw$ then we have $$ \left(\left(X-aw\right)\%w\right)-w,$$ and if $X<aw$ then we have $$ \left(\left(X-aw\right)\%w\right).$$

share|cite|improve this answer
Putting that equation in my program doesn't produce the same output. I think it may be because the % operator doesn't output a number between 0 and 1 like you say: "5 % 3" outputs 2, as the remainder of 5 / 3 is 2. – Groky Jul 17 '11 at 1:19
@Groky: Here, try this now, I think it should be fixed. (I was confused about the meaning of $\%$ for some reason) – Eric Naslund Jul 17 '11 at 1:25
Sorry, still no good. I'm not sure what you meant by -w being an integer multiple of w (-w and w are obviously always integer multiples; one's a negation of the other!) but w cannot be assumed to be an integer. – Groky Jul 17 '11 at 1:33
@Groky: Ok there is something strange going on here, which is that the $\%$ operator outputs negatives as well. (This makes absolutely no sense to me, and seems like bad design. Why output into $[-w,w]$ instead of just $[0,w]$...) Anyway, try what I wrote above now. I am going to just delete this if it is wrong again. – Eric Naslund Jul 17 '11 at 1:57
@Eric: I agree that it sounds wrong. This was discussed in sci.math ages ago, and IIRC the explanation went something like this. Two integer operations were to be defined: $a\%b$ and $a DIV b$. IOW $a,b$ are integers, $b\neq0$ and so should the answers. A starting point was that $a DIV b:=[a/b]$ for positive integers. Another starting point was that the equation $$ a= (a DIV b) b + (a\%b)$$ should always be true. Sounds reasonable, but then adding yet another "reasonable" premise $(-a) DIV b = -(a DIV b)$ to the mix lead to the conclusion that $a\%b$ had to be occasionally negative. – Jyrki Lahtonen Jul 17 '11 at 6:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.