Reputation: 123
Is it possible in C language, to convert a non integer number to integer number (closest greater integer) in the pre-processor directive only?
Inside the loop the the Ceil function generally does the job, but I have to define vector(constant size).
#define Lenght_of_box 52.5
#define Vector_size ceil(Lenght_of_box)
Ceil function isn't working inside the pre-processor.
Upvotes: 1
Views: 308
Reputation: 23208
..."I need to divide the box into grids of length 1, So for 52.1 or 52.9, I need 53, also negative numbers are not my concern." (from comments)
Given the corner cases you are willing to accept, this macro will work:
#define CEIL(x) (((x)-(int)(x)) > 0 ? (int)((x)+1) : (int)(x))
Tested for the following small sample size:
int main(void)
{
// float num = 4.0;
// float num = 4.999;
float num = 4.001;
int iNum = CEIL(num);
printf( "This is a float to int conversion: %d\n", iNum);
return 0;
}
Upvotes: 3
Reputation: 234665
One way is to use
#define f_ 55.1
#define n_ ((int)f_ + !!(f_ - (int)f_))
where n_
is evaluated by the preprocessor, and is f_
rounded up. The !!
implements the rounding up.
Test program:
int main()
{
char n[n_]; // might be a VLA, so runtime `n_` would be permitted ...
switch (n_){
case n_: // ...but this will not compile if `n_` is not a constant
printf("%zu", sizeof(n)); // The value of `n_`
}
}
Clearly dealing with negative numbers is not a concern.
Upvotes: 6