错误:获取float模数时,操作数无效为二进制%

这是一个相当长的一周,所以如果我很厚,请原谅我。

我有一些代码:

float someFloat = 0; //....do some stuff to someFloat //.... if( someFloat % 1) { //take some action } 

我收到一个编译器错误: error: invalid operands to binary %

假设编译器不是药物,这有什么不对吗?

编辑:顺便说一句,我实际想要做的是检测非整数值并向上舍入。 我应该做的是调用roundf(我想检查返回是否小于操作数,然后再增加,如果是这样,请注意我们已经四舍五入)

%是整数运算符 – 使用fmod或fmodf表示双精度数或浮点数。

或者,如果您希望浮点数表示整数值,则首先将其转换为int ,例如:

 if ((int)someFloat % 2 == 1) // if f is an odd integer value { ... } 

模数运算符仅适用于整数。 对于浮点值,请使用fmodfmodf

%只适用于整数使用fmod进行浮点或双值**

 double fmod(double x, double y) x -- This is the floating point value with the division numerator iex y -- This is the floating point value with the division denominator iey