6/(-132)= 0 or -1


  • 47
    Y

    when I test ["10","6","9","3","+","-11","","/","","17","+","5","+"],
    in this program, the result I got is 12, I think I am right. Because when I calculate 6/(-132) = -1, not 0, so i think the result is 12 not 22.


  • 33
    L

    You're using Python, right? I think it's because in java, 6/(-132) = 0 (java use truncation).
    You should use int(float(a)/b) (in this case, a=6 and b=-132) and you can get the same result with the answer


  • 1
    O

    Yes, you were right. Anyway, if the user is using python, the results should be validated with the results obtained in Python. Thanks for the info with the casts.


  • 5
    M

    Just got the same problem!
    Python program got wrong answer!
    I think the OnlineJudge should consider more about diffrent language.


  • 1
    J

    6/(-132) = -1, because Python try to Integer toward 0.
    if a % b != 0 and a / b < 0: temp = a / b + 1 else: temp = a / b


  • 0
    M

    I agree with you, maybe OJ should adjust the result for different languages


  • 0
    Z

    I got the same issue. I think it's OK evaluate Python with same standard answer. But need to clarify the rule of negative integer division on the question page (or display this test case on the page)


  • 1
    M

    No it shouldn't. If you are preparing yourself for an interview they will also evaluate your knowledge in the language you use. However it won't probably be considered as a terrible mistake. I didn't remember this Python division issue either, so I'm glad I step into this problem here and not in a real interview.


  • -1
    W

    this problem means the Admin who is creating the test case didn't think thoroughly.

    They should consider the language compatibility. No excuse.


  • -1
    E

    @whglamrock Don't complain. If you are working on so many languages all together, you will know how hard it is to keep track of everything


  • 1
    E

    Javascript also have similar problem and I feel it's impossible to pass all the test cases.
    if using Math.round, 6/(-132) will evaluate to -0, however in another test case (4 + (13 / 5)) -> 6, 13/5 will evaluate to 3. If using Math.floor, the first will evaluate to -1 the second will evaluate to 2.


  • 0

    I used Python 3 on PyCharm, and the result is the same as the answer. But if I run it on LeetCode, the result is -1. You can use int(operator.truediv(a, b)) or int(float(a)/b) etc.


  • 0
    L

    @ethdragon I found just use parseInt() is OK


  • 0
    U

    Exactly the same problem. I think the solution should be generated using the same language as the submission does.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.