Skip to content

Inconsistencies (PR#550)

3 messages · Berwin A Turlach, Brian Ripley

#
Dear all,

I was playing around with some C-code that I dynamically linked to R
and noticed the following inconsistency:
[1] TRUE
[1] FALSE
[1] FALSE
[1] TRUE

How did I find out?  I passed `dat' to a double * in the C-code and
printed out the matrix from C.  But in the .C() call I just passed
down `dat' and forgot to coerce to double via `as.double(dat)'.  Of
course the print out from the C routine was garbage, but since I used
        dat <- matrix(1,3,3)
first, I was confused.  Note that R says that `dat' is.double!  But it
seems to pass dat to C as an integer matrix nevertheless!  Using the
second matrix it became clear to me why I saw garbage being printed
out from the C-routine.  My question is now why does the first
`is.double(dat)' returns `TRUE'?

Cheers,

        Berwin


--please do not edit the information below--

Version:
 platform = i686-unknown-linux
 arch = i686
 os = linux
 system = i686, linux
 status = 
 major = 1
 minor = 0.1
 year = 2000
 month = April
 day = 14
 language = R

Search Path:
 .GlobalEnv, Autoloads, package:base

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Mon, 22 May 2000 berwin@maths.uwa.edu.au wrote:

            
Because it is double!   This refers to the storage mode, not the
mathematical properties of the object's content.

I can find no inconsistency.  1 is double, and 1:9 is integer (just as
in S-PLUS 3.x).  Why should this not be the case?  Currently the
only time in R you are likely to get an integer result is a : or seq
expression, but you are not supposed to rely on this.  (Since I know
you have a copy, this is exactly the problem of the wam example
on page 203 of `S Prograaming'.)

For example, if printit.c is

void printit(double *x, int *n)
{
    int i;
    
    for(i = 0; i < *n; i++) printf("[%d] %f\n", i, x[i]);
}
[1] TRUE
[0] 1.000000
[1] 1.000000
[2] 1.000000
[3] 1.000000
[4] 1.000000
[5] 1.000000
[6] 1.000000
[7] 1.000000
[8] 1.000000
[0] 0.000000
[1] 0.000000
[2] 0.000000
[3] 0.000000
[4] 0.000000
[5] 0.000000
[6] 0.000000
[7] 0.000000
[8] 0.000000

as I would expect.

I think the moral is that you cannot assume the storage mode of
expressions returned by R (or by S).
#

        
BDR> On Mon, 22 May 2000 berwin@maths.uwa.edu.au wrote:
>> Dear all,
  >> 
  >> I was playing around with some C-code that I dynamically linked to R
  >> and noticed the following inconsistency:

  BDR> I can find no inconsistency.  [...]
  BDR> For example, if printit.c is

  BDR> void printit(double *x, int *n)
  BDR> {
  BDR>     int i;
    
  BDR>     for(i = 0; i < *n; i++) printf("[%d] %f\n", i, x[i]);
  BDR> }

  >> dat <- matrix(1,3,3)
  >> is.double(dat)
  BDR> [1] TRUE
  >> invisible(.C("printit", dat, as.integer(9)))
  BDR> [0] 1.000000 [...]
Sorry, mea culpa.  My C-code was a bit more complicated and I know
realise that I had screwed up within the C-routine.  By the time the
C-code was fixed, I had an `as.double(dat)' in my call to .C but was
still under the impression that the problems where due to the fact
that R past a matrix that it claimed to be in storage mode double as
an integer matrix to the C-routine....  

  BDR> I think the moral is that you cannot assume the storage mode of
  BDR> expressions returned by R (or by S).
I guess the moral is rather that I should believe that I found a bug
in R when I am fooling around with some code around midnight ;-/  Or
that I should check better for the sources of my alleged bugs.....

Cheers,

        Berwin
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._