1

In my mind, the answer should be -1. Because 3//2 is equal to 1. Can someone explain why my console outputs -2?

value = -3//2
print(value)
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Tomarik
  • 75
  • 1
  • 7

2 Answers2

2

I'm assuming this is python and // is integer division

In python integer division // rounds down (left on the number line) to the nearest integer

-3/2 outputs a float -1.5 not integer division so no rounding

-3//2 outputs an integer -2 rounds down(left) to the nearest integer on the number line

number line

bogus
  • 457
  • 5
  • 16
1

If this is python, then // is known as floor division, and it is an operator that rounds the result down to the nearest whole number. Therefore

-3/2 = -1,5 rounded down to the nearest whole number is equal to -2.

Therefore:

-3//2 = -2.

As seen here Given that the red line is where -1,5 is, by rounding down (this means moving always left in the axis) the closest number is -2, which is what the operator does. It does not round to the closest number to 0, because that would mean it is rounding up, since it would be moving right in the axis.

Celius Stingher
  • 17,835
  • 6
  • 23
  • 53