# Can this equation with Modulo be simplified?

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.

-
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. (msdn.microsoft.com/en-us/library/0w4e0fzs.aspx) – 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

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).$$

-
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

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.

-
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