[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 13:57:19 CET 2008
> 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?
You can do this by using all.equal to check for cases close to the
boundary:
if(isTRUE(all.equal(0.2, 0.2 + 0.1 - 0.1)))
{
message("Possible rounding problems.")
#investigate further
} else if(0.2 <= 0.2 + 0.1 - 0.1)
{
#your code here
}
all.equal has a tolerance parameter that you can set to see how strict you
want the equality to be; you may want to make this value smaller.
> 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"
Regards,
Richie.
Mathematical Sciences Unit
HSL
------------------------------------------------------------------------
ATTENTION:
This message contains privileged and confidential inform...{{dropped:20}}
More information about the R-help
mailing list