我正在编写一些C代码,以反复循环浮动的整数值。标准C数学库包括floor()和ceil()函数。我注意到,函数的一个更快的实现是直接转换为整数:
int up, down;
float test = 1.3548;
up = (int)(test + 1); //ceil()
down = (int)test; //floor()我做了一个快速检查,这似乎很好。
发布于 2015-02-02 20:31:44
函数ceil()和floor()将返回与使用
up = (int)(test + 1);
down = (int)test;当你有一个负数的时候。
如果你有:
float test = -1.3548;
up = (int)test; // ceil()
down = (int)(test-1); // floor()当floor()是整数时,即使最后一条语句也不是计算test的好方法。
除非您想要以不同的方式处理正负数,以及test是整数的特殊情况,否则最好使用ceil()和floor()。
https://stackoverflow.com/questions/28286129
复制相似问题