Rf_asReal: Convert an R scalar double vector to a C double

Description Arguments Value Declaration Definition Examples

Description

\Sexpr[results=rd, stage=render]{c3po:::badge('fnp')} \Sexpr[results=rd, stage=render]{c3po:::badge('r2c')}

Converts a scalar (length one) double vector (SEXP) x to a C double.

Arguments

x

a pointer SEXP, referring to an object of type REALSXP.

Value

A C double value.

Declaration

1

In Rinternals.h.

Definition

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
double asReal(SEXP x)
{
 int warn = 0;
 double res;

 if (isVectorAtomic(x) && XLENGTH(x) >= 1) {
   switch (TYPEOF(x)) {
     case LGLSXP:
         res = RealFromLogical(LOGICAL_ELT(x, 0), &warn);
         CoercionWarning(warn);
         return res;
     case INTSXP:
         res = RealFromInteger(INTEGER_ELT(x, 0), &warn);
         CoercionWarning(warn);
         return res;
     case REALSXP:
         return REAL_ELT(x, 0);
     case CPLXSXP:
         res = RealFromComplex(COMPLEX_ELT(x, 0), &warn);
         CoercionWarning(warn);
         return res;
     case STRSXP:
         res = RealFromString(STRING_ELT(x, 0), &warn);
         CoercionWarning(warn);
         return res;
     default:
         UNIMPLEMENTED_TYPE("asReal", x);
   }
 } else if(TYPEOF(x) == CHARSXP) {
   res = RealFromString(x, &warn);
   CoercionWarning(warn);
   return res;
 }
 return NA_REAL;
}

In coerce.c.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Convert an R length one integer vector to a C double number
# Please note that the double backslash in "\n" in Rprintf is only required
# because of the inlining of the code here.
rdouble_to_cdouble <- inline::cfunction(c(x = "double"),
 ' double x_;
   x_ = Rf_asReal(x);
   Rprintf("x_ is %f\\n", x_);
   return R_NilValue;
 ')
invisible(rdouble_to_cdouble(NA_real_))
invisible(rdouble_to_cdouble(NaN))
invisible(rdouble_to_cdouble(-1))
invisible(rdouble_to_cdouble(0))
invisible(rdouble_to_cdouble(1))
invisible(rdouble_to_cdouble(Inf))

ramiromagno/c3po documentation built on Jan. 5, 2021, 8:01 p.m.