2024-03-03

Remainder/Modulo operators in programming languages

While doing my programming assignment in Go, for some question I had to do something like (x - 1) % 5 but I didn’t realize that this would cause me problems. Luckily the test case provided to use opened my eyes. So, when x is 0 the expression becomes -1 % 5 and the outcome of this value changes depending on the programming language design. In the case of Go, -1 % 5 would give -1 as I was using this value for accessing an array value it didn’t end well. But the same expression will give you 4 in python. in programming languages, the % is called either “Remainder” operator or “Modulo” operator by different people.

References