Hi Jerry

I like your round function.  Maybe one more small refinement?  The way I
learned to round is to round to nearest (as your function does), except
where the least significant digit is 5.  In that case, one should round
even.  For example:

1.4 rounds to 1
1.6 rounds to 2
1.5 rounds to 2
2.5 rounds to 2
3.5 rounds to 4

This extra step helps avoid bias when rounding a sequence of numbers which
end in the digit 5.  Otherwise the sum of the rounded sequence is not equal
to the sum of the unrounded sequence.  Another way of avoiding bias is to
randomly round up or down when the last digit is 5.

Larry


----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, November 22, 1999 1:15 PM
Subject: [REBOL] Round function Re:(3)


> Donald,
>
> Try the script below. It rounds to n digits after the decimal point. n may
> even be negative. Use do read %filename to load the script.
>
> Jerry
>
>
> round: func [x[integer! decimal!] n[integer!]]
> [
>    either x >= 0
>    [
>       x: (x * (10 ** n)) + 0.5
>       x: x - (x // 1)
>       x: x * (10 ** - n)
>    ]
>    [x: - round - x n]
> ]

Reply via email to