From tlumley at u.washington.edu  Thu Apr  1 18:46:51 2010
From: tlumley at u.washington.edu (Thomas Lumley)
Date: Thu, 1 Apr 2010 09:46:51 -0700 (PDT)
Subject: [Rd] Is it valid to do x == Inf?
In-Reply-To: <j2n59d7961d1004010303ja372b671r8da5fc73ca62edb7@mail.gmail.com>
Message-ID: <alpine.LRH.2.01.1004010946510.29508@hymn11.u.washington.edu>

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



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