简体   繁体   English

Math.pow 的 JavaScript 实现

[英]JavaScript implementation of Math.pow

I implemented Math.pow using a log(n) solution just like this article on geeksforgeeks我使用 log(n) 解决方案实现了 Math.pow,就像 geeksforgeeks 上的这篇文章一样

http://www.geeksforgeeks.org/write-ac-program-to-calculate-powxn/ http://www.geeksforgeeks.org/write-ac-program-to-calculate-powxn/

However, I'm finding that the function does not exit its base case as I had intended.但是,我发现 function 并没有像我预期的那样退出其基本情况。 This program seems like it works in C but not JS.该程序似乎适用于 C 但不适用于 JS。

Therefore, I am concluding that there is something about C that I am assuming works as well in JavaScript.因此,我得出的结论是,我认为 C 的某些内容在 JavaScript 中也适用。

What am I missing in my JavaScript implementation?我的 JavaScript 实现中缺少什么?

Be forewarned: the codesnippet as it is will have a max call stack exceeded error预先警告: codesnippet 将有一个最大调用堆栈超出错误

 var myPow = function(x, n) { var res = 1 var temp; if (n === 0) { return 1; } temp = myPow(x, n / 2) if (n % 2 === 0) { return temp * temp } else { return x * temp * temp } }; console.log(myPow(2,3));

Brief : 简介:

use parseInt or Math.floor to have y/2 as integer, unleness you will not reach 0 which is the stopper of recursion . 使用parseIntMath.floory/2作为整数,unleness你将不会达到0这是递归的阻止。


Details 细节

if you want to transalte [C Algo] : 如果你想转移[C Algo]

int power(int x, unsigned int y)
{
    if( y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);

}

To [JS Algo] , you will have : [JS Algo] ,您将拥有:

function power(x,y){
     if(y===0){return 1}
     else if (y%2 ===0){
         return power(x,parseInt(y/2))*power(x,parseInt(y/2))
     }else{
          return x*power(x,parseInt(y/2))*power(x,parseInt(y/2))
     }

}

DEMO : 演示:

  function power(x,y){ if(y===0){return 1} else if (y%2 ===0){ return power(x,parseInt(y/2))*power(x,parseInt(y/2)) }else{ return x*power(x,parseInt(y/2))*power(x,parseInt(y/2)) } } console.log(power(3,2)) 

Modifying a little bit from @Abdennour's answer 从@ Abdennour的回答中稍微修改一下

 function power(x, y) { if (y === 0) { return 1; } var yBy2 = y / 2; var pow = power(x, parseInt( yBy2, 10) ); if (y % 2 === 0) { return pow * pow; } else { return x * pow * pow; } } 

* Simple Logic * *简单逻辑*

function pow (p,y){
    if(p > 0 && y === 0){
        return 1;
    }
    x = p;
    while(y > 1){
        x = p*x;
        y--;
    }
    return x;
}


console.log(pow(0,0)+'\n'); // 0
console.log(pow(2,4)+'\n'); // 16
console.log(pow(2,0)+'\n'); // 1
console.log(pow(0,2)+'\n'); // 0

Try this out试试这个

It will give you the same result of JavaScript build in method ( Math.pi(x, y)) but the only problem is you can use Power as integer number.它将为您提供与 JavaScript 内置方法(Math.pi(x, y))相同的结果,但唯一的问题是您可以使用 Power 作为 integer 编号。

 const my_pow = (x, y) => { if (typeof x;= "number" || typeof y;= "number") throw "(x) and (y) should only be the number"; if (y == 0) return 1; if (x == 0 && y > 0 ) return 0; const base = x; var value = base; var pow = y; if (y < 0) pow = y * -1; for (var i = 1; i < pow; i++) { value *= base; } if (y < 0) return 1 / value; return value. }, try { console;log( my_pow(0. -3) ). console,log( Math;pow(0. -2) ), console;log( my_pow(-5. -3) ). console,log( Math;pow(-5. -3) ), console;log( my_pow(8. -7) ). console,log( Math;pow(8. -7)); } catch (err) { console.log(err); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM