1

I'm working on a project where we're performing monthly date arithmetic: adding $n$ months to an initial month $m_1$ to obtain a resulting value of 1 through 12.

The standard modulo operation, $m_2$ = ($m_1 + n$) mod 12, will return the remainder $m_2$ = 0 rather than 12 if $m_1 + n$ = 12.

It's easy to redefine "0" as "12" with an if-then function in any programming language. But is there a more elegant way to perform a modulo operation that returns values of 1 - 12 rather than 0 - 11?

RobertF
  • 197
  • 2
    Do you consider $m_2 = ((m_1 + n - 1) \text{ mod } 12) + 1$ to be a "more elegant" method? – John Omielan May 20 '22 at 18:16
  • 1
    @JohnOmielan Yes, thank you, I was looking for a pure mathematical solution. I'll check which method takes less time to compute, the if-then statement or your more elegant approach. :-) – RobertF May 20 '22 at 20:52

0 Answers0