[Rd] documented/undocumented behavior of as.double(formatC(x, digits=17))
savicky at cs.cas.cz
Fri May 25 17:21:54 CEST 2007
Some days ago, there was a discussion about the command
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).
More information about the R-devel