Re: Mod (%) function in C/Objective-C?
Re: Mod (%) function in C/Objective-C?
- Subject: Re: Mod (%) function in C/Objective-C?
- From: Dave <email@hidden>
- Date: Tue, 12 Aug 2014 17:01:41 +0100
On 12 Aug 2014, at 15:01, Keary Suska <email@hidden> wrote:
>
> On Aug 12, 2014, at 1:42 AM, Dave <email@hidden> wrote:
>
>>> 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…
>
> Yes, it's my brain. I just needed to see it to make sense of it.
>
>> I’m not doing any division, the function for “myMod” is as I posted:
>
> Well, it is the divisor I was asking about and although you aren't interested in the quotient, you are asking for division to occur.
>
>> 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.
>
> I see that you are interested in a circular index. Most of the time I find myself needing a "remainder" result more than a true modulo, so I use abs(). That definitely won't work when you need the true series.
Yes, this is what this thread is about. % was wrongly given the description of the modulus function in the 1960s/1970s and as a result of that one mis-naming “%” in C has been thought as a Modulus operation by a lot of people ever since (and it continues to be mis-described to this very day). The “%” character means a “remainder” not a “Modulus”, but are useful, but in this case I really wanted modulus. Most of the time remainder and modulus are the same thing, but when you introduce negative numbers remainder becomes a mirror image of the positive domain, where as modulus stays the same.
I’m not sure what you mean by dividing by a negative number is undefined? It seems to work ok for what I’m doing in another place:
NSInteger myIndex;
CGPoint myStartPoint;
CGSize mySize;
myStartPoint = -640;
mySize.width = 320;
myIndex = myStartPoint.x / mySize.width;
myIndex = -2 as expected.
Is that what you meant?
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