[R] Number of digits of a value for problem 7.31 RFAQ
Matthieu Stigler
stigler3 at etu.unige.ch
Mon Feb 18 13:17:12 CET 2008
Hello dear R users!
I did not find a function which gives information about the number of
digits of a value shown by R.
Do you know one?
I need it to solve the problem (see RFAQ 7.31)that 0.2==0.2+0.1-0.1 FALSE
The solution suggested in RFAQ is to use isTRUE(all.equal(0.2,0.2+0.1-0.1))
But if I want to compare inequality:
0.2<=0.2 +0.1-0.1 TRUE
but 0.2<=0.2 +0.1-0.1 FALSE
bad!
but in this case all.equal does not work I think... Unless to write a
double condition like
0.2>0.2 +0.1-0.1 | isTRUE(all.equal(0.2,0.2 +0.1-0.1))
The solution I found is to round the values, because
0.2==round(0.2+0.1-0.1,2) TRUE
However, one has to know the number of digits of the target value. How
can you do when it is unknown?
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.
I did not find a way to represent the number of digits but I found that
as.numeric(as.character(x)) rounds the values to the number of digits
shown. But is there a better way to make it?
Thanks!
More information about the R-help
mailing list