How to use % operator for float values in c

前端 未结 6 471
故里飘歌
故里飘歌 2020-12-03 14:30

When I use % operator on float values I get error stating that \"invalid operands to binary % (have ‘float’ and ‘double’)\".I want to enter the integers value only but the n

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-03 15:30

    When I haven't had easy access to fmod or other libraries (for example, doing a quick Arduino sketch), I find that the following works well enough:

    float someValue = 0.0;
    
    // later...
    
    // Since someValue = (someValue + 1) % 256 won't work for floats...
    someValue += 1.0; // (or whatever increment you want to use)
    while (someValue >= 256.0){
        someValue -= 256.0;
    }
    

提交回复
热议问题