Recent Entries 2
- pattern critical 112d agoFast ceiling of an integer division in C / C++Given integer values `x` and `y`, C and C++ both return as the quotient `q = x/y` the floor of the floating point equivalent. I'm interested in a method of returning the ceiling instead. For example, `ceil(10/5)=2` and `ceil(11/5)=3`. The obvious approach involves something like: ``` q = x / y; if (q * y < x) ++q; ``` This requires an extra comparison and multiplication; and other methods I've seen (used in fact) involve casting as a `float` or `double`. Is there a more direct method that avoids the additional multiplication (or a second division) and branch, and that also avoids casting as a floating point number?
- snippet critical 112d agoHow to perform an integer division, and separately get the remainder, in JavaScriptIn JavaScript, how do I get: - The whole number of times a given integer goes into another? - The remainder?