[英]invalid operands of types int and double to binary 'operator%'
After compiling the program I am getting below error 编译程序后,我遇到错误
invalid operands of types int and double to binary 'operator%' at line
"newnum1 = two % (double)10.0;"
Why is it so? 为什么会这样?
#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;
}
Because %
is only defined for integer types. 因为
%
仅为整数类型定义。 That's the modulus operator. 那是模数运算符。
5.6.2 of the standard: 5.6.2标准:
The operands of * and / shall have arithmetic or enumeration type;
*和/的操作数应具有算术或枚举类型; the operands of % shall have integral or enumeration type.
%的操作数应具有整数或枚举类型。 [...]
[...]
As Oli pointed out, you can use fmod()
. 正如Oli所指出的,你可以使用
fmod()
。 Don't forget to include math.h
. 别忘了包含
math.h
Because %
only works with integer types. 因为
%
仅适用于整数类型。 Perhaps you want to use fmod()
. 也许你想使用
fmod()
。
Yes. 是。 % operator is not defined for double type.
%运算符未定义为double类型。 Same is true for bitwise operators like "&,^,|,~,<<,>>" as well.
对于像“&,^,|,〜,<<,>>”这样的按位运算符也是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.