Shop OBEX P1 Docs P2 Docs Learn Events
Modulus understanding — Parallax Forums

Modulus understanding

TCTC Posts: 1,019
edited 2014-06-14 09:31 in Propeller 1
Hello all,

I am trying to get a better understanding on a few things. One of them is the "modulus". I am having trouble understanding what is going on, how it works, and how could I show the same value on a calculator (so I can run through things). The manual is no help at all to me
X := Y // 4

If Y started out as 5 then Y // 4 equals 1, meaning the division of 5 by 4 results in a real number whose fractional component equals ¼, or .25.

WHAT!?

Could someone please help me understand it?

Thanks
TC

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-03-04 17:57
    That's not a very good explanation, as you're painfully aware. Instead, think of it in terms of the long division you learned in elementary school. With Spin's integer division, as in elementary-school division, you have a quotient and a remainder. "Modulus" is just a fancy word for "remainder." So, 10 / 3 == 3, with a remainder of 1. IOW 10 // 3 == 1.

    That's all there is to it.

    -Phil
  • TCTC Posts: 1,019
    edited 2014-03-04 18:08
    That's not a very good explanation, as you're painfully aware. Instead, think of it in terms of the long division you learned in elementary school. With Spin's integer division, as in elementary-school division, you have a quotient and a remainder. "Modulus" is just a fancy word for "remainder." So, 10 / 3 == 3, with a remainder of 1. IOW 10 // 3 == 1.

    That's all there is to it.

    -Phil

    I think I got it, just to make sure I understand would this be right?

    9//5 ==4 <--- was right first time
    OOPS! 9//5 == 1
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-03-04 18:20
    9 / 5 == 1 with a remainder of 4. So 9 // 5 == 4.

    -Phil
  • TCTC Posts: 1,019
    edited 2014-03-04 18:22
    9 / 5 == 1 with a remainder of 4. So 9 // 5 == 4.

    -Phil

    I just second guessed my self, and changed it. Now I fully understand it. Thanks
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2014-03-04 18:27
    Calculators often don't offer you a remainder function, so when you divide you get something like this...
    12345 / 256 = 48.22265625.

    To find the remainder, subtract the integer part, 48 here to leave 0.22265625, and then multiply the fractional remainder times the divisor, here 0.22265625 * 256 = 57. That is the integer remainder. Another way is to mulitply 48 * 256 = 12288, and subtract that from the original number, 12345 - 12288 = 57.

    57/256 = 0.22265625.

    If you are working in excel, and you have 12345 in one cell, and 256 in another, here are functions for the integer part and the integer remainder (modulus):
    =int(12345 / 256)
    =mod(12345, 256)

    Modular arithmetic is clock math. Given something that takes 86400 seconds, what is that in hours:minutes:seconds?
  • TCTC Posts: 1,019
    edited 2014-03-04 18:49
    Calculators often don't offer you a remainder function, so when you divide you get something like this...
    12345 / 256 = 48.22265625.

    To find the remainder, subtract the integer part, 48 here to leave 0.22265625, and then multiply the fractional remainder times the divisor, here 0.22265625 * 256 = 57. That is the integer remainder. Another way is to mulitply 48 * 256 = 12288, and subtract that from the original number, 12345 - 12288 = 57.

    57/256 = 0.22265625.

    If you are working in excel, and you have 12345 in one cell, and 256 in another, here are functions for the integer part and the integer remainder (modulus):
    =int(12345 / 256)
    =mod(12345, 256)

    Modular arithmetic is clock math. Given something that takes 86400 seconds, what is that in hours:minutes:seconds?

    That works for me. Now I can see what is going on, witch helps me understand things better. Thank you for the excel info, I use excel a lot to figure things out. And to answer your question(I hope) 2 hours : 0 minutes : 0 seconds
  • Mark_TMark_T Posts: 1,981
    edited 2014-03-05 04:41
    Consider the all integer equation:

    a * q + r = p (where all values are non-negative and 0 <= r < q)

    Then you can invert this uniquely, given the standard integer division and remainder/modulus functions:

    a = p / q , r = p mod q (remember r ranges from 0 .. q-1)

    Mathematically you can extend this to negative a and p, but many programming languages change the
    definition when q is negative to allow negative remainders (which are not mathematically well behaved).

    To be consistent a pair of integer division and modulus operators should obey the equation:

    p = (p/q) * q + (p mod q)

    [ put another way you can define p mod q == p - (p/q)*q ]
  • TCTC Posts: 1,019
    edited 2014-03-05 05:23
    Mark_T wrote: »
    Consider the all integer equation:

    a * q + r = p (where all values are non-negative and 0 <= r < q)

    Then you can invert this uniquely, given the standard integer division and remainder/modulus functions:

    a = p / q , r = p mod q (remember r ranges from 0 .. q-1)

    Mathematically you can extend this to negative a and p, but many programming languages change the
    definition when q is negative to allow negative remainders (which are not mathematically well behaved).

    To be consistent a pair of integer division and modulus operators should obey the equation:

    p = (p/q) * q + (p mod q)

    [ put another way you can define p mod q == p - (p/q)*q ]

    I am terribly sorry, but I was never good at math, and I never learned more than basic math and some algebra (not much). So, I don't understand your example.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2014-03-05 09:37
    And to answer your question(I hope) 2 hours : 0 minutes : 0 seconds

    If you meant to say 24 h : 0 m : 0 s, that would be correct. 86400 seconds in a day.

    As Mark points out, when dealing with negative numbers, you have to be careful and test how your program implements it. The Prop and Excel do it differently.

    In Spin, using dividend/divisor and dividend//divisor
    135 = 2 * 60 + 15
    -135 = -2 * 60 - 15
    135 = -2 * -60 + 15
    -135 = 2 * -60 - 15
    remainder has same sign as the dividend

    In Excel, using INT(dividend/divisor) and MOD(dividend, divisor)
    135 = 2 * 60 + 15
    -135 = -3 * 60 + 45
    135 = -3 * -60 - 45
    -135 = 2 * -60 - 15
    remainder has same sign as the divisor

    You can run into negative modulus problems, for example, an event happened 135 minutes ago. What is that in h:m? Or, it is now 1:05 am. At what clock time did the event occur? There is ambiguity in expression, like the difference of saying "ten fifty" versus "ten minutes to 11". Same thing.

    Note that =mod(135, 0) will give a divide by zero error in Excel, but in Spin, 135//0 will return zero.
  • noizebotnoizebot Posts: 3
    edited 2014-06-13 18:07
    Im not sure I still understand after reading all your comments.

    Im trying to use the modulus in conjunction with a random number generator. I want to limit the numbers it can choose to only 50.

    I dont seem to understand how I can achieve this.
  • kuronekokuroneko Posts: 3,623
    edited 2014-06-13 19:30
    noizebot wrote: »
    Im trying to use the modulus in conjunction with a random number generator. I want to limit the numbers it can choose to only 50.

    Let's say the variable rnd contains an arbitrary (pseudo-)random number (e.g. ?frqa). Then simply return this:
    return [COLOR="#FF8C00"]||[/COLOR](rnd // 50)
    
    If rnd is negative then the modulus is negative, we therefore apply the abs operator.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-06-14 09:31
    Please note that micro-controller do everything in integer maths, so the whole reason for modulus is divisions of integers will provide a number that is missing the remainder.

    Modulus is there to fetch the remainder when that is what you need. In some cases, it is all you need. In other cases you need both the product of the division and the remainder and have to do two calculations to get both.

    Using it for a random number generator to limit the range of the results just uses the modulus function alone.
Sign In or Register to comment.