为什么这个值的输出结果始终为零?为零、结果

2023-09-11 02:33:08 作者:你不缺我i

我在运行此code,但为什么m的输出结果始终为零吗?

这是很奇怪的,因为M初始化为2。

 公共类ScalabilityTest {

公共静态无效的主要(字串[] args){
    长古来= System.currentTimeMillis的();
    双[]数组=新的双[100000]。
    INT P = 2;
    INT米= 2;
    的for(int i = 0; I< array.length;我++){
        P + = P * 12348;
        对于(INT J = 0; J<我; J ++){
            双X =阵列[J] +阵列[I]
            M + = M * 12381923;
        }
    }

    的System.out.println((System.currentTimeMillis的() - 古来)/ 1000);
    的System.out.println(P +,+米);
    }

}
 

解决方案

由于你总是有数倍的 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
 
为什么输出为零

I'm running this code, but why the output result of m is always zero here?

This is very strange since m is initialized to 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 );
    }

}

解决方案

Since you are always multiplying the value of m with a number and add to m, on the 16th iteration it overflows to become 0.

In fact, since you are multiplying the number with an odd number then add it to the original, you are multiplying it with a even number, which make the trailing 0 bits moves at least one step left, thus it ends with 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