Share your knowledge at the LQ Wiki.
 LinuxQuestions.org Ceiling Function (C programming)
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

 04-23-2008, 08:41 PM #1 TastyWheat Member   Registered: Aug 2003 Location: Texas Distribution: Knoppix 5.0.1, Fedora Core 5 Posts: 66 Rep: Ceiling Function (C programming) 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: Code: `#define CEILING(X) (X-(int)(X) > 0 ? (int)(X+1) : (int)(X))` Last edited by TastyWheat; 04-23-2008 at 08:57 PM.
 04-23-2008, 09:18 PM #2 95se Member   Registered: Apr 2002 Location: Windsor, ON, CA Distribution: Ubuntu Posts: 740 Rep: Include math.h and use ceil. Don't forget to link to the math library
 04-23-2008, 10:50 PM #3 graemef Senior Member   Registered: Nov 2005 Location: Hanoi Distribution: Fedora 13, Ubuntu 10.04 Posts: 2,379 Rep: 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.
 04-24-2008, 05:21 PM #4 TastyWheat Member   Registered: Aug 2003 Location: Texas Distribution: Knoppix 5.0.1, Fedora Core 5 Posts: 66 Original Poster Rep: 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.
 04-24-2008, 06:18 PM #5 graemef Senior Member   Registered: Nov 2005 Location: Hanoi Distribution: Fedora 13, Ubuntu 10.04 Posts: 2,379 Rep: 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.
 04-25-2008, 04:43 AM #6 jim mcnamara Member   Registered: May 2002 Posts: 964 Rep: 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.
 11-18-2008, 04:08 PM #7 Homncruse LQ Newbie   Registered: Nov 2008 Posts: 1 Rep: 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) ) Test Cases: CEILING(11.1) --> CEILING_POS(11.1) = 11.1 - 11 (=0.1) --> --> 0.1 > 0 = TRUE, return (int)(11.1 + 1) = 12 CEILING(-3.5) --> CEILING_NEG(-3.5) = -3.5 - -3 (=-0.5) --> --> -0.5 < 0 = TRUE, return (int)(-3.5 - 1) = -4 CEILING(5) --> CEILING_POS(5) = 5 - 5 (=0) --> --> 0 > 0 = FALSE, return (int)(5) = 5 CEILING(-5) --> CEILING_NEG(-5) = -5 - -5 (=0) --> --> 0 < 0 = FALSE, return (int)(-5) = -5 CEILING(0) --> CEILING_NEG(0) = 0 - 0 (=0) --> --> 0 < 0 = FALSE, return (int)(0) = 0 Last edited by Homncruse; 11-18-2008 at 04:11 PM. Reason: Comment clarification
 10-19-2012, 09:45 PM #8 mtytel LQ Newbie   Registered: Oct 2012 Posts: 1 Rep: I'm sorry for digging this up from the grave, but it's a high hit on google for this question and there are no other answers. This is what I came up, it works for both positive and negative numbers: Code: `#define CEILING(x) (int)(x) + (1 - (int)((int)((x) + 1) - (x)))` Last edited by mtytel; 10-19-2012 at 09:46 PM.
 10-19-2012, 11:19 PM #9 NevemTeve Senior Member   Registered: Oct 2011 Location: Budapest Distribution: Debian/GNU/Linux, AIX Posts: 4,149 Rep: But do these solutions work for big numbers? What (int)1e20 is, for example?
 03-31-2017, 03:41 PM #10 dizcza LQ Newbie   Registered: Mar 2017 Posts: 1 Rep: @mtytel, your solution doesn't work for any real number in (-1, 0) range.
 03-31-2017, 04:05 PM #11 astrogeek Moderator   Registered: Oct 2008 Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1} Posts: 5,583 Blog Entries: 11 Rep: Welcome to LQ dizcza! 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. 2 members found this post helpful.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Hard_Working_ Programming 3 04-03-2007 01:36 AM smoothdogg00 Programming 3 04-17-2006 02:34 AM lez Linux - Networking 2 09-05-2005 04:00 AM njeri Programming 7 04-20-2005 06:24 AM dazdaz Linux - Software 14 11-04-2003 04:37 AM

LinuxQuestions.org

All times are GMT -5. The time now is 09:27 PM.

 Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -