Re: [R] Number of digits of a value for problem 7.31 RFAQ SOLVED not really

From: <Richard.Cotton_at_hsl.gov.uk>
Date: Mon, 18 Feb 2008 17:32:46 +0000


> Actually (after some trials) there is a little problem when faced with
> zeros...
>
> >getndp(1.0)
> [1] 0

Are you sure this isn't what you want? 1.0 is just 1 in disguise, and round(1.0, 0) is the same as round(1.0, 1) anyway.

> Note that I thought on a very different way which was starting from the
> point that as.character seems to work:
>
> a<-as.character(1.23134)
> b<-strsplit(a,"\\.")
>
> nchar(b)

I think the last line should be:

nchar(b[[1]][2])

If you choose this route, be careful with R clipping your values. e.g.
 x <- 0.12345678901234567890123456789

format(x, nsmall=20)
[1] "0.12345678901234567737" #this is what is stored (actually some binary number, this is the closest decimal representation)

a <- as.character(x); a
[1] "0.123456789012346" #

b<-strsplit(a,"\\.")
nchar(b[[1]][2])
[1] 15

Regards,
Richie.

Mathematical Sciences Unit
HSL



ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 18 Feb 2008 - 17:35:36 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 Mon 18 Feb 2008 - 18:30:15 GMT.

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

list of date sections of archive