'Double modulo' operator

Sorry for bumping this thread after more than two years, but it's because of a recent related post and my concurrence with the people upthread who says essentially:

  • I’ve never actually wanted a negative return value from %. It does what I need only as long as the lhs is positive, but not when it's negative. In fact, I wouldn't be surprised if many uses of % are in situations where only a positive lhs has been considered, and if the use case was generalized to work also for a negative lhs, the code wouldn't work as intended unless % is replaced with a "true modulo" operation.

  • I've had to write a true mod function enough times across different projects (circular buffer indexing, angular arithmetic, periodic boundary conditions, subdivision of coordinate grids in games or graphics apps, etc.) that I would absolutely support its inclusion in the Standard Library.

I also think that this thread has arrived at some important conclusions (especially the ones quoted above), so it seems better to continue this discussion here rather than in a new thread. And there might be one more thing that needs to be sorted out before a proposal:

@scanon: According to this Wikipedia article, especially the top right diagram, the requested remainder seems to be the one of a euclidean rather than floored division.

Also, from the related recent post I mentioned above:

Could you please clarify, to make sure we have the correct background for a potential proposal, is it floored or euclidean?

3 Likes