• 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: Rounding off in AppleScript
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Rounding off in AppleScript


  • Subject: Re: Rounding off in AppleScript
  • From: Nigel Garvey <email@hidden>
  • Date: Thu, 14 Apr 2005 02:05:52 +0100

Paul Berkowitz wrote on Tue, 12 Apr 2005 20:33:38 -0700:

>> From: Scott Babcock <email@hidden>
>> Date: Tue, 12 Apr 2005 20:13:51 -0700

>> Try this:
>>
>> on RoundOff(num, precis)
>>     set factor to 10 ^ precis
>>     return ((round(num * factor)) / factor)
>> end RoundOff
>>
>> This uses the 'round' osax instead of adding 0.5 and applying integer
>> division by 1. The asax works correctly for negative numbers as well as
>> positive numbers, rounding to the nearest integer.

>Why do you see that as an advantage, Scott? To do one or a few roundings
>makes no difference for efficiency, but to do hundreds in a repeat loop you
>would be _much_ better off using Vince's script "adding 0.5 and applying
>integer division by 1". 'round' is a scripting addition call, and will be
>close to 100 times slower than pure AppleScript. Easier to read, true, so if
>you're just doing it once or a few times, by all means use it.

As Scott pointed out, the problem with adding 0.5 is that it gives the
wrong results with negative numbers. It also rounds 0.5 up, whereas
'round' - without an appallingly named parameter - rounds it to the
nearest even. OP wasn't specific about which he wanted.

An alternative to '(n + 0.5) div 1' is 'n div 0.5 - n div 1', which works
both with positives and negatives. The following handlers use this and
contain some additional hackery to reduce the incidence of floating-point
errors:

  on roundOff(n, d) -- .5 away from zero
    set p to 10 ^ d
    set nShift to n * ((10 ^ 0.5) ^ 2) * p
    return (nShift div 5 - nShift div 10) / p
  end roundOff

  on roundOffIEEE(n, d) -- .5 to nearest even
    set p to 10 ^ d
    set nShift to n * ((10 ^ 0.5) ^ 2) * p
    if (((nShift as string) mod 20) ^ 2 > 25) then
      return (nShift div 5 - nShift div 10) / p
    else
      return nShift div 10 / p
    end if
  end roundOffIEEE

  roundOff(324.205, 2)
  --> 324.21

  roundOffIEEE(324.205, 2)
  --> 324.2

NG
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Rounding off in AppleScript
      • From: "Dennis W. Manasco" <email@hidden>
  • Prev by Date: Help......scripting for Quicktime Fullscreen mode doesn't work correctly
  • Next by Date: Re: Mail to iCal and iCal to Mail
  • Previous by thread: Re: Rounding off in AppleScript
  • Next by thread: Re: Rounding off in AppleScript
  • Index(es):
    • Date
    • Thread