我试图将1020拿到第275次电源,并在1073年进行修改,但是Java不会输出正确的答案



我正在处理解码器,我需要将1020拿到275次功率,并在1073年之前进行mod,但是当我尝试使用时,它不会打印出正确的答案

    double decoded = (1020^275)%1073;

那是我正在尝试的代码,但它将打印出751,它应该打印4,任何人都有任何提示?

^xor操作员;但是,第275次电源的1020是巨大数字和 Will 即使使用doublelong,也应由BigInteger

表示。
System.out.println(BigInteger.valueOf(1020).pow(275).mod(BigInteger.valueOf(1073)));

输出:

4

NOTE :我使用了BigInteger#mod,但是BigInteger#remainder在您的情况下会返回相同的值(因为您仅处理非负值(。

您可以使用BigInteger.pow(),其次是BigInteger.mod()

但是,BigInteger类专门为您的任务运行:BigInteger.modPow()

System.out.println(BigInteger.valueOf(1020).modPow(BigInteger.valueOf(275), BigInteger.valueOf(1073)));

给出4。

尝试一下:

BigDecimal remainder = new BigDecimal(1020).pow(275).remainder(new BigDecimal(1073));
System.out.println(remainder);

应用剩余后,可以将其最大值为1072(在这种情况下(:

,可以转换为长期。
remainder.longValueExact();

double是有限的并且具有精确限制,请尝试:

System.out.println(Math.pow(1020, 275));  // Infinity

通常,您应该使用BigDecimalBigInteger来操纵大数字,

BigDecimal bigDecimal = new BigDecimal(1020);
bigDecimal = bigDecimal.pow(275);
bigDecimal = bigDecimal.remainder(new BigDecimal(1073));
System.out.println(bigDecimal); //4
Xor is differ from power function. 2 ^ 3, The output of an XOR gate is true only when exactly one of its inputs is true. If both of an XOR gate's inputs are false, or if both of its inputs are true, then the output of the XOR gate is false.
double maximum value = 1.7976931348623157E308
Actually (1020^275) = 2.3176467070363862212063591722218e+827 which greater than double maximum value.
Go with BigDecimal.
System.out.println(new BigDecimal(1020.0).pow(275).remainder(new BigDecimal(1073)));

相关内容

最新更新