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, 2022 in C++ by Nicholas
• 7,760 points
1,230 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, 2022 by Damon
• 4,960 points

edited Jun 14, 2022 by Hemant

Related Questions In C++

0 votes
0 answers

How can I get the size of a C++ function?

In C++, how can I find the size& ...READ MORE

Aug 5, 2022 in C++ by Nicholas
• 7,760 points
554 views
0 votes
0 answers

How to implement 2D vector array?

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

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
537 views
0 votes
0 answers

How to implement 2D vector array?

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

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
400 views
0 votes
1 answer

How can I get all the unique keys in a multimap

This method worked for me. for( multimap<char,int>::iterator it ...READ MORE

answered Jun 27, 2022 in C++ by Damon
• 4,960 points
1,498 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,020 points
690 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,020 points
2,392 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
1,452 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,740 points
1,859 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, 2022 in C++ by Damon
• 4,960 points
695 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, 2022 in C++ by Damon
• 4,960 points
2,518 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP