Skip to content

MOD operator returns wrong results for negative argument #231

@tingtron

Description

@tingtron

Bug report

Problem
MOD operator returns wrong results with negative argument.

Steps

  1. Run PC-BASIC
  2. Enter the following and press Enter
    for i=-5 to 3: ? i mod 2; : next
  3. Observed: -1 -2 -1 -2 -1 0 1 0 1
  4. Expected: -1 0 -1 0 -1 0 1 0 1
  5. Compare with GW-BASIC from MS-DOS (in DOSBox)

Note: Python works as expected:

>>> [x % 2 for x in range(-5,3)]
[1, 0, 1, 0, 1, 0, 1, 0]

The problem is in the source pcbasic\basic\values\numbers.py:

    if dividend < 0 or mod < 0:
      mod -= divisor

It should be

      mod = -mod

Program
This affects an existing program, which calculates coordinate offset.

Notes
PC-BASIC version: 2.0.7
Operating system version: Windows 11 64-bit

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions