2013-04-10 93 views
0

我想將一個浮點數作爲第二個參數(指數)傳遞給Math.pow()。我總是得到NaN,當通過的號碼不是整數,就像0,2,7,你的名字。有沒有在JavaScript的工作方式,使這項工作?Math.pow()作爲指數的浮點數

(function() { 
    var notice = document.getElementById('notice'); 
    var value = 0.0; 
    var interval = 0.02; 
    var timeInterval = 10; 
    function interpolation(x) { 
     var y = Math.pow(Math.e, x);  // <<<HERE>>> 
     console.log(x, y); 
     return y; 
    } 
    function animation() { 
     var callAgain = true; 
     if (value >= 1) { 
      value = 1.0; 
      callAgain = false; 
     } 
     notice.style['opacity'] = interpolation(value); 
     notice.style['marginTop'] = (value * 20 + 20) + 'px'; 
     value += interval; 
     if (callAgain) { 
      setTimeout(animation, timeInterval); 
     } 
    } 
    animation(); 
})(); 

PS:請不要評論,即大於1的透明度沒有任何意義。我知道e^x; x > 0將產生大於1的值。當我得到這個工作時,我會插入一個適當的功能。

+2

'Math.pow(2,4.5);'(例如)在鉻爲我工作。 – 2013-04-10 14:35:33

回答

6

常數是Math.E,不Math.e

+0

而'Math.exp(x)'是計算這個值的更好方法。 – 2013-04-10 14:51:32

+0

哦,我的..謝謝! ;) – 2013-04-10 14:53:26