LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 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: Reputation: 15
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.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-23-2008, 09:18 PM   #2
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Include math.h and use ceil. Don't forget to link to the math library
 
Old 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: Reputation: 148Reputation: 148
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.
 
Old 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: Reputation: 15
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.
 
Old 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: Reputation: 148Reputation: 148
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.
 
Old 04-25-2008, 04:43 AM   #6
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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.
 
Old 11-18-2008, 04:08 PM   #7
Homncruse
LQ Newbie
 
Registered: Nov 2008
Posts: 1

Rep: Reputation: 0
Post

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
 
Old 10-19-2012, 09:45 PM   #8
mtytel
LQ Newbie
 
Registered: Oct 2012
Posts: 1

Rep: Reputation: Disabled
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.
 
Old 10-19-2012, 11:19 PM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,149

Rep: Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523Reputation: 1523
But do these solutions work for big numbers? What (int)1e20 is, for example?
 
Old 03-31-2017, 03:41 PM   #10
dizcza
LQ Newbie
 
Registered: Mar 2017
Posts: 1

Rep: Reputation: Disabled
@mtytel,
your solution doesn't work for any real number in (-1, 0) range.
 
Old 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: Reputation: 3602Reputation: 3602Reputation: 3602Reputation: 3602Reputation: 3602Reputation: 3602Reputation: 3602Reputation: 3602Reputation: 3602Reputation: 3602Reputation: 3602
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.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
programming pwd function Hard_Working_ Programming 3 04-03-2007 01:36 AM
int -- floor or ceiling? (C Programming) smoothdogg00 Programming 3 04-17-2006 02:34 AM
HTB shaped bandwitch runs over the ceiling! lez Linux - Networking 2 09-05-2005 04:00 AM
gets() function in C programming njeri Programming 7 04-20-2005 06:24 AM
Programming function keys dazdaz Linux - Software 14 11-04-2003 04:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration