Re: [R] Formatting numbers with a limited amount of digits consistently

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Wed 01 Jun 2005 - 01:11:55 EST

Marc Schwartz wrote:

> Final note to Henrik: Note that the IEEE 754 rounding standard as
> implemented in R results in:
>
>

>>round(18.15, 1)

>
> [1] 18.1
>
>>formatC(18.15, format = "f", digits = 1)

>
> [1] "18.1"
>
>>sprintf("%5.1f", 18.15)

>
> [1] " 18.1"
>
> This is because the rounding method implemented is the "go to the even
> digit" approach. Thus, you don't get 18.2.
>
> See ?round for more information.

I don't think "go to the even digit" is being applied here: ".1" is not   an even digit.

I suspect what's going on in this example is that 18.15 is not being represented exactly; it's stored internally as something slightly less than that value, so it rounds down.

You'd see the "go to the even digit" rule applied when rounding 17.5 or 18.5, which can be represented exactly, being fractions with a power of 2 in the denominator:

 > round(18.5, 0)
[1] 18
 > round(17.5, 0)
[1] 18

(This is very gratifying. Usually when I try to predict the exact behaviour of round() or signif() I end up having to rewrite my prediction afterwards. But this time I got it right. Honest!)

Duncan Murdoch



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Jun 01 01:18:20 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:17 EST