[Rd] Illegal Logical Values

brodie gaslam brodie.gaslam at yahoo.com
Fri Oct 20 16:01:16 CEST 2017


I'm wondering if WRE Section 5.2 should be a little more explicit about misuse of integer values other than NA, 0, and 1 in LGLSXPs.  I'm thinking of this passage:

> Logical values are sent as 0 (FALSE), 1 (TRUE) or INT_MIN = -2147483648 (NA, but only if NAOK is true), and the compiled code should return one of these three values. (Non-zero values other than INT_MIN are mapped to TRUE.) 

The parenthetical seems to suggest that something like 'LOGICAL(x)[0] = 2;' will be treated as TRUE, which it sometimes is, and sometimes isn't:

not.true <- inline::cfunction(body='
  SEXP res = allocVector(LGLSXP, 1);
  LOGICAL(res)[0] = 2;
  return res;'
)()
not.true
## [1] TRUE
not.true == TRUE
## [1] FALSE
not.true[1] == TRUE  # due to scalar subset handling
## [1] TRUE
not.true == 2L
## [1] TRUE


Perhaps a more explicit warning that using anything other than 0, 1, or NA is undefined behavior is warranted?  Obviously people should know better than to expect correct behavior, but the fact that the behavior is correct in some cases (e.g. printing, scalar subsetting) might be confusing.

This is based off of Drew Schmidt's accidental discovery yesterday: <https://gist.github.com/wrathematics/c3e5225cf56596b31e01f717e8675640>.
Best,
Brodie.

	[[alternative HTML version deleted]]



More information about the R-devel mailing list