Weird Objective-C Mod Behavior for Negative Numbers

前端 未结 12 1937
旧巷少年郎
旧巷少年郎 2020-12-09 15:00

So I thought that negative numbers, when mod\'ed should be put into positive space... I cant get this to happen in objective-c

I expect this:

-1 % 3          


        
12条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 15:03

    Not only java script, almost all the languages shows the wrong answer' what coneybeare said is correct, when we have mode'd we have to get remainder Remainder is nothing but which remains after division and it should be a positive integer....

    If you check the number line you can understand that

    I also face the same issue in VB and and it made me to forcefully add extra check like if the result is a negative we have to add the divisor to the result

提交回复
热议问题