Hacker News new | ask | show | jobs
by sensanaty 750 days ago
Yeah I know how to implement FizzBuzz since it's such a meme, but I've basically never used the mod operator in real code. Maybe it comes up in more math-y code I suppose, but for most backend/frontend/SQL code I've never reached for it.
4 comments

  for (…) {
    heavy_op();
    if (i % 100 == 0) {
      printf("not dead");
    }
Classic
More mathy code like checking if a number is even or splitting a total number of seconds into minutes and seconds?
I’ve used it for coloring alternating lines differently in UI code, and as a lazy way to log only every so many times some loop runs.

I only know it well because it was covered near the beginning of one of the first programming books I picked up (on Perl 5) and it stuck with me because it seemed wild to me that they had an operator for that.

Depends on the application. I've written accounting software that makes use of it, along with heavy use of floor() and ceil(), including in SQL.