Re:Ask HN: Average coder at dead-end. What now?
by chipsy on Monday June 21, 2010 @02:33AM
(#1448166)
The main danger with using the mod operator is that you can get an off-by-one if you aren't in the habit of using it and forget the exact definition. If that's in doubt, rolling your own thing with a division or a while/if makes for a stronger guarantee of success on the first try.
In an interview you could explain your reasoning for such a detour as well, which might work out better than just "knowing the answer."
Re:Ask HN: Average coder at dead-end. What now?
by robinduckett on Monday June 21, 2010 @05:25AM
(#1448340)
I used the old int(i) == i (or even, int(i/2) == i/2) way before I knew about the mod operator.
Outside of the mod operator, you could always use a combo of floor() and ceiling() (which I assume most languages have in a standard library). e.g.:
Or even more basic: