• 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: swift and objective-c
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: swift and objective-c
      • From: Jeremy Pereira <email@hidden>
References: 
 >swift and objective-c (From: "McLaughlin, Michael P." <email@hidden>)
 >Re: swift and objective-c (From: Roland King <email@hidden>)
 >Re: swift and objective-c (From: Ron Hunsinger <email@hidden>)
 >Re: swift and objective-c (From: Jeremy Pereira <email@hidden>)

  • Prev by Date: Re: swift and objective-c
  • Next by Date: Re: swift and objective-c
  • Previous by thread: Re: swift and objective-c
  • Next by thread: Re: swift and objective-c
  • Index(es):
    • Date
    • Thread