+ 3

%= assignment operator

x=2 x%=4 (or x%=6, or x%=7 etc.) output: always 2 why? why it always gives as output the default value which assigned to x.

20th Jul 2018, 1:05 PM
tardigrade
tardigrade - avatar
11 odpowiedzi
+ 3
it means when we can't divide first value to another, then we must multiple 0 to dividible value, and plus the first value, yes?
20th Jul 2018, 1:34 PM
tardigrade
tardigrade - avatar
+ 2
thanks a lot Kishalaya, I understand what you wrote in all comments, but still blur for me why? I think this rule must rely to something in the math.
20th Jul 2018, 1:50 PM
tardigrade
tardigrade - avatar
+ 2
because you will get a decimal result and not the modulus, the modulus result of any operation like num%numbigger is always num
20th Jul 2018, 1:52 PM
Guillem Padilla
Guillem Padilla - avatar
+ 2
tardigrade That "something" in math is called Euclidean division: https://en.m.wikipedia.org/wiki/Euclidean_division Note the statement: "Given two integers a and b, with b ≠ 0, there exist unique integers q and r such that a = bq + r and 0 ≤ r < |b|, where |b| denotes the absolute value of b. The four integers that appear in this theorem have been given names: a is called the dividend, b is called the divisor, q is called the quotient and r is called the remainder." Here we are given a=2 and b=4. We obtain q=0 and r=2, since they satisfy those two equations, and *must* be unique.
20th Jul 2018, 2:51 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
I didn't understand 2=0*4+2. why multiple 0 to 4?
20th Jul 2018, 1:30 PM
tardigrade
tardigrade - avatar
+ 1
if this is a rule which we must obey, where I can find about it? Google gives nothing )
20th Jul 2018, 1:40 PM
tardigrade
tardigrade - avatar
0
a %= b is equival to a = a%b. The operation a%b produces the remainder of a when divided by b. For example, 25%3 = 1 (because 25 = 8*3 + 1) 17%5 = 2 (because 17 = 3*5 + 2) 12%4 = 0 (because 12 = 3*4 + 0) Here x was initally 2, and 2%4=2 (because 2=0*4+2), thus x%=4 gives you 2 for x. If, instead, you change the first line to x = 15, you'll see that after the second line x becomes 3. Hope that makes sense :)
20th Jul 2018, 1:23 PM
Kishalaya Saha
Kishalaya Saha - avatar
0
because you "can not" divide 2 by 4 with modulus
20th Jul 2018, 1:33 PM
Guillem Padilla
Guillem Padilla - avatar
0
Yes, I think, Maths experts come here please ;)
20th Jul 2018, 1:38 PM
Guillem Padilla
Guillem Padilla - avatar
0
tardigrade: Let me go back to why 25%3 = 1. Here we find the largest multiple of 3 that is not bigger than 25. That multiple is 24 (because 8*3=24, and 9*3=27, which is bigger). Now we do 25-24 and get 1, our remainder. Same for 2%4. The largest multiple of 4 that is not bigger than 2 is 0 (since 0*4=0, and 1*4=4, which is bigger than 2). Now we do 2-0 to get 2.
20th Jul 2018, 1:42 PM
Kishalaya Saha
Kishalaya Saha - avatar
0
because when you say x%= 4 you mean x = x%4; so 2%4 = 2 2%5 =2 2%6=2 2%7=2 2%8=2 2%9=2 to understand easily look at the following 2 divided by 4 equals 0 with a remainder of 2. 2 divided by 5 equals 0 with a remainder of 2. 2 divided by 6 equals 0 with a remainder of 2. 2 divided by 7 equals 0 with a remainder of 2. 2 divided by 8 equals 0 with a remainder of 2. Therefore, 2%4 evaluates to 2.
18th Jun 2023, 11:30 AM
Haylemeskel Haylemariam
Haylemeskel Haylemariam - avatar