2014-07-02 42 views
3

该手册Writing R Extensions,Sec。 6.7.3,指出声明为double R_pow (double x, double y)将R API函数计算x^yR的R_pow()和libc的pow()之间有什么区别?

[...]使用R_FINITE检查和用于箱子返回正确的结果(同R),其中xy或我是0或失踪或无限或NaN。

但是,我找不到这样xy为从C库中的函数pow()不当结果。我尝试过不同的情况,如x,y being Inf , NA / NaN , integers, and so on, but I found no input data that generated the results different than those returned by ordinary 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)。 For R_pow()的源代码搜索R_powhere

+1

在所有平台上,pow()是否给出了相同的特殊情况处理? –

+0

@DavidHeffernan:我不这么认为。这里是[glibc]的源代码(https://sourceware.org/git/?p=glibc.git;a=tree;f=math)。基本上,它是'exp(x * log(y))'或'__ieee754_powf'或者其中的任何一个+一些检查,如[https://sourceware.org/git/?p=glibc.git;a =斑点; F =数学/ w_powf.c; H = fa9e0071c82939f8e3db87251328ab00f840a672; HB = HEAD)。 – gagolews

+0

'man 3 pow'也给出了所有潜在奇点条件的详尽解释。很难找到一套不受保护的'x'或'y'。 –

回答

4

这可能只是最简单的看看我从中复制的实际功能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)

相关问题