[R] as.character on OS X tiger

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Sep 23 09:39:52 CEST 2005


This is just a question of rounding error.  You are computing 11/999, 
which is not exactly representable.  Most computers use IEC60559 
arithmetic, which gives a precision of .Machine$double.eps ~ 2e-16.  So 
you can expect rounding error around 11/999 * .Machine$double.eps ~ 2e-18. 
However, as.character is documented to represent the number to 15 
significant digits.

On all of Linux (i686 and AMD64), Solaris and Windows boxes I get

> a <- 11/999
> b <- as.numeric(as.character(a))
> b
[1] 0.01101101
> a - b
[1] 1.040834e-17
> as.character(a)
[1] "0.0110110110110110"
> as.character(b)
[1] "0.011011011011011"

and these are different representations of different numbers.

So your claim to get different results on `linux' is not one I can 
reproduce.

The fix is not to rely on the fine details of numerical (or character) 
representations of numbers: perhaps convert them back to numbers and use 
all.equal()?


On Fri, 23 Sep 2005, Marcus Davy wrote:

>
> Hi,
> When I run this example code on a G5 the last digit is dropped off on a
> binary installation of R (R.app), whereas on a linux machine the digit is
> not removed.
>
> # Code:
> a <- seq(0,1, length=1000)[12]
> a
>
> as.character(a)
> as.character(as.numeric(as.character(a)))
>
>
> # Results I get:
> a <- seq(0,1, length=1000)[12]
>> a
> [1] 0.01101101
>>
>> as.character(a)
> [1] "0.0110110110110110"
>> as.character(as.numeric(as.character(a)))
> [1] "0.011011011011011"
>
> Can anyone enlighten me on the subtle difference between OS's, its causing
> issues for me when I write to list elements with these characters strings
> and some occasionally have lost the last "0".

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list