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

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Mon Feb 18 14:21:16 CET 2008


> > What I mean is if R shows 2.3456 I want to obtain the info that 
digits=4 
> 
> > even if in facts the value has more (internal) digits.
> 
> Try:
> x = 1.23456789
> format(x, nsmall=20)
> # [1] "1.23456788999999989009"

I've just re-read the question.  I suspect what you really wanted was 
something like:

getndp <- function(x, tol=2*.Machine$double.eps)
{
  ndp <- 0
  while(!isTRUE(all.equal(x, round(x, ndp), tol=tol))) ndp <- ndp+1 
  if(ndp > -log10(tol)) warning("Tolerance reached, ndp possibly 
underestimated.")
  ndp 
}

x = 0.123456
getndp(x)
# [1] 6

x2 <- 3
getndp(x2)
# [1] 0 

x3 <- 0.1234567890123456789
getndp(x3)
# [1] 16
# Warning message:
# In getndp(x3) : Tolerance reached, ndp possibly underestimated.

Regards,
Richie.

Mathematical Sciences Unit
HSL


------------------------------------------------------------------------
ATTENTION:

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



More information about the R-help mailing list