Weird Objective-C Mod Behavior for Negative Numbers

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

    If this will be the behavior, and you know that it will be, then for m % n = r, just use r = n + r. If you're unsure of what will happen here, use then r = r % n.

    Edit: To sum up, use r = ( n + ( m % n ) ) % n

提交回复
热议问题