+ 1
20//6=3? 1,25%0,5?
Pls help on // and%
2 Respuestas
+ 7
a // b is integer division. Imagine you have $20 and want to buy as many apples as possible. One apple is $6.00 (~ high quality organic apples). How many apples can you buy?
=> 20/6 = 3.3333...
You can't buy 1/3 of an apple, so you'll have to settle with 3 apples. The result of an integer division is always an integer.
=> 20//6 = 3.
% is the modulo operator. It will tell you how much money you'll have after you bought the 3 apples: $20.00 - 3 * $6.00 = $2.00 => 20 % 6 = 2. In other words, the result of a modulo division is the remainder of the integer division.
=> 20 % 6 = 20 - (20//6)*6 = 2.
Same with 1.25 % 0.5:
1.25 // 0.5 = 2
2 * 0.5 = 1
1.25 - 1 = 0.25 (rest)
1.25 % 0.5 = 0.25
+ 1