2016-06-27 56 views
2

又豈是表達R的表達效果沒有明顯的理由

> (exp(17.118708 + 4.491715 * -2)/-67.421587)^(-67.421587) 

結果

[1] NaN 

> -50.61828^(-67.421587) 

應基本具備同樣的結局,給我

[1] -1.238487e-115 

這讓我瘋狂,我花了幾個小時尋找錯誤。在這種情況下,「-2」是函數的參數。我真的想不出一個解決方案。謝謝你的幫助!

編輯:

我看到,當我加括號

> (-50.61828)^(-67.421587) 

這也導致

[1] NaN 

...但是,這並沒有解決我的問題。

+0

@warmoverflow,你是對的,我抄了錯誤的代碼。我編輯了這個問題。 –

+4

[HelpLink1](http://r.789695.n4.nabble.com/8-1-3-NaN-td895638.html)[HelpLink2](http://stackoverflow.com/questions/19437701/how-to -calculate-any-negative-number-of-the-power-of-some-fraction-in-r)[HelpLink3](http://stackoverflow.com/questions/8166988/exponentiation-with-negative-base)好運氣。 – user5249203

回答

3

這是因爲C99標準下實施了pow

別說OP的例子:(-50.61828)^(-67.421587),數學上合理(-8)^(1/3) = -2沒有R中工作:

(-8)^(1/3) 
# [1] NaN 

?"^"報價:

Users are sometimes surprised by the value returned, for example 
why ‘(-8)^(1/3)’ is ‘NaN’. For double inputs, R makes use of IEC 
60559 arithmetic on all platforms, together with the C system 
function ‘pow’ for the ‘^’ operator. The relevant standards 
define the result in many corner cases. In particular, the result 
in the example above is mandated by the C99 standard. On many 
Unix-alike systems the command ‘man pow’ gives details of the 
values in a large number of corner cases. 

我在Ubuntu Linux操作系統,所以可以幫助獲得man power的相關部分印在這裏:

If x is a finite value less than 0, and y is a finite noninteger, a 
    domain error occurs, and a NaN is returned. 
+0

那麼爲什麼(-10)^( - 60)會導致1e-60? –

+0

啊,我明白了。的確很明顯。該功能根本就沒有爲特定範圍定義。 –

1

從我所知道的,-50.61828^(-67.421587)正在評估爲-(50.61828^(-67.421587))(-50.61828)^(-67.421587)也導致NaN。

+0

看到我編輯的問題,我複製了錯誤的值,但是,你是對的。但爲什麼結果仍然是NaN? –

+3

當'x'和'y'都是負數和非整數時,如何定義'x^y'? –