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