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

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Mon Feb 18 18:32:46 CET 2008


> 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}}



More information about the R-help mailing list