Ceil function: how can we implement it ourselves?

前端 未结 9 2067
别那么骄傲
别那么骄傲 2020-12-30 02:06

I know that C++ provides us with a ceil function. For practice, I was wondering how can we implement the ceil function in C++. The signature of the method is public static

9条回答
  •  感动是毒
    2020-12-30 02:33

    Here's one that works with negative numbers:

    int ceil(float num) {
        int inum = (int)num;
        if (num < 0 || num == (float)inum) {
            return inum;
        }
        return inum + 1;
    }
    

提交回复
热议问题