Ceil function how can we implement it ourselves

0 votes
I'm aware that C++ has a ceiling function.

I was wondering how we might implement the ceil function in C++ for practise.

The method's signature is public static int ceil (float num)

Please share your thoughts.

I came up with an easy solution:

Convert num to a string, locate the decimal point's index, and see if the decimal component is bigger than zero.

Return num+1 if yes; else, return num.

But I don't want to use the string conversion.
Jun 13 in C++ by Nicholas
• 2,460 points
11 views

1 answer to this question.

0 votes

You can disassemble the components of an IEEE754 floating point number and put the logic together yourself:

#include <cstring>

float my_ceil(float f)
{
    unsigned input;
    memcpy(&input, &f, 4);
    int exponent = ((input >> 23) & 255) - 127;
    if (exponent < 0) return (f > 0);
    // small numbers get rounded to 0 or 1, depending on their sign

    int fractional_bits = 23 - exponent;
    if (fractional_bits <= 0) return f;
    // numbers without fractional bits are mapped to themselves

    unsigned integral_mask = 0xffffffff << fractional_bits;
    unsigned output = input & integral_mask;
    // round the number down by masking out the fractional bits

    memcpy(&f, &output, 4);
    if (f > 0 && output != input) ++f;
    // positive numbers need to be rounded up, not down

    return f;
}
answered Jun 13 by Damon
• 3,580 points

edited Jun 14 by Hemant

Related Questions In C++

0 votes
0 answers

How to implement 2D vector array?

I'm using the vector class in the ...READ MORE

Jun 1 in C++ by Nicholas
• 2,460 points
11 views
0 votes
0 answers

How to implement 2D vector array?

I'm using the vector class in the ...READ MORE

Jun 1 in C++ by Nicholas
• 2,460 points
7 views
0 votes
0 answers

How can I get all the unique keys in a multimap

I have a multimap and I want to save all of the unique keys in it in a vector. multimap<char,int> mymm; multimap<char,int>::iterator it; char ...READ MORE

6 days ago in C++ by Nicholas
• 2,460 points
8 views
0 votes
0 answers

How can I get the maximum or minimum value in a vector?

In C++, how can I find the greatest or minimum value in a vector? Is it correct to assume that it would be similar with an array? Do I require an iterator?  I tried max element, but I kept receiving errors. vector<int>::const_iterator it; it = max_element(cloud.begin(), cloud.end()); error: request for ...READ MORE

1 day ago in C++ by Nicholas
• 2,460 points
4 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,080 points
190 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,080 points
1,372 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
787 views
0 votes
1 answer

Invalid method parameters for eth_sendTransaction

params needs to be an array, try {"jsonrpc":"2.0","method":"eth_se ...READ MORE

answered Sep 28, 2018 in Blockchain by digger
• 26,720 points
582 views
0 votes
1 answer

How can I convert a std::string to int?

There are some new convert methods in C++ that convert std::string to a numeric type. As an alternative to str.c str() atoi(str.c str()) atoi(str.c str() you can make use of std::stoi std::stoi ...READ MORE

answered Jun 1 in C++ by Damon
• 3,580 points
25 views
0 votes
1 answer

how could I use the power function in c/c++ without pow(), functions, or recursion

It is part of a series.  Replace pow() with the previous iteration's value. There is no need for code to call pow ().  Pow(x, 5 * I - 1) and pow(-1, I - 1) may be formed since both have an int exponent dependent on the iterator I from the previous loop iteration. Example: Let f(x, i) = pow(x, 5 * i ...READ MORE

answered Jun 21 in C++ by Damon
• 3,580 points
10 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP