Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java数学问题输出不正确_Java_Math_Modulo - Fatal编程技术网

Java数学问题输出不正确

Java数学问题输出不正确,java,math,modulo,Java,Math,Modulo,(51^43)科学计算器中的Mod77给出2作为输出 (int)(Math.pow(51,43)%(double)77)给出12,应该是2 你能帮忙吗?而不是: (int)(Math.pow(51,43)%(double)77) 做: 而不是: (int)(Math.pow(51,43)%(double)77) 做: 双精度double的精度不足以容纳Math.pow(51,43)的所有数字。因此,当你采用77模时,答案很容易出现明显的舍入误差 我建议使用biginger进行任意精度的整数运

(51^43)科学计算器中的Mod77给出2作为输出

(int)(Math.pow(51,43)%(double)77)给出12,应该是2

你能帮忙吗?

而不是:

(int)(Math.pow(51,43)%(double)77)
做:

而不是:

(int)(Math.pow(51,43)%(double)77)
做:


双精度
double
的精度不足以容纳
Math.pow(51,43)
的所有数字。因此,当你采用77模时,答案很容易出现明显的舍入误差


我建议使用
biginger
进行任意精度的整数运算。

A
double
的精度不足以容纳
Math.pow(51,43)
的所有数字。因此,当你采用77模时,答案很容易出现明显的舍入误差

    final BigInteger base = BigInteger.valueOf(51);
    final BigInteger exponent = BigInteger.valueOf(43);
    final BigInteger modulus = BigInteger.valueOf(77);
    System.out.println(base.modPow(exponent, modulus));
我建议对任意精度的整数运算使用
biginger

    final BigInteger base = BigInteger.valueOf(51);
    final BigInteger exponent = BigInteger.valueOf(43);
    final BigInteger modulus = BigInteger.valueOf(77);
    System.out.println(base.modPow(exponent, modulus));
打印
2


打印
2

为什么演员阵容要加倍?这是完全没有意义的,因为%只为整数类型定义,并且%的左右两侧在应用%之前被提升为整数。为什么要转换为双精度?它完全没有意义,因为%只为整数类型定义,并且%的左右两侧在应用%之前被提升为整数。