Hi, I found in a bit of code the following test for infinity: if (x == Inf) ... Is that valid, or should it be (as I always thought): if (is.infinite(x)) ...? Does it depend on whether 'x' is float or integer? My question is related to testing for missing values where is.na(x) is required. /Henrik
Is it valid to do x == Inf?
3 messages · Henrik Bengtsson, Barry Rowlingson, Thomas Lumley
On Thu, Apr 1, 2010 at 11:03 AM, Henrik Bengtsson <hb at stat.berkeley.edu> wrote:
Hi, I found in a bit of code the following test for infinity: ?if (x == Inf) ... Is that valid, or should it be (as I always thought): ?if (is.infinite(x)) ...? Does it depend on whether 'x' is float or integer? My question is related to testing for missing values where is.na(x) is required.
Well, '-Inf' is infinite too: > is.infinite(-Inf) [1] TRUE but is not equal to Inf: > Inf == -Inf [1] FALSE Also, ?is.infinite says it is a generic method, so is.infinite(x) could be doing anything, depending on x. I would say the best way of testing if x is a numeric value of plus infinity would be to test x==Inf. Also also, is.infinite (on a numeric vector) returns FALSE on NA, and NaN, whereas x==Inf returns NA values for non nice-number inputs. Barry
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