Modulus and moon phases


#1



Code is correct but I cannot for the life of me understand what Modulus is. How do i know what the moon phase will be in a year with what I have written in the exercise?


console.log(365/27)
console.log(365%27)


I divide the number of days in a year(365) with how many days it takes the moon to rotate around the earth(27) and the result is: 13.518518518518519

This means that the moon rotates the earth 13.518518518518519 times in a year.

Then I do the same division but with a modulus sign and get the answer: 14
What does 14 even mean? Where did it come from?

I feel like If I dont understand this basic concept then I will have a real hard time in tthe future.
Could anyone explain a little better what modulus is and what its meaning is in this exercise?

Thanks in advance!


#2

Modulo division is just division with remainders. In normal division the interpreter does not include the remainders. :slight_smile:


#3

Thats exactly what i dont understand. How can there be a "remainder" anywhere? Remainder of what?

If i divide 2/2 i get 1. There is nothing "remaining". Is this exercise just wooshing over my head? o.O


#4

No don't use it or regard it when dividing small numbers but say you had 9 and 5 modulo division will return 4 which is the remain if I were to divide 9 by 5 regardless of whether it is a factor of 9 or not.


#5

Im not sure if Im following.

9 / 5 = 1.8

There is nothing remaining as far as i understand. Where does the 4 come in to the picture?


#6

This is normal division,

But this,

9 % 5 = 4

is modulo division. :slight_smile:


#7

But you just said modulo division would return 4.

Shouldnt 9 % 5 = 4 then?


#8

Lol oops that was a typo yes modulo division of 9 and 5 would bring back 4. :slight_smile:

Try it for yourself. :grin:


#9

Alright, but WHY? o.O
Where does the 4 come from?


#10

9 / 5 = 1.8

9 % 5 = 4

I dont see any relation at all.


#11

because the modulo (%) gives you the remainder. lets do a different example:

17 % 3

what do you reckon is the outcome? the answer is two. why? the biggest division 3 can make is 15 (15 / 3) so then we have 17 - 15 = 2 two remaining

does that make sense?


#12

The remainder of what? Im not subtracting so there cannot be a remainder. Am i missing something crucial?

Where does the number 15 come from? How come you randomly picked that number?

(Thanks for trying to explain!)


#13

okay, look at this table:

0 * 3 = 0
1 * 3 = 3
2 * 3 = 6
3 * 3 = 9
4 * 3 = 12
5 * 3 = 15
6 * 3 = 18
7 * 3 = 21

the table of 3. so if we have 17 what is the biggest whole number which is in the table of 3?

15 is in the table of 3 (which is less or equal to 17), making it the biggest possible division, so the remainder is what it remains after the biggest possible division (17 -15 = 2, so the remainder is 2)


#14

Doesn't the table go on to infinity? Why would 15 be the biggest?

Maybe it is easiest if I stick to the example in the exercise because I cannot put the random numbers you guys are providing in a real life scenario.

I understand:

365/27 = 13.518518518518519

13.518518518518519 is the number of times the moon rotates the earth in a year.

But:

365%27 = 14

14 here is just a number I got doing modulus instead of division. I have no idea how I got it and where it belongs or what its meaning is in real life.


#15

What it does, basically:
365 / 27 will be 13.51, after 13*27 = 351 and 365-351= your 14,
so 365%27 takes the maximum amount of 27 it can fit into 365 and what is left will show you as remainder!
That is my understanding of it.


#16

but you don't understand the modulo operator at all. You will need to understand the concept first before you can apply it in real life.

lets do your real life example, the table of 27:

... (0 till 11)
27 * 12 = 324
27 * 13 = 351
27 * 14 = 378
27 * 15 = 405
... (till infinity)

which number less then or equal to 365 (left operator) is the highest number in the table of 27 (right operator)?

351 is the right answer, agree? 378 is higher then 365.

so the module operator calculates that 351 is the biggest division possible. so then the modulo operator calculates the remainder (the rest of the number which didn't fit in the division) which is : 365 - 351 = 14

you don't do minus, i know, the modulo operator does this for you, this is pure to demonstrate how the modulo works.

in short: the right operator tries to find the biggest divisible number inside the left operator, what remains is the remainder, which is what the modulo operator gives you


#17

good approach :slight_smile: yes, lets hope your explanation is understood, i am clearly failing to explain this


#18

Wow, you two are golden! These are exactly the kind of explanations i needed to understand what was going on.

So in the context of this exercise, the remainder 14 is the number of days left in the year after the moon has rotated as many full rotations around the earth as possible.

Thank you so much!


#19

yes exactly, there are 14 days left, and you can't just discard them


#20