我正在编写一个程序,其中列出了 100,000 个素数。它适用于 10 个数字,但在这么多数字之后它们变成负值。我将整数更改为长整数,但没有任何改变,然后我将它们更改为 double ,我得到了标题中列出的错误。我的变量应该是什么?请记住,我还是编程新手。我也看了一些以前的帖子,没有看到答案。
int is_prime(double x,char array[]){
//doesnt use array but I put it in there
double j=2;//divider
for(j=2;j<=pow(x,0.5);j++){
if((x%j==0)){
return(0);
} //isnt prime
}
return(1);// because it is prime.
}
最佳答案
您不能对运算符使用 double,您必须使用 int。
你应该:#include <math.h>
然后使用 fmod 函数。
if(fmod(x,j)==0)
完整代码:
#include <math.h>
int is_prime(double x,char array[]){
//doesnt use array but I put it in there
double j=2;//divider
for(j=2;j<=pow(x,0.5);j++){
if(fmod(x,j)==0){
return(0);
} //isnt prime
}
return(1);// because it is prime.
}
关于c++ - 错误 : invalid operands to binary % (have 'double' and 'double' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3902399/