c - R的R_pow()和libc的pow()有什么区别?

标签 c r glibc rcpp pow

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() 函数给出的 xy 不正确 结果。我尝试了各种情况,例如 xy 是InfNA/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/

相关文章:

R图表转换为html格式,无需其他文件

c - C标准库中的函数线程安全吗?

c - 使用 float 变量和 scanf() 获取用户成绩并 printf() 进行调试断言失败

c - write() 与 ttyS 是非阻塞的

r - 如何使用 plotly 在 R 中将每个带有数据子集的多条轨迹/趋势线添加到单个散点图

linux - 备用 glibc 动态链接器 (ld.so) 的库路径顺序

linux - 在/dev/shm 的子目录中创建共享内存时,shm_open() 失败并返回 EINVAL

c - C中如何检查用户输入是否不是int值

c - 使用 void* 参数传递整数或指针

r - 在 Shiny 的 actionButton 中复制图标