gpt4 book ai didi

java - 为什么这个值的输出结果总是零?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:26:37 26 4
gpt4 key购买 nike

我正在运行这段代码,但为什么这里m的输出结果总是零?

这很奇怪,因为 m 被初始化为 2。

public class ScalabilityTest {

public static void main(String[] args) {
long oldTime = System.currentTimeMillis();
double[] array = new double[100000];
int p = 2;
int m = 2;
for ( int i = 0; i < array.length; i++ ) {
p += p * 12348;
for ( int j = 0; j < i; j++ ) {
double x = array[j] + array[i];
m += m * 12381923;
}
}

System.out.println( (System.currentTimeMillis()-oldTime) / 1000 );
System.out.println( p + ", " + m );
}

}

最佳答案

由于您总是将 m 的值乘以一个数字并添加到 m,因此在第 16 次迭代时它会溢出变成 0 .

事实上,由于您将数字与奇数相乘,然后将其与原始数字相乘,因此您将其与偶数相乘,这使得尾随 0 位至少移动了一步左边,因此它以 0 结尾:

1 1011110011101110111001000 24763848
2 1111011100110010111011000100000 2073654816
3 1111111111111101111010010000000 2147415168
4 10010100011000001100001000000000 -1805598208
5 10010010100010001100100000000000 -1836529664
6 10001011110000100010000000000000 -1950212096
7 1110010101001001000000000000000 1923383296
8 1001100000100000000000000000 159514624
9 1010011110010000000000000000000 1405616128
10 10001110001000000000000000000000 -1910505472
11 1010100100000000000000000000000 1417674752
12 1000010000000000000000000000000 1107296256
13 11001000000000000000000000000000 -939524096
14 100000000000000000000000000000 536870912
15 10000000000000000000000000000000 -2147483648
16 0 0

关于java - 为什么这个值的输出结果总是零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17095903/

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