[R] Testing ``<=" in R

Thomas Lumley tlumley at u.washington.edu
Fri Jan 31 22:52:03 CET 2003


On Fri, 31 Jan 2003, Maria N Kocherginsky wrote:

> Hello,
>
> I've encountered the following:
>
> > n_500
> > tau_.95
> > (n*(1-tau))
> [1] 25
> > (n*(1-tau))<=25
> [1] FALSE
> > (n*(1-tau))==25
> [1] FALSE
>
> I'm using UNIX R Version 1.4.0, and also tested in out in Windows 1.6.0. Is
> this a bug?
>

No.

You can't get exact floating point on a computer.
> n <- 500
> tau <- 0.95
> n*(1-tau)-25
[1] 2.1316282072803006e-14

Floating point is only accurate to about 15 decimal digits (53 binary
digits). There are only a few circumstances where it makes sense to ask if
two floating point numbers are equal.

If you care about getting n*(1-tau)==25 then either
1/ you know the numbers are integers, in which case you can round them
2/ you care about the difference between 25 and 25.00000000000002, in
which case you are in trouble


	-thomas




More information about the R-help mailing list