Re: Unsigned Modulo
Re: Unsigned Modulo
- Subject: Re: Unsigned Modulo
- From: Bavarious <email@hidden>
- Date: Wed, 12 Aug 2015 08:23:55 -0300
> On Aug 12, 2015, at 07:12, Gerriet M. Denkmann <email@hidden> wrote:
>
> OS X 10.10.4, Xcode 7 beta 3
>
> This code produces some (for me) unexpected results:
>
> NSUInteger unsignedSize = 3;
> NSInteger signedSize = 3;
>
> for ( NSInteger rawValue = -6; rawValue < 5; rawValue++ )
> {
> NSInteger modValueU = rawValue;
> NSInteger modValueS = rawValue;
> modValueU %= unsignedSize;
> modValueS %= signedSize;
> fprintf(stderr,"rawValue %+2ld modValueU %+2ld modValueS %+2ld\n", rawValue, modValueU, modValueS);
> };
>
> rawValue -6 modValueU +1 modValueS +0 expected modValueU = 0
> rawValue -5 modValueU +2 modValueS -2 expected modValueU = 1
> rawValue -4 modValueU +0 modValueS -1 expected modValueU = 2
> rawValue -3 modValueU +1 modValueS +0 expected modValueU = 0
> rawValue -2 modValueU +2 modValueS -2 expected modValueU = 1
> rawValue -1 modValueU +0 modValueS -1 expected modValueU = 2
> rawValue +0 modValueU +0 modValueS +0
> rawValue +1 modValueU +1 modValueS +1
> rawValue +2 modValueU +2 modValueS +2
> rawValue +3 modValueU +0 modValueS +0
> rawValue +4 modValueU +1 modValueS +1
>
> Probably the answer is: works as documented (but I can’t figure out the rule it is following).
That’s documented by the C standard. ISO C99 says in 6.5.5p6:
‘When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a.’
As a corollary, the sign of a%b is the same as the sign of a, which also means that a%b is not necessarily an element of the least residue system modulo b. Since the returned values are actually congruent modulo b to your expected values,
-2 (mod 3) ≣ 1 (mod 3)
-1 (mod 3) ≣ 2 (mod 3)
you can just add b to a%b when a%b is negative. Adding b to a value is an idempotent operation as far as the modulo b system is concerned.
>
> But even if so: why are there no warnings, compiling with these flags:
>
> CLANG_WARN_IMPLICIT_SIGN_CONVERSION
> GCC_WARN_ABOUT_POINTER_SIGNEDNESS
> GCC_WARN_SIGN_COMPARE
>
> Gerriet.
I’m not sure there’s a warning for this case since it is covered by the C99 and subsequent C standards.
If it helps, here’s a relevant entry in CERT’s C Coding Standard:
https://www.securecoding.cert.org/confluence/pages/viewpage.action?pageId=6422581
_______________________________________________
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