Modulo operator in Objective-C returns the wrong result

Posted by Greg Maletic on Stack Overflow See other posts from Stack Overflow or by Greg Maletic
Published on 2010-03-12T05:44:01Z Indexed on 2010/03/12 5:47 UTC
Read the original article Hit count: 537

Filed under:
|
|

I'm a little freaked out by the results I'm getting when I do modulo arithmetic in Objective-C. -1 % 3 is coming out to be -1, which isn't the right answer: according to my understanding, it should be 2. -2 % 3 is coming out to -2, which also isn't right: it should be 1.

Is there another method I should be using besides the % operator to get the correct result?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about arithmetic