small medium large xlarge

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 ﬂoor of the result of an integer division, the result is one
smaller than you might expect:
>>> -17 / 10
-2
When using modulo, the sign of the result matches the sign of the
second operand:
>>> -17 % 10
3
>>> 17 % -10
-3
``````

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.

Cheers.

I’ve started reading a maths primer.

Can’t wait for this book to get completed.

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

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?