11 Jan 2009, 00:50
192x192_pragsmall

Gareth (3 posts)

Hi.

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 
-2 
When using modulo, the sign of the result matches the sign of the 
second operand: 
Download basic/neg_int_mod.cmd 
>>> -17 % 10 
3 
>>> 17 % -10 
-3 
23 Jan 2009, 04:02
Generic-user-small

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
192x192_pragsmall

Gareth (3 posts)

Cheers.

I’ve started reading a maths primer.

Can’t wait for this book to get completed.

14 Jun 2012, 14:40
Generic-user-small

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_mugshot_pragsmall

Paul Gries (44 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:

http://python-history.blogspot.ca/2010/08/why-pythons-integer-division-floors.html

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?

  You must be logged in to comment