• 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: Dave <email@hidden>
  • Date: Tue, 12 Aug 2014 08:42:03 +0100

On 12 Aug 2014, at 00:41, Keary Suska <email@hidden> wrote:

>
> On Aug 11, 2014, at 2:52 PM, Dave <email@hidden> wrote:
>
>> On 10 Aug 2014, at 16:16, Keary Suska <email@hidden> wrote:
>>
>>
>>> 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?
>>>
>>
>>
>>> abs(x)%y
>>
>> Doesn’t give the same result, it’s one off:
>>
>> myIndex: -5  myMod: 1   abs(x)%y: 2
>> myIndex: -4  myMod: 2  abs(x)%y: 1
>> myIndex: -3  myMod: 0  abs(x)%y: 0
>> myIndex: -2  myMod: 1  abs(x)%y: 2
>> myIndex: -1  myMod: 2  abs(x)%y: 1
>> myIndex: 0  myMod: 0  abs(x)%y: 0
>> myIndex: 1  myMod: 1  abs(x)%y: 1
>> myIndex: 2  myMod: 2  abs(x)%y: 2
>> myIndex: 3  myMod: 0  abs(x)%y: 0
>> myIndex: 4  myMod: 1  abs(x)%y: 1
>>
>> Where:
>>
>> myIndex = input value.
>> myMod = result of calling the mod function I added.
>> abs(x)%y =  abs(x)%y.
>
> Maybe my brain isn't working correctly but that doesn't make sense to me. Could you show the output with both x and y shown? Now, you aren't dividing by a negative integer, are you? I believe that is undefined…

I’m not doing any division, the function for “myMod” is as I posted:

Try it yourself:

NSInteger							myIndex;
NSInteger							myNewIndex;
NSInteger							myNewNewIndex;
for (myIndex = -5;myIndex < 5;myIndex++)
	{
	myNewIndex = [self modulusWithDividend:myIndex andDivisor:3];
	myNewNewIndex = abs(myIndex) % 3;
	NSLog(@"myIndex: %i  myNewIndex: %i  myNewNewIndex: %i",myIndex,myNewIndex,myNewNewIndex);
	}

—————————————

If you think of the number as being used as an Index into an NSArray, you can see why a true mod function is better, e.g. -1 gets you the last element, -2 the second from last and so on.

Cheers
Dave










_______________________________________________

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: Keary Suska <email@hidden>
References: 
 >Mod (%) function in C/Objective-C? (From: Dave <email@hidden>)
 >Re: Mod (%) function in C/Objective-C? (From: Keary Suska <email@hidden>)
 >Re: Mod (%) function in C/Objective-C? (From: Keary Suska <email@hidden>)

  • Prev by Date: Re: Mod (%) function in C/Objective-C?
  • Next by Date: How to get SDK at compile time?
  • Previous by thread: Re: Mod (%) function in C/Objective-C?
  • Next by thread: Re: Mod (%) function in C/Objective-C?
  • Index(es):
    • Date
    • Thread