Description Arguments Value Declaration Definition Examples
Converts a scalar (length one) double
vector
(SEXP
) x
to a C double
.
x |
a pointer |
A C double
value.
1 |
In Rinternals.h.
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.
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))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.