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

Правка en3, от TryOmar, 2022-11-27 20:26:25

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) $$$\rceil \frac{a}{b} \lceil = \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 $$$\rceil \frac{-3}{7} \lceil = \rceil -0.5 \lceil = 0$$$ but with this formula $$$\frac {-3-7+1}{-3}$$$ = $$$\frac {-9}{-7} = 1$$$, 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 $$$\rfloor \frac{a}{b} \lfloor$$$

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 $$$\rfloor x \lfloor$$$ is equal to $$$(int)(x)$$$ because for example $$$\rfloor -2.5 \lfloor \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)

Теги c++, ceil, floor, integer, function, built-in functions, division, manual function

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en11 Английский TryOmar 2022-11-28 09:22:50 2 Tiny change: ' b - 1) / 2;` this wo' -> ' b - 1) / b;` this wo'
en10 Английский 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 Английский TryOmar 2022-11-27 21:05:12 1 Tiny change: ' {-6+2-1}{-2}$ = $\fr' -> ' {-6+2-1}{2}$ = $\fr'
en8 Английский 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 Английский TryOmar 2022-11-27 20:57:42 2 Tiny change: ' datatype double\n\n~~~~~\' -> ' datatype `double`\n\n~~~~~\'
en6 Английский TryOmar 2022-11-27 20:55:54 2336 Tiny change: 't floor $\lfloor x \rfloor$ is ' -> 't floor $\rfloor x \lfloor$ is '
en5 Английский TryOmar 2022-11-27 20:50:30 48 Tiny change: '\frac {-6+7-1}{-2}$ =' -> '\frac {-6+2-1}{-2}$ ='
en4 Английский TryOmar 2022-11-27 20:34:42 24 Tiny change: 's formula $\frac {-' -> 's formula we get different result $\frac {-'
en3 Английский TryOmar 2022-11-27 20:26:25 3 Tiny change: 'so simple `int floo' -> 'so simple as `int floo'
en2 Английский 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 Английский TryOmar 2022-11-27 20:20:34 2089 Initial revision (published)