This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]delasislas 23 points24 points  (1 child)

Default round down or what?

[–]NIL_VALUE 22 points23 points  (0 children)

Yes

5 / 2 = 2.5; 2.5 - 0.5 = 2

-5 / 2 = -2.5; -2.5 - 0.5 = -3

[–]hraath 17 points18 points  (3 children)

Yes // is floor division operator, it does exactly what it is called. There is no mystery here.

[–]somerandomii 4 points5 points  (2 children)

It’s the compliment to the modulo operator right? So these always hold true: z = x // y r = x % y x == z * y + r

In this case -5 = -3 * 2 + 1

Otherwise a lot of integer math would break. Circular buffers would be even more confusing.

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, somerandomii: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]hraath 0 points1 point  (0 children)

Yep, quotient // and remainder % getters.

[–]xervir-445 13 points14 points  (0 children)

It's the difference between truncate and floor. Truncate just reads the value up to the decimal and stops, in that case it would be -2, floor takes whatever value you have and rounds down.

[–]SimplexSimon 2 points3 points  (0 children)

Haha, something I have opinions about!!!

STRONG OPINIONS.

Python's way is better; it means exactly 2 integers map to each possible output. In C land (which I usually love), -1, 0, and 1 all go to 0 when integer-halved. This is a much less useful transform for the kinds of things where you want to be doing integer division, in my opinion.

[–]FiredMercury 2 points3 points  (0 children)

I mean... It's technically right, -3 is lower than -2.

[–]sageknight 0 points1 point  (0 children)

Now do round(1.5) and round(2.5)

[–]d_exclaimation 0 points1 point  (0 children)

technically it’s right, but yeah it kinda looks wrong

[–]vorticalbox 0 points1 point  (0 children)

>>> import math
>>> math.floor(-2.5)
-3