[Rd] documented/undocumented behavior of as.double(formatC(x, digits=17))

From: Petr Savicky <savicky_at_cs.cas.cz>
Date: Fri, 25 May 2007 17:21:54 +0200


Some days ago, there was a discussion about the command

   formatC(exp(1),digits=100,width=-1)

Converting a double value to a string, from which the double may be reconstructed exactly, may be useful. So, I did some experimentation with it in my linux installation of R-2.5.0.

I generated a vector x of a large number of random doubles (random sign, random mantissa with 53 significant bits and binary exponent from -1074:1023) and did the following

     y <- formatC(x,digits=17,width=-1)
     z <- as.double(y)
     all(x == z)  # TRUE

On Wed, May 23, 2007 at 06:32:36PM +0100, Prof Brian Ripley wrote:
> However, the C99 standard does make clear that [sf]printf is not required
> (even as 'recommended practice') to be accurate to more than *_DIG places,
> which as ?as.character has pointed out is 15 on the machines R runs on.
>
> It really is the case that writing out a number to > 15 significant digits
> and reading it back in again is not required to give exactly the same
> IEC60559 (sic) number, and furthermore there are R platforms for which
> this does not happen.

I think that this implies that preserving the exact double value in formatC does not follow from C99 standard. Is there some other (e.g. platform specific) documentation that implies this or it has to be used as an undocumented feature? (Well, I do know that the R sources or gcc sources are such a documentation, but I am trying to find some other).

Petr.



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Fri 25 May 2007 - 15:54:26 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 25 May 2007 - 23:33:59 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.