Rounding (Was: How to coerce a real to an integer in X?)
Rounding (Was: How to coerce a real to an integer in X?)
- Subject: Rounding (Was: How to coerce a real to an integer in X?)
- From: email@hidden
- Date: Fri, 1 Feb 2002 19:51:32 -0500
Those who predicted a multi-week return of the IEEE Rounding Rules thread were,
of course, accurate. :-)
Fri, 1 Feb 2002 12:07:02 -0500, email@hidden (Michael Sullivan) asked,
>
> > (3.5)
>
> > integer result
>
> > --> 4
>
> >
>
> > (4.5)
>
> > integer result
>
> > --> 4 (???)
>
> This is the IEEE Floating Point standard's rounding rules, which are to
>
> round a value that is exactly half way between two points so that the
>
> resulting least significant digit is even. This way, half the rounding is
>
> up, and half the rounding is down, and you have no bias to the error.
>
> IEEE Floating Point is how the PowerPC chip is hardwired.
>
>
Except that's not true, unless you don't count x.0
>
>
{0,1,2,3,4} is 5 possible digits
>
>
{5,6,7,8,9} is 5 possible digits.
>
>
The only way I can make sense of it is by assuming that any digits
>
beyond the result were *rounded* rather than truncated -- so that 3.5
>
could be the result of a calculation that yielded 3.45, or one that
>
yielded 3.55. You don't know which side it was on, so it makes sense to
>
use some arbitrary known 50% method to break up 5s.
First, we need to stop the confusion that others readers may have: The
unfamiliar rule (round to odd) only applies for numbers that are exactly half
way between two integers. This means 3.5 and 4.5 both round to 4, but 4.51
rounds to 5, because 5 is closer.
Why is this statistically better? Lets assume we get data reported in terms of
tenths, and we want to round it off to integers before collecting statistics.
If the data is uniformly distributed between 0.0 and 100.0 inclusive, and we
take the average, we expect 50.0. If we average the rounded integers, we'd like
to also have 50.0 as the expected value.
In an ensemble of 1001 samples, we get the following expected distributions of
tenths digits:
xx.0 101 (because we have both 0.0 and 100.0)
xx.1 100
xx.2 100
[...]
xx.9 100
When we round we produce an error equal to the difference between the original
and rounded value. That means we get the following distribution of errors in
1001 samples.
xx.0 0 101
xx.1 1 100
xx.2 2 100
xx.3 3 100
xx.4 4 100
xx.5 5 100
xx.6 -4 100
xx.7 -3 100
xx.8 -2 100
xx.9 -1 100
If we add the product of the error and the number of times it occurs, and divide
by 1001, we get the average error.
[ 0*101 + 1*100 + 2*100 + 3*100 + 4*100
+ 5*100 + (-4)*100 + (-3)*100 + (-2)*100 + (-1)*100 ] / 1001
--> result: 0.4995
Now if we change to rule so that 5 rounds to make the result even, we get the
average,
[ 0*101 + 1*100 + 2*100 + 3*100 + 4*100
+ 5* 50 + (-5)* 50
+ (-4)*100 + (-3)*100 + (-2)*100 + (-1)*100 ] / 1001
--> result: 0.0000
>
This makes a lot of sense when interpreting analog data (such as
>
measurements) where a scale that reads tenths will probably read 3.5 for
>
an actual weight of 3.45 or 3.46.
>
>
But i thought floating point digital representations usually truncated
>
rather than rounding when reaching the end of their precision, so the
>
traditional method should be used.
Error analysis really requires a very careful examination of the way the errors
are being introduced, where rounding is occuring, and how important is the bias
of the result. But if you phrase it to a panel designing a piece of computer
hardware, the method of rounding that produces an average error of zero seems
better than a method the produces an average error equal to half the retained
digit. And all the hardware manufacturers agree that this is the way to go. Its
important to realize that every time you multiply two floating point numbers,
you get an answer with twice as many bits as you can store, so you need to round
every time.
>
Bah. Engineers.
>
>
Is there an apologia for this rounding method with FP data in the house?
>
Am I misinformed?
>
Do many standard chips round lost digits in FP
>
arithmetic?
All the chips I'm aware of round by these rules. The 68000, the PowerPC, the
Intel 80x86, and the DEC Alpha all the ANSI/IEEE Standard 754 Floating Point
standard, which has these rules, and I read that most of the RISC chips used in
Unix workstations also use IEEE 754.. The Java language specifies it as its
numerical model.
Development of the IEEE 754 standard is quite an interesting story of the best
brains in hardware and numeric analysis working together and actually doing The
Right Thing.
http://www.cs.berkeley.edu/~wkahan/ieee754status/754story.html
ACM awarded William Kahan their Turing Award in 1989 for developing the IEEE 754
standard.
--
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