Difference between / in C++ and Python
Question: **Using Python 2.7**

I was trying to solve the Reverse Polish Notation problem on LeetCodeOJ.

[RPN on LeetCodeOJ](https://oj.leetcode.com/problems/evaluate-reverse-polish-
notation/)

I wrote my straightforward solution in Python as follows:

    
    
    class Solution:
    # @param tokens, a list of string
    # @return an integer
    def evalRPN(self, tokens):
        stack = []
        for token in tokens:
            if token in ["+" , "-" ,"*", "/"]:
                op1 = stack.pop()
                op2 = stack.pop()
                if token == "+":
                    stack.append(op2+op1)
                elif token == "-":
                    stack.append(op2-op1)
                elif token == "*":
                    stack.append(op2*op1)
                elif token == "/":
                    stack.append(op2/op1)
            else:
                stack.append(int(token))
        if len(stack) == 1:
            return stack.pop()
        else:
            return 0
    

This gets rejected on a test case:

    
    
    Input:  ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
    Output: 12
    Expected: 22
    

But if I modify the application of `'/'` operation to `stack.append(int(op2 /
(op1*1.0)))`, it succeeds.

The `/` operation is performed once on this input calculating `6/-132` which
results in `-1` using either of two ways.

Strangely, despite the fact that both evaluations result in `-1`, the program
as a whole differs in its output. As shown above, using the first way gives
`12` as the RPNEval while using the second would give `22`. What causes this?

I visited this [link](http://yucoding.blogspot.com/2013/12/leetcode-question-
evaluate-reverse.html#comment-form), but it only says that there is some
difference in the `/` operator in Python and C++. What is the difference?


Answer: If you are on Python 2, `/` does integer division (meaning, it drops the
remainder and just gives you the _rounded-down_ result) unless at least one of
the operands is of type `float` rather than `int`. You fix this by multiplying
with `1.0`, but you could also call `float(...)` on one of the operands. This
is similar to C++, however, in C++ the result is rounded _towards zero_ rather
than _down_ , meaning that you will receive different results with one
negative operand:

### C++:

    
    
    1 / 2       // gives 0
    (-1) / 2    // also gives 0
    

### Python 2:

    
    
    1 / 2       # gives 0
    (-1) / 2    # gives -1 (-0.5 rounded down)
    

### Python 3:

On Python 3, `/` always does proper floating point division, meaning that you
always get a `float` back, you can use `//` to restore the old behaviour

    
    
    1 / 2       # gives 0.5
    (-1) / 2    # gives -0.5
    
    1 // 2      # gives 0
    (-1) // 2   # gives -1
    

### Edited to add:

Since you are on Python 2.7 (see the edited question), it indeed seems to be
the integer division thing you are stuck at. To get the new Python 3-style
behaviour in Python 2, you can also run

    
    
    from __future__ import division
    

at the **beginning** of your program (it must be at the very start, or the
interpreter will complain)

### Yet another edit regarding `int(something)`

Beware that while integer division rounds _down_ , conversion to integer
rounds _towards zero_ , like integer division in C++.

