计算任何指数的幂(负或正)

发布于 2024-10-06 05:10:04 字数 406 浏览 3 评论 0原文

我想计算结果,给定任何指数(负或正)和整数类型的基数。我正在使用递归:

public static double hoch(double basis, int exponent) {
    if (exponent > 0) {
        return (basis * hoch(basis, exponent - 1));
    } else if (exponent < 0) {
        return ((1 / (basis * hoch(basis, exponent + 1))));
    } else {
        return 1;
    }
}

如果指数为负数,则返回 1.0,但这是错误的。对于例如 hoch(2,-2),它应该是 0.25。有什么想法可能是错的吗?

I want to calculate the result, given any exponent (negative or positive) and a base of type integer. I am using recursion:

public static double hoch(double basis, int exponent) {
    if (exponent > 0) {
        return (basis * hoch(basis, exponent - 1));
    } else if (exponent < 0) {
        return ((1 / (basis * hoch(basis, exponent + 1))));
    } else {
        return 1;
    }
}

If exponent is negative 1.0 is returned but that is wrong. For e.g. hoch(2,-2) it should be 0.25. Any ideas what could be wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

很酷不放纵 2024-10-13 05:10:04
 }else if(exponent < 0){
         return ((1/(basis*hoch(basis, exponent+1))))

应该是

 }else if(exponent < 0){
        return (1/hoch(basis, -exponent));
 }else if(exponent < 0){
         return ((1/(basis*hoch(basis, exponent+1))))

should be

 }else if(exponent < 0){
        return (1/hoch(basis, -exponent));
一抹微笑 2024-10-13 05:10:04
public static double hoch(double basis, int exponent){
    if(exponent > 0){
        return basis*hoch(basis, exponent-1);
    }else if(exponent < 0){
        return hoch(basis, exponent+1)/basis;
    }else{
        return 1;
    }
}

尽管更有效的(递归)解决方案是

public static double hoch(double basis, int exponent){
    if(exponent == 0)
        return 1;
    else{
        double r = hoch(basis, exponent/2);
        if(exponent % 2 < 0)
            return r * r / basis;
        else if(exponent % 2 > 0)
            return r * r * basis;
        else
            return r * r;
    }
}
public static double hoch(double basis, int exponent){
    if(exponent > 0){
        return basis*hoch(basis, exponent-1);
    }else if(exponent < 0){
        return hoch(basis, exponent+1)/basis;
    }else{
        return 1;
    }
}

although the more efficient (recursive) solution is

public static double hoch(double basis, int exponent){
    if(exponent == 0)
        return 1;
    else{
        double r = hoch(basis, exponent/2);
        if(exponent % 2 < 0)
            return r * r / basis;
        else if(exponent % 2 > 0)
            return r * r * basis;
        else
            return r * r;
    }
}
静待花开 2024-10-13 05:10:04

你的括号是错误的方式。您想要乘以递归调用的结果,而不是除以它;并且您希望乘以 1/basis (“剥离”一个负指数)。

Your parentheses are the wrong way around. You want to be multiplying by the result of the recursive call, not dividing by it; and you want the thing you multiply by to be 1/basis (which "peels off" one negative exponent).

猫弦 2024-10-13 05:10:04

使用 hoch(2,-2) 你实际上计算

     1 / (-2 * (1 / (-1 * (1 / 1)))
<=>  1 / (-2 * (1 / (-1))
<=>  1 / (-2 * -1)
<=>  1/2

With hoch(2,-2) you actually calculate

     1 / (-2 * (1 / (-1 * (1 / 1)))
<=>  1 / (-2 * (1 / (-1))
<=>  1 / (-2 * -1)
<=>  1/2
人生百味 2024-10-13 05:10:04

将 BASE 提升为正或负 BASE 的工作代码:

FUNC Raise_To_Power

LPARAMETERS pnBase, pnPow

DO CASE

  CASE pnPow = 0
    RETURN 1  
  CASE pnPow > 0
    RETURN pnBase * Raise_To_Power(pnBase, pnPow-1)  
  CASE pnPow < 0
    RETURN 1 / (pnBase * Raise_To_Power(pnBase, -(pnPow+1)))

ENDCASE

ENDFUNC

Working code for raising BASE to a pos or neg BASE:

FUNC Raise_To_Power

LPARAMETERS pnBase, pnPow

DO CASE

  CASE pnPow = 0
    RETURN 1  
  CASE pnPow > 0
    RETURN pnBase * Raise_To_Power(pnBase, pnPow-1)  
  CASE pnPow < 0
    RETURN 1 / (pnBase * Raise_To_Power(pnBase, -(pnPow+1)))

ENDCASE

ENDFUNC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文