Re: Mod (%) function in C/Objective-C?
Re: Mod (%) function in C/Objective-C?
- Subject: Re: Mod (%) function in C/Objective-C?
- From: koko <email@hidden>
- Date: Mon, 11 Aug 2014 15:15:03 -0600
In computing, the modulo (sometimes called modulus) operation finds the remainder of division of one number by another.
Given two positive numbers, a (the dividend) and n (the divisor), a modulo n (abbreviated as a mod n) is the remainder of theEuclidean division of a by n. For instance, the expression "5 mod 2" would evaluate to 1 because 5 divided by 2 leaves aquotient of 2 and a remainder of 1, while "9 mod 3" would evaluate to 0 because the division of 9 by 3 has a quotient of 3 and leaves a remainder of 0; there is nothing to subtract from 9 after multiplying 3 times 3. (Note that doing the division with a calculator won't show the result referred to here by this operation; the quotient will be expressed as a decimal fraction.)
Although typically performed with a and n both being integers, many computing systems allow other types of numeric operands. The range of numbers for an integer modulo of n is 0 to n − 1. (n mod 1 is always 0; n mod 0 is undefined, possibly resulting in a "Division by zero" error in computer programming languages) See modular arithmetic for an older and related convention applied in number theory.
When either a or n is negative, the naive definition breaks down and programming languages differ in how these values are defined.
On Aug 11, 2014, at 3:03 PM, Dave <email@hidden> wrote:
>
> On 10 Aug 2014, at 17:04, Scott Ribe <email@hidden> wrote:
>
>> On Aug 10, 2014, at 9:16 AM, Keary Suska <email@hidden> wrote:
>>
>>> I don't think so, although I would expect a C lib somewhere to address it.
>>
>> I think the standard C libs only have floating-point versions of mod functions. (That does seem like an odd omission.)
>>
>> This would at least be a tiny bit better if people would learn to quit incorrectly referring to % as mod, but I guess that ship has sailed (all the way off the edge of the earth, actually)…
>
> Actually I think it’s circumnavigated it a few times!
>
> That’s why I made the mistake in the first place, I was think of % as being a true mod function, not a remainder. In Ruby for instance it works as you’d expect a mod function to work and it uses % too, not sure about Java?
>
> From doing a bit of digging:
>
> Based on the C99 Specification: a = (a / b) * b + a % b
>
> We can write a function to calculate (a % b) = a - (a / b) * b!
>
> int remainder(int a, int b)
> {
> return a - (a / b) * b;
> }
> For modulo operation, we can have the following function:
>
> int mod(int a, int b)
> {
> int r = a % b;
> return r < 0 ? r + b : r;
> }
>
> My conclusion is (a % b) in C is a remainder operator and NOT modulo operator.
>
> All the Best
> 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
_______________________________________________
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