4
  • C++: cout << -1/2 evaluates to 0
  • Python: -1/2 evaluates to -1.

Why is this the case?

Steinar Lima
  • 7,644
  • 2
  • 39
  • 40
Chao Xu
  • 41
  • 3

4 Answers4

8

Integer division in C++ rounds toward 0, and in Python, it rounds toward -infinity.

People dealing with these things in the abstract tend to feel that rounding toward negative infinity makes more sense (that means it's compatible with the modulo function as defined in mathematics, rather than % having a somewhat funny meaning). The tradition in programming languages is to round toward 0--this wasn't originally defined in C++ (following C's example at the time), but eventually C++ (and C) defined it this way, copying Fortran.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike Graham
  • 73,987
  • 14
  • 101
  • 130
  • 1
    The "tradition" probably was due to the way integer division was implemented on popular computers. APL (A Programming Language) was first released in 1960, and it's modulo operator works as if integer division rounds towards negative infinity (the sign of the remainder is the same as the sign of the divisor (or zero) as opposed to the dividend). – rcgldr Feb 26 '14 at 04:28
  • For anyone not familiar with APL, check out http://en.wikipedia.org/wiki/APL_%28programming_language%29 -- I'd include a few cool snippets, but unfortunately the characters I'd need to show you not in the normal part of unicode and most computers would barf – Mike Graham Feb 26 '14 at 21:30
1

From the Python docs (emphasis mine):

The / (division) and // (floor division) operators yield the quotient of their arguments. The numeric arguments are first converted to a common type. Plain or long integer division yields an integer of the same type; the result is that of mathematical division with the ‘floor’ function applied to the result.

The floor function rounds to the number closest to negative infinity, hence -1.

Mike Graham
  • 73,987
  • 14
  • 101
  • 130
SethMMorton
  • 45,752
  • 12
  • 65
  • 86
1

For C++, from this reference: 5.2 — Arithmetic operators

It is easiest to think of the division operator as having two different “modes”. If both of the operands are integers, the division operator performs integer division. Integer division drops any fractions and returns an integer value.

Thus, -1/2 would yield -0.5 with the fraction dropped, yielding 0.

As SethMMorton indicated, Python's rule is floor, which yields -1. It's described in 5. Expressions.

Put in the terms that Mike Graham mentioned, floor is a round toward minus infinity. Dropping the fraction is a round toward zero.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lurker
  • 56,987
  • 9
  • 69
  • 103
-1

I am not sure about Python, but in C++ integer/integer = integer, and therefore in case of -1/2 is -0.5 which is rounded automatically to integer and therefore you get the 0 answer.

In case of Python, maybe the system used the floor function to convert the result into an integer.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131