Hi the list, I am writing a R function that call a C function. The C function needs integers but I do not manage to give a NA integer as argument : --- C code --- void essai(int *t){ Rprintf("\nT0=%i T1=%i T2=%i T3=%i",t[0],t[1],t[2],t[3]); } --- R --- boub <- c(1,2,3,4) .C("pour",as.integer(boub),NAOK=TRUE) # T0=1 T1=2 T2=3 T3=4[[1]] # [1] 1 2 3 4 boub <- c(1,2,NA,4) .C("essai",as.integer(boub),NAOK=TRUE) # T0=1 T1=2 T2=-2147483648 T3=4[[1]] # [1] 1 2 NA 4 --- --- In the second example, T2=-2147483648 and not NA. I check the "writing R extension", there is a part that explain that the test of NA is not the same between double and integer (NA_INTEGER, ISNA), but I did not find explanation on passing NA argument as integer. Any idea of what is wrong in my code? Christophe
On Jun 18, 2009, at 9:57 , Christophe Genolini wrote:> Hi the list, > I am writing a R function that call a C function. The C function > needs integers but I do not manage to give a NA integer as argument : > > --- C code --- > void essai(int *t){ > Rprintf("\nT0=%i T1=%i T2=%i T3=%i",t[0],t[1],t[2],t[3]); > } > > --- R --- > boub <- c(1,2,3,4) > .C("pour",as.integer(boub),NAOK=TRUE) > > # T0=1 T1=2 T2=3 T3=4[[1]] > # [1] 1 2 3 4 > > boub <- c(1,2,NA,4) > .C("essai",as.integer(boub),NAOK=TRUE) > > # T0=1 T1=2 T2=-2147483648 T3=4[[1]] > # [1] 1 2 NA 4 > --- --- > > In the second example, T2=-2147483648 and not NA. > > I check the "writing R extension", there is a part that explain that > the test of NA is not the same between double and integer > (NA_INTEGER, ISNA), but I did not find explanation on passing NA > argument as integer. > > Any idea of what is wrong in my code? >I don't see any problem - in C there is no inherent NA, so what you get is NA_INTEGER value which prints as -2147483648 when you print it as integer (which is what you do in essai). Cheers, S
On Thu, 18 Jun 2009, Christophe Genolini wrote:> Hi the list, > I am writing a R function that call a C function. The C function needs > integers but I do not manage to give a NA integer as argument : > > --- C code --- > void essai(int *t){ > Rprintf("\nT0=%i T1=%i T2=%i T3=%i",t[0],t[1],t[2],t[3]); > } > > --- R --- > boub <- c(1,2,3,4) > .C("pour",as.integer(boub),NAOK=TRUE) > > # T0=1 T1=2 T2=3 T3=4[[1]] > # [1] 1 2 3 4 > > boub <- c(1,2,NA,4) > .C("essai",as.integer(boub),NAOK=TRUE) > > # T0=1 T1=2 T2=-2147483648 T3=4[[1]] > # [1] 1 2 NA 4 > --- --- > > In the second example, T2=-2147483648 and not NA. > > I check the "writing R extension", there is a part that explain that the test > of NA is not the same between double and integer (NA_INTEGER, ISNA), but I > did not find explanation on passing NA argument as integer. > > Any idea of what is wrong in my code?Simple: Rprintf does not know about NAs (and nor does printf). From the manual: The most useful function for printing from a C routine compiled into R is Rprintf. This is used in exactly the same way as printf, but is guaranteed to write to R's output (which might be a GUI console rather than a file). The value of NA is stored as NA_INTEGER = -2^32, and if you want your C code to be aware of it, *you* need to program so that value is treated specially. (Since double NAs are stored as a particular NaN, the default C handling of doubles will probably do something sensible but careful code will also need to take the difference between NaNs into account.) -- 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