While I'll admit I'm a bit sketchy on modulo operations of negative numbers, I do know that
-2 mod 50 = 48
in just about every online modulo calculator as well as LibreOffice Calc:
=MOD(-2,50)
And Python:
-2 % 50
This number (48
) conveniently is the answer I need for a function I am writing in a MySQL procedure. However, in MySQL:
SELECT MOD(-2,50)
Gives me -2
as a result.
What is the reason for this, and how do I get the result I am looking for in MySQL?
For Posterity:
As of 2018 the following languages provide these different results from -2 % 50
:
48
- Python
- R
- Google Calculator
- Google Sheets
- LibreOffice Calc
- Ubuntu Calculator
-2
- Javascript
- MySQL
- Microsoft Calculator
- PHP
And as pointed out in the comments, a more exhaustive list is here: https://en.wikipedia.org/wiki/Modulo_operation