gpt4 book ai didi

Java幂函数答案

转载 作者:太空宇宙 更新时间:2023-11-04 10:23:55 25 4
gpt4 key购买 nike

我正在编写自己的幂函数实现,发现在 Integer.MAX_VALUE 附近出现一些奇怪的结果,我不确定为什么会出现这些结果。这是我的实现:

public static long power(long x, long y) {
int result = 1;
while (y > 0) {
if ((y & 1) == 0) {
x *= x;
y >>>= 1;
} else {
result *= x;
y--;
}
}
return result;
}

运行以下代码,

System.out.println(fastPower(2, 31));
System.out.println(Math.pow(2, 31);
System.out.println((long)Math.pow(2, 31));
System.out.println((int)Math.pow(2, 31));

结果如下,我不太明白。

-2147483648
2.147483648E9
2147483648
2147483647

当使用 short 时,这让我更加困惑:

System.out.println(fastPower(2, 15));
System.out.println(Math.pow(2, 15));
System.out.println((int)Math.pow(2, 15));
System.out.println((short)Math.pow(2,15));

32768
32768.0
32768
-32768

这些是我期望的答案,但它们似乎与 ints 的结果不一致。

最佳答案

intshort 的前三个输出很容易解释:

-2147483648 // your method returns an int, so overflows
2.147483648E9 // Math.pow returns a double, so formatted like this
2147483648 // double casted to a long, 2147483648 inside the possible range for long

32768 // your method returns an int, 32768 is inside the possible range for int
32768.0 // Math.pow returns a double, so formatted like this
32768 // double casted to an int, 32768 is inside the possible range for int

难以解释的是第四个结果。 System.out.println((int)Math.pow(2, 31)); 不应该也打印 -2147483648 吗?

这里的技巧是 Java 如何进行从 doubleint 的转换。根据规范,这称为缩小原始转换 ( §5.1.3 ):

22 specific conversions on primitive types are called the narrowing primitive conversions:

  • short to byte or char
  • char to byte or short
  • int to byte, short, or char
  • long to byte, short, char, or int
  • float to byte, short, char, int, or long
  • double to byte, short, char, int, long, or float

这是执行doubleint 转换的方式(我用粗体表示):

1. In the first step, the floating-point number is converted either to a long, if T is long, or to an int, if T is byte, short, char, or int, as follows:

  • If the floating-point number is NaN(§4.2.3), the result of the first step of the conversion is an int or long 0.
  • Otherwise, if the floating-point number is not an infinity, the floating-point value is rounded to an integer value V, rounding toward zero using IEEE 754 round-toward-zero mode (§4.2.3). Then there are two cases:

a. If T is long, and this integer value can be represented as a long, then the result of the first step is the long value V. b. Otherwise, if this integer value can be represented as an int, then the result of the first step is the int value V.

  • Otherwise, one of the following two cases must be true: a. The value must be too small (a negative value of large magnitude or negative infinity), and the result of the first step is the smallest representable value of type int or long. b. The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type int or long.

    1. In the second step:
  • If T is int or long, the result of the conversion is the result of the first step.

  • If T is byte, char, or short, the result of the conversion is the result of a narrowing conversion to type T (§5.1.3) of the result of the first step.

第一步将 double 更改为 int 的最大可表示值 - 2147483647。这就是为什么在 int 情况下打印 2147483647。在 short 情况下,第二步将 int 值 2147483647 更改为 short,如下所示:

A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T.

这就是为什么 short 溢出,但 int 没有溢出!

关于Java幂函数答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50775768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com