Java数学问题输出不正确
(51^43)科学计算器中的Mod77给出2作为输出 (int)(Math.pow(51,43)%(double)77)给出12,应该是2 你能帮忙吗?而不是: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进行任意精度的整数运
(int)(Math.pow(51,43)%(double)77)
做:
而不是:
(int)(Math.pow(51,43)%(double)77)
做:
双精度
double
的精度不足以容纳Math.pow(51,43)
的所有数字。因此,当你采用77模时,答案很容易出现明显的舍入误差
我建议使用
biginger
进行任意精度的整数运算。Adouble
的精度不足以容纳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
为什么演员阵容要加倍?这是完全没有意义的,因为%只为整数类型定义,并且%的左右两侧在应用%之前被提升为整数。为什么要转换为双精度?它完全没有意义,因为%只为整数类型定义,并且%的左右两侧在应用%之前被提升为整数。