Open tingtron opened 8 months ago
I can confirm this is a bug and I can reproduce.
Surprisingly, Python works wrong here:
-17 % 3 1 17 % 3 2 -17 % -3 -2
For reference, these are the results for Python and for GW-BASIC:
GW-BASIC:
? 17 mod 3
2
Ok
? 17 mod -3
2
Ok
? -17 mod 3
-2
Ok
? -17 mod -3
-2
Ok
Python:
>>> 17 % 3
2
>>> 17 % -3
-1
>>> -17 % 3
1
>>> -17 % -3
-2
Some background as to what Python does and why it does that, linked below. TLDR, Python's //
rounds toward negative infinity, and %
then does what it needs to do to ensure the relation holds that divisor*quotient + remainder = dividend
.
https://stackoverflow.com/questions/3883004/how-does-the-modulo-operator-work-on-negative-numbers-in-python#3883019
http://python-history.blogspot.com/2010/08/why-pythons-integer-division-floors.html
Bug report
Problem MOD operator returns wrong results with negative argument.
Steps
Enter
for i=-5 to 3: ? i mod 2; : next
-1 -2 -1 -2 -1 0 1 0 1
-1 0 -1 0 -1 0 1 0 1
Note: Python works as expected:
The problem is in the source
pcbasic\basic\values\numbers.py
:Program This affects an existing program, which calculates coordinate offset.
Notes PC-BASIC version: 2.0.7 Operating system version: Windows 11 64-bit