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

Marc Schwartz MSchwartz at mn.rr.com
Wed Jun 1 01:22:58 CEST 2005


On Tue, 2005-05-31 at 11:11 -0400, Duncan Murdoch wrote:
> 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

Duncan,

Just got back from a day long meeting.

You are indeed correct on the rounding here. If you look at how 18.15
appears when printed with more significant digits:

> print(18.15, 20)
[1] 18.149999999999998579

That's what I get for trying to deal with floating point representation
issues first thing after a three day weekend...  ;-)

Thanks for the correction.

Marc




More information about the R-help mailing list