Hi,

I just got caught out by the C/Objective-C Implementation of the % (mod) 
function in XCode C/Objective-C. I remember having this very same problem years 
ago (after I solved it again this time).

It stems from the modulus (%) function not returning a true modulus for 
negative numbers - it returns the remainder not the modulus.


I fixed it easily enough by writing my own mod function as so:

-(NSInteger) modulusWithDividend:(NSInteger) theDividend andDivisor:(NSInteger) 
theDivisor
{
NSInteger               myDividend;

myDividend = theDividend % theDivisor;
if (myDividend < 0)
        myDividend =  myDividend + theDivisor;

return myDividend;
}

(there is also a C function for it).

——————

My question is, is there an inbuilt function/method I can use instead of wring 
my own?

Thanks a lot
Dave


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to