简体   繁体   中英

Why does NA_real_ <= Inf return NA?

I am surprised by some of the values returned when comparing Inf and -Inf with NA_real_ .

Specifically:

NA_real_ <= Inf
#[1] NA
-Inf <= NA_real_
#[1] NA

The first sentence of the documentation for NA ( help(NA) ) is

NA is a logical constant of length 1 which contains a missing value indicator.

I suppose there must be some values which are not less than or equal to Inf and some values that are not greater than or equal to -Inf . What are those values? How do you represent them in R? Please provide a complete list.

I am somewhat familiar with NaN 's, but don't these arise out of computations that produce results that are simply out of the range of proper values that a double can store? I don't know what Inf - Inf is, exactly, but it can't be greater than Inf . What's bigger than Inf in the IEEE standard that double is supposed to implement? Does that standard simply define that any operation involving NaN returns NaN ? Then why doesn't R return NaN ? NA is not an IEEE 754 value, is it? Maybe I am misreading the documentation ?

The places to point you to are:

?Arithmetic
?Inf
?`>`

The first indicates that IEEE 754 is generally used:

R is dependent on OS services (and they on FPUs) for floating-point arithmetic. On all current R platforms IEC 60559 (also known as IEEE 754) arithmetic is used, but some things in those standards are optional. In particular, the support for denormal numbers (those outside the range given by .Machine) may differ between platforms and even between calculations on a single platform.

And the second indicates that arithmetic with infinity should "work":

In R, basically all mathematical functions (including basic Arithmetic), are supposed to work properly with +/- Inf and NaN as input or output.

However, the third indicates that any logical comparison involving NA returns NA :

Missing values (NA) and NaN values are regarded as non-comparable even to themselves, so comparisons involving them will always result in NA.

So the issue is not that something is "larger" than Inf , but rather than R returns a missing value when you call a comparison with a missing value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM