Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8520861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:39:01+00:00 2026-06-11T06:39:01+00:00

I want to implement my own ceil() in C . Searched through the libraries

  • 0

I want to implement my own ceil() in C. Searched through the libraries for source code & found here, but it seems pretty difficult to understand. I want clean & elegant code.

I also searched on SO, found some answer here. None of the answer seems to be correct. One of the answer is:

#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) )

AFAIK, the return type of the ceil() is not int. Will macro be type-safe here?
Further, will the above implementation work for negative numbers?

What will be the best way to implement it?

Can you provide the clean code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:39:03+00:00Added an answer on June 11, 2026 at 6:39 am

    The macro you quoted definitely won’t work correctly for numbers that are greater than INT_MAX but which can still be represented exactly as a double.

    The only way to implement ceil() correctly (assuming you can’t implement it using an equivalent assembly instruction) is to do bit-twiddling on the binary representation of the floating point number, as is done in the s_ceil.c source file behind your first link. Understanding how the code works requires an understanding of the floating point representation of the underlying platform — the representation is most probably going to be IEEE 754 — but there’s no way around this.

    Edit:

    Some of the complexities in s_ceil.c stem from the special cases it handles (NaNs, infinities) and the fact that it needs to do its work without being able to assume that a 64-bit integral type exists.

    The basic idea of all the bit-twiddling is to mask off the fractional bits of the mantissa and add 1 to it if the number is greater than zero… but there’s a bit of additional logic involved as well to make sure you do the right thing in all cases.

    Here’s a illustrative version of ceil() for floats that I cobbled together. Beware: This does not handle the special cases correctly and it is not tested extensively — so don’t actually use it. It does however serve to illustrate the principles involved in the bit-twiddling. I’ve tried to comment the routine extensively, but the comments do assume that you understand how floating point numbers are represented in IEEE 754 format.

    union float_int
    {
        float f;
        int i;
    };
    
    float myceil(float x)
    {
        float_int val;
        val.f=x;
    
        // Extract sign, exponent and mantissa
        // Bias is removed from exponent
        int sign=val.i >> 31;
        int exponent=((val.i & 0x7fffffff) >> 23) - 127;
        int mantissa=val.i & 0x7fffff;
    
        // Is the exponent less than zero?
        if(exponent<0) 
        {   
            // In this case, x is in the open interval (-1, 1)
            if(x<=0.0f)
                return 0.0f;
            else
                return 1.0f;
        }
        else
        {
            // Construct a bit mask that will mask off the
            // fractional part of the mantissa
            int mask=0x7fffff >> exponent;
    
            // Is x already an integer (i.e. are all the
            // fractional bits zero?)
            if((mantissa & mask) == 0)
                return x;
            else
            {
                // If x is positive, we need to add 1 to it
                // before clearing the fractional bits
                if(!sign)
                {
                    mantissa+=1 << (23-exponent);
    
                    // Did the mantissa overflow?
                    if(mantissa & 0x800000)
                    {
                        // The mantissa can only overflow if all the
                        // integer bits were previously 1 -- so we can
                        // just clear out the mantissa and increment
                        // the exponent
                        mantissa=0;
                        exponent++;
                    }
                }
    
                // Clear the fractional bits
                mantissa&=~mask;
            }
        }
    
        // Put sign, exponent and mantissa together again
        val.i=(sign << 31) | ((exponent+127) << 23) | mantissa;
    
        return val.f;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement all my own rotation animations but if I only return
I want to modify this method (implement my own version of it) but I
i want to implement a search engine.Is there any source code available for it
I'm developing a Windows Phone 7 app but don't want to re-implement my own
I want to implement my own copy method for a class A which looks
I want to implement an action bar for my own android app like the
I'm developing own CMS and i want to implement functionality to dynamically include existing
In Django admin I want to override and implement my own form for a
I'm attempting to override and implement my own TabExpansion. In the function I want
I want implement in my software solution an VBA editor but in c# 3.0.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.