I've learnt to always check that when dealing with potential negative numbers in new languages, and sometimes in ones 8 know already, too just to be safe. Though I must admit I sometimes take the lazy route of (a%n+n)%n since it's faster to write.
I'm a programmer who likes games, and wants to make games for a living. At the same time, there are some really cool non-game things I want to work on -- especially if they use C or C++.
As neat as that one-liner is, I'm not a huge fan of it. The "correct" function is only a few lines in any language, and makes it explicitly clear what you're trying to do.
...that said, I have used it more than a few times when I just need something working correctly, and I'd be surprised if the compiler couldn't optimize it into something better.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I've learnt to always check that when dealing with potential negative numbers in new languages, and sometimes in ones 8 know already, too just to be safe. Though I must admit I sometimes take the lazy route of
(a%n+n)%n
since it's faster to write.As neat as that one-liner is, I'm not a huge fan of it. The "correct" function is only a few lines in any language, and makes it explicitly clear what you're trying to do.
...that said, I have used it more than a few times when I just need something working correctly, and I'd be surprised if the compiler couldn't optimize it into something better.