6 / (-132) = -1 ?
In Leetcode problem 150. Evaluate Reverse Polish Notation, a test case1
["10","6","9","3","+","-11","","/","","17","+","5","+"]
gave a wrong answer.
After looking into it, it turned out that Python caculates 6 / (-132) as -1, which seems weird to me.
The reason why that happened is that Python’s default division of intergers returns the floor(towards negitive infinity).
A simple way to avoid this is to use1
result = int(float(a) / b).