简体   繁体   English

int类型的无效操作数和二进制'operator%'的double

[英]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.

相关问题 二进制“ operator%”的类型为“ double”和“ int”的无效操作数 - invalid operands of types `double' and `int' to binary `operator%' 'double' 和 'int' 类型的无效操作数到二进制 'operator%' - invalid operands of types 'double' and 'int' to binary 'operator%' 'int' 和 'int [3]' 类型的无效操作数到二进制 'operator*' - invalid operands of types ‘int’ and ‘int [3]’ to binary ‘operator*’ 二进制“ operator%”的类型为“ double”和“ int”的无效操作数 - Invalid operands of type 'double' and 'int' to binary 'operator%' 类型为&#39;double *&#39;和&#39;double&#39;的无效操作数到二进制&#39;operator +&#39; - invalid operands of types 'double*' and 'double' to binary 'operator+' 错误:“double”和“double”类型的无效操作数转换为二进制“operator%” - error: invalid operands of types ‘double’ and ‘double’ to binary ‘operator% 类型为“ double”的无效操作数snd const char [3]”转换为二进制“ operator &lt;&lt;” - invalid operands of types ‘double’ snd const char [3]’ to binary 'operator<<' 二进制“ operator!=” |的类型为&#39;double&#39;和&#39;const char [2]&#39;的无效操作数 - Invalid operands of types 'double' and 'const char [2]' to binary 'operator!='| 错误:C ++中类型为“ float”和“ int”的二进制“ operator%”类型的无效操作数 - error: invalid operands of types 'float' and 'int' to binary 'operator%' in c++ 错误:类型为&#39;const char [3]&#39;和&#39;int *&#39;的无效操作数为二进制&#39;operator *&#39; - Error: invalid operands of types 'const char [3]' and 'int*' to binary 'operator*'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM