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

From: <Richard.Cotton_at_hsl.gov.uk>
Date: Mon, 18 Feb 2008 12:57:19 +0000


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

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 - 12:59:22 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 - 14:00: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