From ripley at stats.ox.ac.uk  Mon May 15 17:43:24 2006
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Mon, 15 May 2006 16:43:24 +0100 (BST)
Subject: [Rd] bindingIsLocked returns illogical logical
In-Reply-To: <m2ves7ia6j.fsf@ziti.local>
References: <6phody0ct9e.fsf@gopher3.fhcrc.org> <m2lkt3n64e.fsf@ziti.local>
	<Pine.LNX.4.64.0605150803070.8993@gannet.stats.ox.ac.uk>
	<m2ves7ia6j.fsf@ziti.local>
Message-ID: <Pine.LNX.4.64.0605151639280.29105@gannet.stats.ox.ac.uk>

On Mon, 15 May 2006, Seth Falcon wrote:

> Prof Brian Ripley <ripley at stats.ox.ac.uk> writes:
>
>> On Sun, 14 May 2006, Seth Falcon wrote:
>>> I wonder if ScalarLogical should force 0/1:
>>
>> Not a good idea: what about the third value, NA?
>
> Indeed, I should have been sleeping (or perhaps I was already :-)
>
> Is it enough to check for NA_LOGICAL or should all NA_* values be
> checked for?  Is there an easier/better way to do this sort of check?

NA_LOGICAL and NA_INTEGER are the same value, and only NA_LOGICAL should 
occur here.  I used

/* As from R 2.4.0 we check that the value is allowed. */
INLINE_FUN SEXP ScalarLogical(int x)
{
     SEXP ans = allocVector(LGLSXP, 1);
     if (x == NA_LOGICAL) INTEGER(ans)[0] = NA_LOGICAL;
     else INTEGER(ans)[0] = (x != 0);
     return ans;
}

mainly because its intentions are crystal clear.

>
> Index: Rinlinedfuns.h
> ===================================================================
> --- Rinlinedfuns.h      (revision 38060)
> +++ Rinlinedfuns.h      (working copy)
> @@ -494,6 +494,8 @@
> INLINE_FUN SEXP ScalarLogical(int x)
> {
>     SEXP ans = allocVector(LGLSXP, 1);
> +    if ((x != NA_LOGICAL) && (x != 0))
> +        x = 1;
>     INTEGER(ans)[0] = x;
>     return ans;
> }
>
> Perhaps for completeness, even though at present they are identical,
> NA_INTEGER should be in the checking...
>
> INLINE_FUN SEXP ScalarLogical(int x)
> {
>    SEXP ans = allocVector(LGLSXP, 1);
>    if ((x == NA_LOGICAL) || (x == NA_INTEGER))
>        x = NA_LOGICAL;
>    else if (x != 0)
>        x = 1;
>     INTEGER(ans)[0] = x;
>     return ans;
> }
>
> Or perhaps there is a better solution entirely.
>
> + seth
>
>
>
>>
>>> Index: include/Rinlinedfuns.h
>>> ===================================================================
>>> --- include/Rinlinedfuns.h      (revision 38060)
>>> +++ include/Rinlinedfuns.h      (working copy)
>>> @@ -494,7 +494,7 @@
>>> INLINE_FUN SEXP ScalarLogical(int x)
>>> {
>>>     SEXP ans = allocVector(LGLSXP, 1);
>>> -    INTEGER(ans)[0] = x;
>>> +    INTEGER(ans)[0] = (x == 0) ? 0 : 1;
>>>     return ans;
>>> }
>>>
>>> Otherwise, I think do_bndIsLocked needs to make a similar operation
>>> before calling ScalarLogical.  But preventing these all purpose
>>> logicals from escaping would seem to be a good argument for changing
>>> ScalarLogical.
>>
>> I think it needs to handle NAs.
>>
>> --
>> Brian D. Ripley,                  ripley at stats.ox.ac.uk
>> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
>> University of Oxford,             Tel:  +44 1865 272861 (self)
>> 1 South Parks Road,                     +44 1865 272866 (PA)
>> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] bindingIsLocked returns illogical logical
Prof Brian Ripley