“实现 RSA”+“我必须计算 4294967296 ^ 2 之类的东西”是矛盾的。要实现 RSA,不需要该计算。该算法不需要比 64 位更宽的整数。
我试过使用 float、double、long double,但结果不正确。
4294967000.0 * 4294967000.0 the result is 18446741874686296064.0
使用unsigned long long
数学。典型double
的精度只有 53 位,但这个计算需要 60+ 位才能得到精确的乘积。
int main(void) {
unsigned long x = 4294967000;
printf("%lu * %lu the result is %lu\n", x,x,x*x); // overflow
printf("%lu * %lu the result is %llu\n", x,x,(unsigned long long) (x*x)); // overflow
printf("%lu * %lu the result is %llu\n", x,x,(unsigned long long)x*x);// good 64-bit math
return 0;
}
输出
4294967000 * 4294967000 the result is 87616
4294967000 * 4294967000 the result is 87616
4294967000 * 4294967000 the result is 18446741531089000000