Writing R Extensions手册,二段。 6.7.3.,说明声明为 double R_pow (double x, double y)
的 R API 函数计算 x^y
:
[...] using R_FINITE checks and returning the proper result (the same as R) for the cases where
x
,y
or i are 0 or missing or infinite or NaN.
但是,我找不到 C 库中的 pow()
函数给出的 x
和 y
不正确 结果。我尝试了各种情况,例如 x
、y 是
Inf、
NA/
NaN、整数和依此类推,但我没有发现生成的结果与普通
pow()`返回的结果不同的输入数据。
Rcpp::evalCpp("::pow(1.124e-15, 2)", includes = "#include <cmath>") == Rcpp::evalCpp("R_pow(1.124e-15, 2)")
## [1] TRUE
也许你们会给我提供一些不恰当的例子。
顺便说一句,我将 gcc 4.8.2 与 glibc 2.18(Fedora 20、x86_64)一起使用。
对于 R_pow()
的源代码搜索 R_pow
here .
最佳答案
查看 source code 可能是最简单的我从中复制了实际功能:
double R_pow(double x, double y) /* = x ^ y */
{
/* squaring is the most common of the specially handled cases so
check for it first. */
if(y == 2.0)
return x * x;
if(x == 1. || y == 0.)
return(1.);
if(x == 0.) {
if(y > 0.) return(0.);
else if(y < 0) return(R_PosInf);
else return(y); /* NA or NaN, we assert */
}
if (R_FINITE(x) && R_FINITE(y)) {
/* There was a special case for y == 0.5 here, but
gcc 4.3.0 -g -O2 mis-compiled it. Showed up with
100^0.5 as 3.162278, example(pbirthday) failed. */
return pow(x, y);
}
if (ISNAN(x) || ISNAN(y))
return(x + y);
if(!R_FINITE(x)) {
if(x > 0) /* Inf ^ y */
return (y < 0.)? 0. : R_PosInf;
else { /* (-Inf) ^ y */
if(R_FINITE(y) && y == floor(y)) /* (-Inf) ^ n */
return (y < 0.) ? 0. : (myfmod(y, 2.) ? x : -x);
}
}
if(!R_FINITE(y)) {
if(x >= 0) {
if(y > 0) /* y == +Inf */
return (x >= 1) ? R_PosInf : 0.;
else /* y == -Inf */
return (x < 1) ? R_PosInf : 0.;
}
}
return R_NaN; // all other cases: (-Inf)^{+-Inf, non-int}; (neg)^{+-Inf}
}
显示在哪些情况下这会折叠为 pow(x, y)
。
关于c - R的R_pow()和libc的pow()有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24540109/