我正在尝试计算一个幂,然后是一个大数的模(类型为double)。我想计算(1234^79) mod 3337。结果是901 (Ubuntu的计算器),但是函数fmod返回1788。
#include <math.h>
#include <tgmath.h>
#include <stdlib.h>
#include <stdio.h>
void main(){
double res1;
double dou = powl(1234.00,79.00);//function for doubles
printf("Result in double o powl %.3f\n",dou);
res1=fmod(dou, 3337.00);//doubles
printf("Result in double of fmod %.2f\n",res1);
}我做错了什么?有答案吗?提前谢谢。
发布于 2017-10-25 16:50:27
1234^79有245 decimal digits。
double只有大约17位十进制数字的精度。
您需要使用一些数学知识将此问题简化为不需要任意精度的问题(请参阅Modular Exponentiation)。
(或者,如果您对有效的解决方案不感兴趣,请使用任意精度的数学库+蛮力。)
https://stackoverflow.com/questions/46927868
复制相似问题