small medium large xlarge

11 Jan 2009, 00:50
Gareth (3 posts)


On page 18 I didn’t understand what “takes the floor of the result…” meant.

I really think this could be better explained.

Please tell me if this forum is not for feedback.

Thanks. G.

Be careful about using {%} and {/} with negative operands. Since Python 
takes the floor of the result of an integer division, the result is one 
smaller than you might expect: 
Download basic/neg_int_div.cmd 
>>> -17 / 10 
When using modulo, the sign of the result matches the sign of the 
second operand: 
Download basic/neg_int_mod.cmd 
>>> -17 % 10 
>>> 17 % -10 
23 Jan 2009, 04:02
Paul Archer (2 posts)

It’s a math term, but basically just means you get the lower whole number. In their example, -17 / 10 yields -2. -17/10 is actually -1.7, but it goes to the nearest lower whole number, which is -2. The reason they mention this is that it’s common to think of it as going to the smaller whole number, which would be -1.

28 Jan 2009, 03:55
Gareth (3 posts)


I’ve started reading a maths primer.

Can’t wait for this book to get completed.

14 Jun 2012, 14:40
Shawn Bowne (1 post)

I’m having trouble understanding -17 % 10 being equal to 3. How does the modulo work with negative numbers?

16 Jun 2012, 01:56
Paul Gries (46 posts)

That’s a great question, and it confuses a lot of people. Guido van Rossum, the inventor of Python, actually wrote a blog post about Why Python’s Integer Division Floors:

Note that in that article Guido is using Python 3.0, and the integer division operator changed between versions 2 and 3, from a single forward slash (/) to a double slash (//).

Does that article explain it well enough?