2014-07-02 41 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)

相關問題