[Rd] Is it valid to do x == Inf?

Thomas Lumley tlumley at u.washington.edu
Thu Apr 1 18:46:51 CEST 2010


On Thu, 1 Apr 2010, Henrik Bengtsson wrote:

> Hi,
>
> I found in a bit of code the following test for infinity:
>
>  if (x == Inf) ...
>
> Is that valid

Yes, if you don't want to also include -Inf

>, or should it be (as I always thought):
>
>  if (is.infinite(x)) ...?

If you don't want to distinguish Inf and -Inf

> Does it depend on whether 'x' is float or integer?

There isn't an integer infinity. Integer values larger than the maximum reprensentable give NA
eg > .Machine$integer.max+1L
[1] NA

> My question is related to testing for missing values where is.na(x) is required.

NA is different, because NA by its nature can't compare equal to anything: x==NA asks: "Is x equal to some number I don't know?", to which the answer is "Don't know".

x==Inf asks "Is x positive infinite?", which is a perfectly well-defined question.

     -thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle



More information about the R-devel mailing list