-5

I am not understanding remainders on fractions. how is there a remainder of 1 for 1/2 , and then a different remainder for the same fraction but un-simplified.

any help would be appreciated.

I can follow how 3%2= 1 , but I do not understand the below:

print(1%2)
=1

print(2%4)
=2
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
pepperjack
  • 67
  • 7
  • 1
    huh, why would fractions come into the picture at all when you're talking about remainders? There are no fractions here. – Paritosh Singh Feb 20 '20 at 04:10
  • 2 divided by 4, as 2 is smaller than 4, the whole 2 will be a remainder. try 5%2 you will get 1 – venkata krishnan Feb 20 '20 at 04:12
  • 1
    You might be confusing `%` and `/`. The latter is the division, where `1/2` and `2/4` have the same result (in Python 3). `%` is a different operation altogether. – Amadan Feb 20 '20 at 04:18
  • So, I see this is getting a lot of down votes.. but there are people just beginning to learn Python, or programming in general. And they may have the same confusion and reach this question in their searches. So, I put in some effort to write a good description of how % and // work and how they can be used in a program below. =) – Todd Feb 20 '20 at 06:13

4 Answers4

1

These aren't fractions, these are simple remainder computations (technically, "modulo" operations, but the distinction is irrelevant for positive operands). 1 % 2 is saying "after removing all complete 2s from 1, what is left?" 2 % 4 is saying "after removing all complete 4s from 2, what is left?" Since in both cases there were no complete 2s or 4s to remove, "what's left?" is the whole amount.

If you want actual fractions, the fractions module will give you the results you expect:

from fractions import Fraction

print(Fraction(1, 2) == Fraction(2, 4))

will print True, because as a fractional quantity, rather than a remainder computation, they're equivalent.

Similarly, performing true division will (sometimes) work, e.g.:

print(1 / 2 == 2 / 4)

also prints True. That's less reliable in the general case though, since floating point math is "broken".

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Do you have an example where true division doesn't work? I think it works more than just "sometimes". – Kelly Bundy Feb 20 '20 at 04:51
  • @HeapOverflow: I'm using "sometimes" in the "not always" sense, not the "infrequently" sense. Plain true division will usually work with `int`s (possibly always on CPython, as CPython does some tricks to do much of the math with integers to minimize float error, converting quite late in the game; it doesn't just convert to `float` and divide the way C code would do by default). But it's going to eventually bite you in some other way if more "advanced" arithmetic is involved and you're not rounding or using `math.isclose` or the like to "fix" it (e.g. `1 / 10 + 2 / 10 != 3 / 10`). – ShadowRanger Feb 20 '20 at 05:02
  • Ok yeah, if you do further operations then yes it'll quickly cause issues. I thought you meant the test you showed, equality of two divisions. – Kelly Bundy Feb 20 '20 at 05:11
1

If you split 20 donuts among 3 people, everyone gets 6 donuts and 2 donuts remain.

If you split 1 donut among 2 people, everyone gets 0 donuts and 1 donut remains.

If you split 2 donuts among 4 people, everyone gets 0 donuts and 2 donuts remain.

Kelly Bundy
  • 23,480
  • 7
  • 29
  • 65
  • thank you , i seem to have used the wrong verbiage in my post by saying fractions. it is this logic that I was not understanding. – pepperjack Feb 20 '20 at 05:16
0

What you are asking is a mod % symbol it isn't the same as division /. Mod operator returns the remainder after integer division of its first argument by its second so if you divide 5 by 2 there is a 1 as remainder. If you try to divide a smaller number by a larger number you get the smaller number 1/2 is not divisible so 1 is the remainder.

Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
0

Understanding The Modulo Operator, %, and Its Mirror, //, and Their Usefulness

The modulo operator, %, gives the remainder of division. 1 % 2 would be like dividing 1 by 2, but stopping any further calculation because that's as far as we can go without using decimal notation, and keeping the part that wasn't divided (which is 1).

Using the modulo operator, %, doesn't conceptually take into account that 1/2 is equal to 2/4. With the expression op1 % op2, what op2 signifies is a range of values, while op1 signifies a value that can be reduced to a value within the range that op1 represents.

To show the usefulness of this in programming, I'll use a chess board as an example. Suppose I have a 1-dimensional array representing the board. If at some line in my code I need to convert an array index (say 23) of a square to chess notation (file & rank --> h6 for instance), I could do something like this...

>>> board = ['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br', # 8
...          'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', # 7
...          None, None, None, None, None, None, None, None, # 6
...          None, None, None, None, None, None, None, None, # 5
...          None, None, None, None, None, None, None, None, # 4
...          None, None, None, None, None, None, None, None, # 3
...          'wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', # 2
...          'wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr'] # 1
...         # a     b     c     d     e     f      g     h
>>>
>>> def position(sq_index):
...    return { 'file': sq_index % 8, 'rank': 7 - sq_index // 8 }
...
>>> position(23)
{'file': 7, 'rank': 5}
>>>
>>> def chess_notation(pos):
...     alpha = 'abcdefgh'
...     return alpha[pos['file']] + str(pos['rank'] + 1) # + 1 because ranks
...                                                      # start at 1 in 
...                                                      # notation.
...
>>> chess_notation(position(23))
'h6'

(In chess, the columns on the x-axis of the board are referred to as 'files' and the y-axis rows are the 'ranks')

Note that the implementation for position() has sq_index % 8 ("sq_index modulo eight") in it to determine the file value. The files on a chess board go from 'a' to 'h' (columns 0 to 7). If we count squares left to right 0 to 7, we get to the end and then repeat our counting on the next rank. The number of squares we may have counted so far could be 9, but using 9 % 8, we know our finger is now on the 'b' file (9 % 8 = 1).

So no matter what index we have within 0 to 63, it can be reduced to a value between 0 and 7. If index n increments from 0 to 63 sequentially, n % 8 will produce 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, .. and so on.

The calculation to get the rank is a similar operation. We have sq_index // 8. The // operator (or "floor division operator") produces an integer result with the remainder discarded (essentially the opposite of %). This gives the same result we'd get if implemented as math.floor( 1 / 8 ). // is a Python 3 feature. With Python 3, 1 / 8 produces a float result, while 1 // 8 produces an integer value.

So, say we count squares again, 0 to 7. At each square n_sq // 8 produces the same value, 0. Then we continue counting up the next rank, 8 to 15. Each of those index values produces 1 (e.g. 11 // 8 = 1). And so on. The board array in the code has ranks numbered 8 to 1, so to convert to the right rank value it's 8 - (sq_index // 8).

Note: For Python 2 the // operator, and the / operator both produce an integer value. This difference between Python 2 and 3 can cause some confusion when migrating.

Todd
  • 4,669
  • 1
  • 22
  • 30