Re: Highlights of AppleScript 1.5.5 in Mac OS 9.1
Re: Highlights of AppleScript 1.5.5 in Mac OS 9.1
- Subject: Re: Highlights of AppleScript 1.5.5 in Mac OS 9.1
- From: email@hidden
- Date: Wed, 10 Jan 2001 21:18:41 -0500
On Wed, 10 Jan 2001 10:52:35 -0500,
"Arthur J Knapp" <email@hidden> asked,
>
> Chris Espinosa wrote on Tue, 09 Jan 2001 14:43:14 -0800:
>
>
>
>>ROUND LIKE TAUGHT IN SCHOOL
>
>
>
> If it's as fast as the current 'round', it'll still be seventeen times
>
> quicker to use a handler:
>
>
>
> on rnd(n)
>
> n div 0.5 - n div 1
>
> end
>
>
>
> on rndIEEE(n)
>
> if (n mod 2) ^ 2 > 0.25 then return n div 0.5 - n div 1
>
> n div 1
>
> end rndIEEE
>
>
>
> rnd(122.5)
>
> --> 123
>
>
>
> rndIEEE(122.5)
>
> --> 122
>
Wow, these are great. I wish I could get a handle on exactly what
>
the IEEE standard is trying to accomplish, (I'm pretty thick when
>
it comes to math). :)
The idea behind the rounding rules in ANSI/IEEE Standard 754 is to make the
errors balance out to zero. Say you have a random number, equally spaced by
tenths, on an interval [a,b]. (That is, x.y, where y is one of {0, 1, 2,..., 8,
9}, round it to an integer, and then average the results, you have the following
errors (based on the digit to the right of the decimal point.
0 0
1 -1
2 -2
3 -3
4 -4
5 +5
6 +4
7 +3
8 +2
9 +1
If the tenths are uniformly distributed from .0 to .9, the expected error is
obtained by just adding the error column and multiplying by 0.1. The result is
an expected error of +0.5. So the IEEE standard uses a slightly more
complicated rule that rounds half of the .5's up and half down, producing a
average error of zero.
Note that this half up, half down rule only applies if the number is exactly
half way between the two integer values. That is, 122.5 rounds down to 122.0
and 123.5 rounds up to 124 because they are exactly half way between the two
integers, but 122.50000000000001 rounds up to 123.0
Also note that this rule applies not only to rounding to an integer, but to the
rounding that must happen every time you multiply two N-bit numbers and want an
N-bit result. (If you multiply two N-bit numbers, you get 2N significant bits
in the result.) This rounding rule is built into the floating point hardware of
the PowerPC chip. That means rounding IEEE style is much faster than rounding
"as taught in school". Including the calling overhead of the OSAX, rounding
IEEE style takes 1.0001 milliseconds, while rounding "as taught in school" takes
1.0002 milliseconds.
--
Scott Norton Phone: +1-703-299-1656
DTI Associates, Inc. Fax: +1-703-706-0476
2920 South Glebe Road Internet: email@hidden
Arlington, VA 22206-2768 or email@hidden