• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Mod (%) function in C/Objective-C?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Mod (%) function in C/Objective-C?


  • Subject: Re: Mod (%) function in C/Objective-C?
  • From: Keary Suska <email@hidden>
  • Date: Sun, 10 Aug 2014 09:16:08 -0600

On Aug 10, 2014, at 6:31 AM, Dave <email@hidden> wrote:

> 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?

I don't think so, although I would expect a C lib somewhere to address it. Anyway, isn't easier to just always abs(x)%y?

Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"


_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Mod (%) function in C/Objective-C?
      • From: Dave <email@hidden>
    • Re: Mod (%) function in C/Objective-C?
      • From: Scott Ribe <email@hidden>
References: 
 >Mod (%) function in C/Objective-C? (From: Dave <email@hidden>)

  • Prev by Date: Re: Bindings in Swift
  • Next by Date: Re: Mod (%) function in C/Objective-C?
  • Previous by thread: Mod (%) function in C/Objective-C?
  • Next by thread: Re: Mod (%) function in C/Objective-C?
  • Index(es):
    • Date
    • Thread