Modulo and Units...

Discussion in 'Mac Programming' started by ArtOfWarfare, Jan 28, 2014.

  1. ArtOfWarfare, Jan 28, 2014
    Last edited: Jan 28, 2014

    ArtOfWarfare macrumors G3


    Nov 26, 2007
    For the following questions, I'll be using two distinct units called A and B… you can call them the unit meter and unit second, if you need them to be specific units.

    Suppose I have A^3 % A^2. What is the result? It's 0, right? Because no matter what A is, A^3 / A^2 = A, and so the operation has been performed with no remainder. So it's 0.

    What about A^2 / A^3, though? When you divide one by the other you end up with A^-1… does that have a remainder? No, so it's 0 again? I feel less confident here.

    Suppose I have A % B. What is the result? Is there even any way this makes sense? (Note that A and B might be numbers, but they might not be. They might be the unit meter and the unit second.)

    If there's some way that A % B makes sense, then will it expand to A^2 % B?

    The application I'm working on is just a calculator application. It has the option of including units and the option of using a modulo operation, so I'd like to know what some good output would be if the user ever mixed the two together. If there's a logical answer, I'd like to give it, and if there isn't, I'll just present an error to the user.
  2. subsonix macrumors 68040

    Feb 2, 2008
    But A^-1 is the same as 1/A. If you multiply 1/A with A^3 you get back to A^2, so A^2 % A^3 is A^2.

    Not quite sure what you mean if A is not a number, if it's not then how are you suppose to calculate a reminder, or do any arithmetic on it.
  3. willieva macrumors 6502

    Mar 12, 2010
    When you said A^2 / A^3, did you actually mean A^2 % A^3? Modulo and division aren't exactly the same thing. 3/9 is 1/3, but 3%9 is 0

    A^2/A^3 = 1/A
    A^2 % A^3 = A^2

    Modulo doesn't make sense when talking about things like m/s.
  4. ArtOfWarfare thread starter macrumors G3


    Nov 26, 2007
    That would indicate that you have no remainder, because it divides perfectly evenly...?

    I'm just going to throw out all the units and give the user an error if they try mixing units and modulo.
  5. subsonix macrumors 68040

    Feb 2, 2008
    Well in this case, if A is say 3 then you get: 9 % 27 which is 9. I guess you could make something that is purely symbolic with no actual values involved, but afaik that's much harder to do than to calculate a value if the variables are known.
  6. ArtOfWarfare thread starter macrumors G3


    Nov 26, 2007
    Yeah, but I'm not dealing with numbers. I'm dealing with units. I read up Wikipedia's page on Modulo and they have an equation there from which I think I can reasonably conclude there's no way of doing this:

    a % n is a - (n * int(a/n)).

    If a and n consist of the same units, I can divide one by the other, but unless they're the exact same (and so evaluate to 1, in which case the answer here is 0), then I'm going to have to convert a unit to an integer. There's no way to convert a meter to an integer, for example, so there's no logical way of making this work, I don't think. We could ignore the conversion step and leave it as the unit, and then multiply it by the other unit. That's doable, but then you need to subtract one unit by another.

    So… I'm going to say this isn't doable.
  7. naples98 macrumors member


    Sep 9, 2008
    3%9 does not equal 0, it equals 3.
  8. iSee macrumors 68040


    Oct 25, 2004
    If I understand what you're doing...

    Modulo only makes sense when the units are measuring the same thing. It's similar in concept to subtraction.

    The units don't have to be the same... You could mix different units that measure the same thing, like meters and centimeters, as long as you handle the conversions (I know you are talking about units, but I'm assing you're doing this for the purpose of correctly labeling the units for results of operations on values with units... if not, nevermind). In that case it would probably make the most sense to express the result in terms of the first unit (becuase modulo is a "remainder" operation, where the result is what *remains* of the first value).

    So if it's meters and seconds, I think you've got to return an error. Something like meters^2 and meters are NOT measuring the same thing so you'd throw an error in that case too.

    (I wonder if modulo ever really makes sense for a squared unit or anything other than the first order? I can't think of anything off the top of my head, but that doesn't mean anything!)

Share This Page