Ceil and floor functions in C++ without using std::ceil or std::floor

Revision en7, by TryOmar, 2022-11-27 20:57:42

Well, instead of using built-in functions in the language it's better if you know how they work and use them in your own way manually

I wondered how the ceil and floor function works, especially in the division $$$\frac{a}{b}$$$.

One of my friends advised me to use this formula to find the ceil(a, b) $$$\lceil \frac{a}{b} \rceil = \frac{a - (b-1)}{b} = \frac{a - b + 1}{b}$$$ so in code : int ceil = (a + b - 1) / 2; this works perfectly fine with positive $$$a/b$$$

But unfortunately, this formula is not accurate, as it does not work with negative numbers

Here is a counter example $$$\lceil \frac{-6}{2} \rceil = -3$$$ but with this formula we get different result $$$\frac {-6+2-1}{-2}$$$ = $$$\frac {-5}{2} = -2.5$$$, so we rely can't on this with negative numbers

Now let's focus on floor for a bit, you maybe think floor(a,b) is so simple as int floor = (int)(a/b);

And I believed it and was happy because it is so simple. But the misunderstanding happened because I used to think that division of integers $$$\frac{a}{b}$$$ in programming is equal to rounding down the result $$$\lfloor \frac{a}{b} \rfloor$$$

While the right was Truncating division Truncating division is division where a fractional result is converted to an integer by rounding towards zero.

Or in another way integer division is just omitting the fraction part not flooring the value so we can't say that floor $$$\lfloor x \rfloor$$$ is equal to $$$(int)(x)$$$ because for example $$$\lfloor -2.5 \rfloor \neq (int)(-2.5)$$$

Anyway.. I tried to make that function again by myself without using the datatype double

int myfloor(int a, int b) {
    if (a % b == 0) return (a / b);
    bool negative = (a < 0) ^ (b < 0);
    return a / b - negative;
}

int myceil(int a, int b) {
    if (a % b == 0) return (a / b);
    bool positive = !((a < 0) ^ (b < 0));
    return a / b + positive;
}

If there are any better or shorter ways, please write them in the comments. This is the reason for my post (Seeking better solutions)

Tags c++, ceil, floor, integer, function, built-in functions, division, manual function

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en11 English TryOmar 2022-11-28 09:22:50 2 Tiny change: ' b - 1) / 2;` this wo' -> ' b - 1) / b;` this wo'
en10 English TryOmar 2022-11-27 21:07:24 10 Tiny change: '5$, so we rely can't on this w' -> '5$, so we can't rely on this w'
en9 English TryOmar 2022-11-27 21:05:12 1 Tiny change: ' {-6+2-1}{-2}$ = $\fr' -> ' {-6+2-1}{2}$ = $\fr'
en8 English TryOmar 2022-11-27 21:03:54 24 Tiny change: '= \frac{a - (b-1)}{b} = \frac{a - b + 1}{b}$ so' -> '= \frac{a + b - 1}{b}$ so'
en7 English TryOmar 2022-11-27 20:57:42 2 Tiny change: ' datatype double\n\n~~~~~\' -> ' datatype `double`\n\n~~~~~\'
en6 English TryOmar 2022-11-27 20:55:54 2336 Tiny change: 't floor $\lfloor x \rfloor$ is ' -> 't floor $\rfloor x \lfloor$ is '
en5 English TryOmar 2022-11-27 20:50:30 48 Tiny change: '\frac {-6+7-1}{-2}$ =' -> '\frac {-6+2-1}{-2}$ ='
en4 English TryOmar 2022-11-27 20:34:42 24 Tiny change: 's formula $\frac {-' -> 's formula we get different result $\frac {-'
en3 English TryOmar 2022-11-27 20:26:25 3 Tiny change: 'so simple `int floo' -> 'so simple as `int floo'
en2 English TryOmar 2022-11-27 20:26:01 6 Tiny change: 'o we rely on this w' -> 'o we rely can't on this w'
en1 English TryOmar 2022-11-27 20:20:34 2089 Initial revision (published)