[R] NaN/NA and sprintf double format
Jonathan Rougier
J.C.Rougier at durham.ac.uk
Thu May 16 10:02:54 CEST 2002
ripley at stats.ox.ac.uk wrote:
>
> On Tue, 14 May 2002, Dirk Eddelbuettel wrote:
>
> > With a (late prerelease of) 1.5.0 on Windows, using sprintf on NaN
> > yields garbage when the number of decimal is specified while it
> > works fine for NA:
> >
> > > sprintf("%.2f", NA)
> > [1] "0.00"
>
> I get that on Linux too, but why is it fine? See below.
>
> > > sprintf("%.2f", NaN)
> > [1] "-1.#J"
> >
> > Not specifying the decimal precision lets both print garbage:
> >
> > > sprintf("%f", NaN)
> > [1] "-1.#IND00"
> > > sprintf("%f", NA)
> > [1] "-1.#QNAN0"
> >
> > Is this a bug or a feature?
>
> Remember that NA is logical, so this is not correct usage. Not that the
> correct usage works very much better ....
>
> The C code says
>
> /* Simple wrapper for C sprintf function: does very little checking to
> see that the format conversion character and the argument are
> compatible. This is the user's responsibility!
> */
>
> and I think that should be on the help page. If it were, this is a
> feature.
Naturally it's a feature! But I agree that an extra sentance or two on
the help page would be a good idea. I suggest appending the following
as a new paragraph to the Value section:
As \code{sprintf} is simply a wrapper for the C function of the same
name, it is the user's responsibility to ensure that the objects passed
in \code{\dots} correspond to the format. Otherwise the resulting
string may contain garbage. For example, the typical R numeric object
is a float not an integer (see Examples, below). Likewise, C does not
recognise special values like \code{NA}, so \code{sprintf("Our survey
said: %f", NA)} is not going to work as you might want.
I've cc'ed this to r-bugs. Jonathan.
--
Jonathan Rougier Science Laboratories
Department of Mathematical Sciences South Road
University of Durham Durham DH1 3LE
tel: +44 (0)191 374 2361, fax: +44 (0)191 374 7388
http://www.maths.dur.ac.uk/stats/people/jcr/jcr.html
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list