Modulus understanding
TC
Posts: 1,019
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
WHAT!?
Could someone please help me understand it?
Thanks
TC
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
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
I just second guessed my self, and changed it. Now I fully understand it. Thanks
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
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.
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.
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.
Let's say the variable rnd contains an arbitrary (pseudo-)random number (e.g. ?frqa). Then simply return this: If rnd is negative then the modulus is negative, we therefore apply the abs operator.
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.