java 不允许使用 Math.pow 时计算指数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15310375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:11:36  来源:igfitidea点击:

Calculate exponent when not allowed to use Math.pow?

javapow

提问by user2148463

I need to write a method named pow2that accepts a real number base and an integer exponent as parameters. It should return the base raised to the given power. Your code should work for both positive and negative exponents. For example, the call pow2(2.0, -2)returns 0.25. Do not use Math.pow in your solution.

我需要编写一个名为的方法pow2,它接受一个实数基数和一个整数指数作为参数。它应该将升高的基数恢复到给定的功率。您的代码应该适用于正指数和负指数。例如,调用pow2(2.0, -2)返回0.25。不要在您的解决方案中使用 Math.pow。

This is what I have so far:

这是我到目前为止:

public?double?pow2(double?x,int?y){
    double total=1;
    for(int i=1;i<=y;i++){
        total*=x;
    }
    return total;
}

But the problem is when I try to call pow(2.0, -2), it returns me 1.0 instead. How do I implement this method?

但问题是当我尝试调用时pow(2.0, -2),它返回给我 1.0。我如何实现这个方法?

回答by Johannes Kuhn

You have to branch, depending if you have a negative or a positive value.

您必须分支,具体取决于您的值是负值还是正值。

Here a version that works with recursion:

这是一个适用于递归的版本:

public double pow2(double x,int y){
    return _pow2(1.0, x, y);
}

private double _pow2(double res, double x, int y) {
    if (y < 0) return _pow2(res/x, x, y+1);
    if (y > 0) return _pow2(res*x, x, y-1);
    return res;
}

If yis too big or too small, then you'll run into a stack overflow, so changing it to a non-recursive function is left to the op.

如果y太大或太小,那么您将遇到堆栈溢出,因此将其更改为非递归函数留给操作。

Edit: about your last question, you set the result to 1.0, the body of the loop is never used because !(1 <= -2), so you return the unmodified result of 1.0

编辑:关于您的最后一个问题,您将结果设置为1.0,循环体从未使用过,因为!(1 <= -2),因此您返回未修改的结果1.0

回答by Rob

Well, finally if you want to do it in an iterative way, just check first if yis positive or negative.

好吧,最后,如果您想以迭代方式进行操作,只需先检查y是正数还是负数。

public double pow2(double x, int y)
{
    double total = 1.0;

    if(y > 0)
    {
        for(int i = 1 ; i <= y ; i++)
        {
            total *= x;
        }
    } 
    else 
    {
        for(int i = -1 ; i >= y ; i--)
        {
            total /= x;
        }
    }
    return total;
}

回答by hakiko

public static void main(String[] args) {

    System.out.println(pow2(2,3));

}

public static double pow2(double x,int y){
    double total=1;
    for(int i=1;i<=y;i++){
        total*=x;
    }
    return total ;
}

回答by Balaji Krishnan

public static double pow2(double x,int y){
    double total=1;
    if(y>0){
    for(int i=1;i<=y;i++){
        total*=x;
    }
    return total;
    }
    else if (y<0){
        double temp=1/x;//this makes 2 as 1/2
        y=y*-1;         //to have a meaningful iteration if for loop
        for(int i=1;i<=y;i++){
            total*=temp;
        }   
        return total;
    }else
        return 1;
}