编译程序后出现以下错误
invalid operands of types int and double to binary 'operator%' at line
"newnum1 = two % (double)10.0;"
为什么会这样?
#include<iostream>
#include<math>
using namespace std;
int main()
{
int num;
double two = 1;
double newnum, newnum1;
newnum = newnum1 = 0;
for(num = 1; num <= 50; num++)
{
two = two * 2;
}
newnum1 = two % (double)10.0;
newnum = newnum + newnum1;
cout << two << "\n";
return 0;
}
最佳答案
因为 %
只为整数类型定义。这就是模运算符。
标准的5.6.2:
The operands of * and / shall have arithmetic or enumeration type; the operands of % shall have integral or enumeration type. [...]
正如 Oli 指出的,您可以使用 fmod()
。不要忘记包含 math.h
。
关于c++ - int 和 double 类型的无效操作数到二进制 'operator%',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9278224/