Discuss Scratch

Crow_Boy08
Scratcher
1000+ posts

What is () mod () for

Sounds like something you would hear in college
(() mod ())

Last edited by Crow_Boy08 (Jan. 6, 2023 22:05:39)

jvvg
Scratcher
1000+ posts

What is () mod () for

It tells you the remainder if you divide the first number by the second number. For example, 7 mod 3 is 1, 25 mod 5 is 0, 20 mod 6 is 2, etc.
Crow_Boy08
Scratcher
1000+ posts

What is () mod () for

jvvg wrote:

It tells you the remainder if you divide the first number by the second number. For example, 7 mod 3 is 1, 25 mod 5 is 0, 20 mod 6 is 2, etc.
What is this useful for
medians
Scratcher
1000+ posts

What is () mod () for

Crow_Boy08 wrote:

jvvg wrote:

It tells you the remainder if you divide the first number by the second number. For example, 7 mod 3 is 1, 25 mod 5 is 0, 20 mod 6 is 2, etc.
What is this useful for
Some examples here:
https://en.scratch-wiki.info/wiki/()_Mod_()_(block)#Example_Uses
lgrov44
Scratcher
500+ posts

What is () mod () for

Also, you can use this to calculate (x) mod (y):
(x) mod (y) = x - y⌊x/y⌋
Or in Scratch Blocks:
<((x) mod (y)) = ((x) - ((y) * ([floor v] of ((x) / (y)))))>
NMario84
Scratcher
1000+ posts

What is () mod () for

I've seen MOD operators being used to loop animation/costume frames of a sprite. So, you could use the operator to calculate animations.
medians
Scratcher
1000+ posts

What is () mod () for

Also the mod operator is used in quite a lot of programming languages..
Spentine
Scratcher
1000+ posts

What is () mod () for

Crow_Boy08 wrote:

jvvg wrote:

It tells you the remainder if you divide the first number by the second number. For example, 7 mod 3 is 1, 25 mod 5 is 0, 20 mod 6 is 2, etc.
What is this useful for

There are many reasons it's useful.

1. Matricies (2d lists)
If you ever want a matrix, it's just going to be a list but with a 2d mapping. If we take an example of a 7x7 matrix, then item 5 in the list would be item 5, 1 in the matrix. Item 16 would be item 2, 2. The thing is, this uses the mod (aka modulo) operator along with the floor and divide operator to accomplish this.

