• 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: How can I format an integer ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How can I format an integer ?


  • Subject: Re: How can I format an integer ?
  • From: Deivy Marck Petrescu <email@hidden>
  • Date: Tue, 4 Nov 2008 19:45:24 -0500


On Nov 4, 2008, at 19:02, Luther Fuller wrote:

On Nov 4, 2008, at 5:22 PM, Philip Aker wrote:
Thanks Barry, using recursion seems like a more logical approach.

on FormatInt(theNum, theSeparator)
if theNum > 999 then return (FormatInt(theNum div 1000, theSeparator) & theSeparator & text -3 thru -1 of ("00" & ((theNum mod 1000) as text)))
return theNum as text
end FormatInt
FormatInt(9.99999999E+8, ",")

I must disagree. Many years ago, I was asked to teach a Fortran programming course as an introduction to programming for CS students. In such a course, one arrives at the subject of "recursion". Explaining this to some students is not easy. The text book invariably gives the example of calculating the value of n!. This works only for small values of n and is very misleading. It is much more efficient to calculate n! with a loop. Very bad example.


So ... Don't do anything with recursion if you can avoid it.


Luther not to disagree with the above statement, indeed, recursion is much more CPU intensive, but, as Philip wrote above, it is more logical.
As for being faster, it is but not by much.
Using Smile's chrono function and computing up to 150! these are the times


{5.5332986448775E-4, 8.32098711274139E+81} -- 60! recursion
{7.35484678443754E-4, 7.15694570462638E+118} --80! loop
{0.001602336015, 7.15694570462638E+118}  -- 80! recursion
{0.001251235328, 9.33262154439441E+157} --100! loop
{0.001244410043, 9.33262154439442E+157}  -- 100! recursion
{8.19837168819504E-4, 9.33262154439442E+157} --100! loop
{0.001140023331, 9.33262154439442E+157} --100! loop
{0.001894458208, 5.71338395644586E+262} --150! loop
{0.002238773763, 5.71338395644585E+262} -- 150! recursion
{0.001810226164, 5.71338395644585E+262}--150! loop
{0.001832910199, 5.71338395644585E+262} -- 150! recursion
{0.001338317932, 5.71338395644586E+262}--150! loop
{0.001354497876, 5.71338395644586E+262} --150! loop



Deivy
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden
  • Follow-Ups:
    • Re: How can I format an integer ?
      • From: Jon Pugh <email@hidden>
References: 
 >Re: How can I format an integer ? (From: Barry Wainwright <email@hidden>)
 >Re: How can I format an integer ? (From: Philip Aker <email@hidden>)
 >Re: How can I format an integer ? (From: Luther Fuller <email@hidden>)

  • Prev by Date: Re: How can I format an integer ?
  • Next by Date: Re: How can I format an integer ?
  • Previous by thread: Re: How can I format an integer ?
  • Next by thread: Re: How can I format an integer ?
  • Index(es):
    • Date
    • Thread