Weird Objective-C Mod Behavior for Negative Numbers

前端 未结 12 1907
旧巷少年郎
旧巷少年郎 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:11

    There are two choices for the remainder, and the sign depends on the language. ANSI C chooses the sign of the dividend. I would suspect this is why you see Objective-C doing so also. See the wikipedia entry as well.

提交回复
热议问题