Math Power(x, n) 在 Java 中对 -2147483648 不起作用。

huangapple go评论56阅读模式
英文:

Math Power(x,n) does not work -2147483648 for in java?

问题

I am doing a leetcode problem.
https://leetcode.com/problems/powx-n/

> 实现 pow(x, n),计算 x 的 n 次方(即 x^n)。

I have no idea why last test case is failing, x^-2147483648 returns 1 instead of 0.

  1. 为什么这应该首先返回 0?
  2. 此外,调试器中的 Math.abs(n) 仍然返回负数 -2147483648。

--

Code:

class Solution {
    public double myPow(double x, int n) {
        if (n == 0)
            return 1;
        if (n < 0) {
            return 1 / getDFS(x, Math.abs(n));
        }
        return getDFS(x, n);
    }

    private double getDFS(double x, int n) {
        if (n == 1) {
            return x;
        }
        double saveData = 1;
        if (n / 2 >= 1) {
            int newN = n / 2;
            saveData = getDFS(x,newN);
        } 
        if (n % 2 == 1) {
            return saveData * saveData * x;
        }
        return saveData * saveData;
    }
}

Results:

Math Power(x, n) 在 Java 中对 -2147483648 不起作用。

英文:

I am doing a leetcode problem.
https://leetcode.com/problems/powx-n/

> Implement pow(x, n), which calculates x raised to the power n (i.e.,
> x^n).

I have no idea why last test case is failing, x^-2147483648 returns 1 instead of 0.

  1. Why should this even return 0 in first place?
  2. Also, Math.abs(n) in debugger still returns a negative -2147483648.

--

Code:

class Solution {
    public double myPow(double x, int n) {
        if (n == 0)
            return 1;
        if (n < 0) {
            return 1 / getDFS(x, Math.abs(n));
        }
        return getDFS(x, n);
    }

    private double getDFS(double x, int n) {
        if (n == 1) {
            return x;
        }
        double saveData = 1;
        if (n / 2 >= 1) {
            int newN = n / 2;
            saveData = getDFS(x,newN);
        } 
        if (n % 2 == 1) {
            return saveData * saveData * x;
        }
        return saveData * saveData;
    }
}

Results:

Math Power(x, n) 在 Java 中对 -2147483648 不起作用。

答案1

得分: 1

最小负整数的绝对值比最大正整数的绝对值要大因此 `Math.abs` 无法返回 `Integer.MIN_VALUE` 的正确值

为了解决这个问题在使用二进制指数之前你可以将指数转换为 `long` 类型

```java
public double myPow(double x, int n) {
	if (n == 0)
		return 1;
	if (n < 0) return 1 / getDFS(x, Math.abs((long) n));
	return getDFS(x, n);
}
private double getDFS(double x, long n) {
    // ...
    if (n / 2 >= 1) { 
        long newN = n / 2;
        saveData = getDFS(x,newN);
    }
    // ...
}
英文:

The magnitude of the smallest negative int value is larger than the magnitude of the largest positive int value. Thus Math.abs cannot return the correct value for Integer.MIN_VALUE.

To solve this problem, you can cast the exponent to long before using binary exponentiation.

public double myPow(double x, int n) {
	if (n == 0)
		return 1;
	if (n &lt; 0) return 1 / getDFS(x, Math.abs((long) n));
	return getDFS(x, n);
}
private double getDFS(double x, long n) {
    // ...
    if (n / 2 &gt;= 1) { 
        long newN = n / 2;
        saveData = getDFS(x,newN);
    }
    // ...
}

huangapple
  • 本文由 发表于 2023年5月11日 10:58:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定