Re: swift and objective-c
Re: swift and objective-c
- Subject: Re: swift and objective-c
- From: Roland King <email@hidden>
- Date: Wed, 04 Jun 2014 22:30:12 +0800
On 4 Jun, 2014, at 10:09 pm, Jeremy Pereira <email@hidden> wrote:
> I think this got bounced by my mail system, apologies if it is a repost.
>
>
> On 3 Jun 2014, at 22:16, Ron Hunsinger <email@hidden> wrote:
>
>>
>> Quotient/remainder:
>> - In Swift, a % b ignores the sign of b. A non-zero remainder has the same sign as a.
>> - Ruby does it right; A non-zero remainder has the same sign as the divisor.
>>
>
> Well, it depends on how division works. You need to maintain the relationship a == a / b + a % b. That means, if you want the sign of the modulus to be the same as the divisor then -10 / 3 has to come out as -4, not -3. Now, personally, (coming from a mathematical background) I think that is how it should work, but I think many programmers would find that counter intuitive.
>
I think you meant
a == ( a / b ) * b + ( a % b )
And indeed if we take a as +/- 10, b as +/- 3 and assuming that '/' truncates ( ie rounds towards zero) then we have, rearranging
a % b == a - ( a / b ) * b
so
10 / 3 = 3 -> 10 % 3 = 1 ( 10 - 9 )
-10 / 3 = -3 -> -10 % 3 = -1 ( -10 - -9 )
10 / -3 = -3 -> 10 % -3 = 1 ( 10 - 9 )
-10 / -3 = 3 -> -10 % -3 = -1 ( -10 - -9 )
so indeed having the result of a % b be the sign of the numerator seems correct.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden