Implementation of ceil function in C

て烟熏妆下的殇ゞ 提交于 2019-11-29 07:44:43

The ceil() function is implemented in the math library, libm.so. By default, the linker does not link against this library when invoked via the gcc frontend. To link against that library, pass -lm on the command line to gcc:

gcc main.c -lm

Try this out:

#define CEILING_POS(X) ((X-(int)(X)) > 0 ? (int)(X+1) : (int)(X))
#define CEILING_NEG(X) ((X-(int)(X)) < 0 ? (int)(X-1) : (int)(X))
#define CEILING(X) ( ((X) > 0) ? CEILING_POS(X) : CEILING_NEG(X) )

Check out the link for comments, proof and discussion: http://www.linuxquestions.org/questions/programming-9/ceiling-function-c-programming-637404/

The prototype of the ceil function is:

double ceil(double)

My guess is that the type of your variable count is not of type double. To use ceil in C, you would write:

#include <math.h>
// ...
double count = 3.0;
double result = ceil(count/2.0);

In C++, you can use std::ceil from <cmath>; std::ceil is overloaded to support multiple types:

#include <cmath>
// ...
double count = 3.0;
double result = std::ceil(count/2.0);
double ceil (double x) {
    if (x > LONG_MAX) return x; // big floats are all ints
    return ((long)(x+(0.99999999999999997)));
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!