I have a C program which involves some floating-point literal that's defined by a macro. And - it just so happens that in a performance-critical part of the code, I need the ceiling of that floating-point number, as an integer (say, an int). Now, if it were an actual literal, then I would just manually take the ceiling and be done with it. But since it's a macro, I can't do that.
Naturally, ceilf(MY_DOUBLE_LITERAL) works, but - it's expensive. (int)(MY_DOUBLE_LITERAL) + 1 will also work... except if the literal is itself an integer, in which case it will be wrong.
So, is there some way - preprocessor macros are fine - to obtain the ceiling of MY_DOUBLE_LITERAL, or alternatively to determine whether it is an integer or not?
Notes:
- I cannot perform the
ceil()before the performance-critical part of the code, due to constraints I won't go into. Naturally if one can move computation outside of the performance-critical part or loop, that's always best, and thanks goes to @SpyrosK for mentioning that. - C99 preferred.
- You may assume the literal is a double, i.e. a decimal number like 123.4 or an exponential-notation like 1.23e4.
#define ceil_literal(X) _Generic((X), double: ceil, default: dummy_int)(X), but of course, not all C99 compilers support_Generic.ceilf(MY_DOUBLE_LITERAL)would be optimized just as well.