It is implemented differently across different languages. Some use the mathematical definition (which you are expecting), some do not (C++ is one that does not).
To get a true mathematically accurate modulo operation using the C++ % you can do:
(a % b + b) % b