2. Looping Animations
If a game has 4 costumes, each of them being an animation frame, then by the looping nature of the mod operator, the code (switch costume to ((costume # mod 4) + 1)) could be used to loop it, since after 3, 4 could be taken away from 4 and it will be looped.

3. Clocks and Timers
Imagine you are using the timer block and want to display how long the player has been playing. For example, 73 seconds will be 1:13, 250 seconds will be 4:10, and 3732 seconds will be 1:02:12. Well, with the modulo operator, it will make your life much easier when programming this concept. First you take the original input mod 60 (because there are 60 seconds, and then after 59 it loops back to 0), and the output is how many seconds the player has played. Take the number of seconds away from the original input and divide by 60, and for now let's call this number x. Once again, take the answer mod 60. This will be how many minutes have elapsed. Take the number of minutes away from x, and divide by 60. This will be the number of hours. For now I'll stop, but this can be extended all the way up clocks and dates, just using mod 24 instead of 60 for hours, and then 365 for days.

I hope you can see why it's useful now. Also don't read the whole clock thing it's pretty bulky in size.
Crow_Boy08
Scratcher
1000+ posts

What is () mod () for

Spentine wrote:

3. Clocks and Timers
Imagine you are using the timer block and want to display how long the player has been playing. For example, 73 seconds will be 1:13, 250 seconds will be 4:10, and 3732 seconds will be 1:02:12. Well, with the modulo operator, it will make your life much easier when programming this concept. First you take the original input mod 60 (because there are 60 seconds, and then after 59 it loops back to 0), and the output is how many seconds the player has played. Take the number of seconds away from the original input and divide by 60, and for now let's call this number x. Once again, take the answer mod 60. This will be how many minutes have elapsed. Take the number of minutes away from x, and divide by 60. This will be the number of hours. For now I'll stop, but this can be extended all the way up clocks and dates, just using mod 24 instead of 60 for hours, and then 365 for days.

I hope you can see why it's useful now. Also don't read the whole clock thing it's pretty bulky in size.
Can you just make 3 variables: Second, minute, and hour. Maybe a day one if a save code is involved.

Last edited by Crow_Boy08 (Jan. 7, 2023 17:30:12)

k0d3rrr
Scratcher
1000+ posts

What is () mod () for

Two uses I have found for ‘() mod ()’ are:
  • Looping a moving background
    forever
    set x to ((x position) mod (-480)) // Or a different x position
    change x by (-10)
    end
  • Options:
    change [option v] by (1)
    set [option v] to ((option) mod (2)) // Sets 'option' to either '0' or '1'
medians
Scratcher
1000+ posts

What is () mod () for

Crow_Boy08 wrote:

Spentine wrote:

3. Clocks and Timers
Imagine you are using the timer block and want to display how long the player has been playing. For example, 73 seconds will be 1:13, 250 seconds will be 4:10, and 3732 seconds will be 1:02:12. Well, with the modulo operator, it will make your life much easier when programming this concept. First you take the original input mod 60 (because there are 60 seconds, and then after 59 it loops back to 0), and the output is how many seconds the player has played. Take the number of seconds away from the original input and divide by 60, and for now let's call this number x. Once again, take the answer mod 60. This will be how many minutes have elapsed. Take the number of minutes away from x, and divide by 60. This will be the number of hours. For now I'll stop, but this can be extended all the way up clocks and dates, just using mod 24 instead of 60 for hours, and then 365 for days.

I hope you can see why it's useful now. Also don't read the whole clock thing it's pretty bulky in size.
Can you just make 3 variables: Second, minute, and hour. Maybe a day one if a save code is involved.
I mean, it's an example, though you could do something like this:

Spentine
Scratcher
1000+ posts

What is () mod () for

medians wrote:

Crow_Boy08 wrote:

Spentine wrote:

- snip -
Can you just make 3 variables: Second, minute, and hour. Maybe a day one if a save code is involved.
I mean, it's an example, though you could do something like this:


The alternate untested code should also work.

define convert seconds (time)
set [days v] to (time)
set [seconds v] to ((days) mod (60))
set [days v] to (((hours) - (seconds)) / (60))
set [minutes v] to ((days) mod (60))
set [days v] to (((days) - (minutes)) / (60))
set [hours v] to ((days) mod (24))
set [days v] to (((days) - (hours)) / (24))

Last edited by Spentine (Jan. 7, 2023 18:01:15)

Crow_Boy08
Scratcher
1000+ posts

What is () mod () for

medians wrote:

Crow_Boy08 wrote:

Spentine wrote:

3. Clocks and Timers
Imagine you are using the timer block and want to display how long the player has been playing. For example, 73 seconds will be 1:13, 250 seconds will be 4:10, and 3732 seconds will be 1:02:12. Well, with the modulo operator, it will make your life much easier when programming this concept. First you take the original input mod 60 (because there are 60 seconds, and then after 59 it loops back to 0), and the output is how many seconds the player has played. Take the number of seconds away from the original input and divide by 60, and for now let's call this number x. Once again, take the answer mod 60. This will be how many minutes have elapsed. Take the number of minutes away from x, and divide by 60. This will be the number of hours. For now I'll stop, but this can be extended all the way up clocks and dates, just using mod 24 instead of 60 for hours, and then 365 for days.

I hope you can see why it's useful now. Also don't read the whole clock thing it's pretty bulky in size.
Can you just make 3 variables: Second, minute, and hour. Maybe a day one if a save code is involved.
I mean, it's an example, though you could do something like this:

You can change convert seconds to change secs by 1

Powered by DjangoBB