3
$\begingroup$

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.

  • 0
    All your variables take on integer values, I presume? Assuming they are, apparently you just have `X % w`...2011-07-17
  • 0
    No, they can't be assumed to be integers. I will update the question.2011-07-17
  • 0
    What's the definition of your modulo operator?2011-07-17
  • 0
    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. (http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx)2011-07-17
  • 0
    If you aren't assuming integers, then what's .9 % .1?2011-07-17
  • 0
    tomcuchta: .9 % .1 gives 0.0999999999999999.2011-07-17
  • 0
    That is terribly odd; since .1 cleanly divides .9, the result ought to be 0, no? ;)2011-07-17
  • 0
    So ... in order for mathematicians to answer the question, you must provide a mathematical definition of this strange operation.2011-07-17
  • 0
    @J.M: Hmm, Yes! WTF? Maybe it's because it's 2:30am here, but you've made me terribly confused. I've double checked and that's what it gives. I think I may need to ask a question on stackoverflow. Either that or get some sleep ;)2011-07-17
  • 0
    @GEdgar: Yes, maybe. I came from a programming background and the % (modulo) operator is a common operator there. I've never considered the vagaries of it, but assumed it would be familiar to mathematicians. Seems not...2011-07-17
  • 0
    It is VERY familiar to mathematicians, but we require that its arguments are integers, else it seems ambiguous in the "what's the remainder when I divide these two numbers?" sense.2011-07-17
  • 0
    Ok, I think this question addresses it: http://stackoverflow.com/questions/2925223/floating-point-arithmetic-modulo-operator-on-double-type2011-07-17
  • 0
    So it seems that .9 % .1 *should* equal 0, but floating point inaccuracies are producing 0.09999... The definition of a % b however is a - (b * floor(a/b)). Sorry if this is getting too far away from maths. I think I will just leave the equation as-is ;)2011-07-17
  • 0
    @tom: not really; I talk about things like $\bmod 2\pi$ or $\mod \omega_3$ when working with (singly/doubly) periodic functions...2011-07-17
  • 0
    @Groky: You are using *double* precision, I assume? If this is what double precision is giving, chuck your compiler in the refuse and write a few steaming letters to your developers...2011-07-17
  • 0
    @J.M. Interesting, I had no idea. Thanks.2011-07-17
  • 0
    @J.M since it is not possible to totally express .1 in binary in finite space, what do you really expect to happen? This is part of the reason the decimal type was created.2011-07-17
  • 0
    I'm perfectly aware @soandos; I suppose I have been spoiled by working in environments where the "binariness" of the arithmetic is mostly under the hood and .9/.1 gives 9.00000... :)2011-07-17

2 Answers 2

2

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.

  • 0
    Some editing needed here on the display; and if $X$ ... what?2011-07-17
  • 0
    Apologies, there was a weird (to me) formmating issue (the '<' hid the rest of the line.2011-07-17
1

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

  • 0
    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.2011-07-17
  • 0
    @Groky: Here, try this now, I think it should be fixed. (I was confused about the meaning of $\%$ for some reason)2011-07-17
  • 0
    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.2011-07-17
  • 0
    @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.2011-07-17
  • 0
    @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.2011-07-17