Java 公式为';不能正确计算
我试图弄明白为什么下面的计算不正确Java 公式为';不能正确计算,java,equation,modulo,Java,Equation,Modulo,我试图弄明白为什么下面的计算不正确 x = Math.pow(w,e); 当我用Java计算它时,我得到1075,但我应该得到779 int e = 17; int w = 803; int n = 2773; double x = 0; x = Math.pow(w,e) % n; System.out.println(x); double浮点数有53位精度,不足以存储像80317这样的大数的精确值。要在Java中执行模幂运算,您可
x = Math.pow(w,e);
当我用Java计算它时,我得到1075,但我应该得到779
int e = 17;
int w = 803;
int n = 2773;
double x = 0;
x = Math.pow(w,e) % n;
System.out.println(x);
double
浮点数有53位精度,不足以存储像80317这样的大数的精确值。要在Java中执行模幂运算,您可以使用biginger
s上的方法。double
浮点数有53位精度,这不足以存储像80317这样的大数的精确值。要在Java中执行模幂运算,您可以使用biginger
s上的方法。因为(803^17)是一个非常大的数字,所以您必须对此处使用的变量使用biginger数据类型,而不是int或double数据类型。我们无法将整数或双精度变量转换为BigInteger变量
因此,该计划将是:
import java.math.BigInteger;
public class JavaPow {
public static void main(String[] args)
{
BigInteger e = new BigInteger("17");
BigInteger w = new BigInteger("803");
BigInteger n = new BigInteger("2773");
BigInteger x;
x = w.modPow(e,n);
System.out.println(x);
}
}
因为(803^17)是一个非常大的数字,所以这里使用的变量必须使用BigInteger数据类型,而不是int或double数据类型。我们无法将整数或双精度变量转换为BigInteger变量
因此,该计划将是:
import java.math.BigInteger;
public class JavaPow {
public static void main(String[] args)
{
BigInteger e = new BigInteger("17");
BigInteger w = new BigInteger("803");
BigInteger n = new BigInteger("2773");
BigInteger x;
x = w.modPow(e,n);
System.out.println(x);
}
}
800^17太大了。考虑使用双整数800 ^ 17太大。考虑使用双整数