Today I learned (that unlike in python), if you mod a negative integer with a natural number, the answer won’t be adjusted to be accurate. Instead, it will return negative mod answers.
In python, -1 % 4
yields 3 (adjusted answer)
But, in golang, -1 % 4
yields -1
To fix it, we can to add the denominator to the final answer to get the accurate mod (but this is manual grunt work).
mod_val = ((numerator) % denominator) + denominator
So, if you want to ‘black-boxify’ it (if you will), you can create an expression so you don’t have to worry if the numerator is negative or positive.
accurate_mod_val = (((numerator) % denominator) + denominator) % denominator
This expression is implementation agnostic, so you can use this anywhere :)
This hack could help programmers gain back a ton of time in dev-loops. Hope you found this helpful!
π βΈ» @TnvMadhav