ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I could've swore I've seen and even implemented this before, but is there a mathematical way to find the ceiling of a number (without equality symbols)? This is the best I could come up with:
Why do you not want to use equality?
The macro you have looks okay, for positive numbers but I think that it will give you floor for negative numbers. Since, if I recall correctly, ceiling is defined as the next integer away from zero.
I definitely won't be dealing with negatives. It's a performance sensitive program so I figured something without an equality would be faster. Also, I can't guarantee I'll have the math library available.
In which case you may want to hold (int)X in a temporary variable and save having to do the conversion twice.
I'm not certain about the performance implications of a temp var against the type conversion but I suspect that will be quicker, just because the internal format of a float is quite complex. You could profile it (or look at the assembler generated) and make a decision.
Same argument goes for the performance of equality against greater than.
What graemf is politely telling you: unless you have profiling to tell you that your current incarnation of your macro saves a lot of execution time, then you are wasting your efforts. Algorithms are the heart of the beast - consider looking at changing to more efficient algorithm design, before going to the level of == vs > compares.
Yes > is faster than == in most implementations, but no user will ever see the difference unless the code in question is a true bottleneck in logic flow.
Sorry for a bump on a really old thread, but I can see TastyWheat's rationale for wanting to macro the ceiling function. I'm also working on a performance-sensitive program (embedded system), and as long as the macro is called only with constants, a good compiler should be able to make light work of it at compile-time, removing all the performance implications.
That said, I Google-Fu'd my way here for this macro and improved upon it to handle negative numbers (well, kind of):
// NOTE: These macros return the "Excel-like" interpretation of the ceiling function.
// This only matters with negative numbers: CEILING(-2.5) will return -3.
#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) )
You have replied to a 5 year old post, itself a reply to a 9 year old thread with little activity.
If you have a question or would like to contribute your own approach to a similar task, please consider opening your own thread to get better exposure.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.