microsoft / microsoft/pxt-microbit

[python] mod operator is modulus in python, not remainder

Open
#2,886 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

P2 python
Dominant language
TypeScript
Stars
804
Forks
721
Avg merge
2d 9h
Merged PRs (30d)
2

Description

Describe the bug

mod operator in python is modulus, not remainder; this means it handles negatives differently from javascript.

-1 % 4

in python is 3, in javascript / c++ is -1

To Reproduce
Steps to reproduce the behavior:

  1. Go to https://makecode.microbit.org/_LhfXu3iHigXL
  2. see console output
  3. go to console / terminal window, start python repl (python)
  4. print(-1 % 4) in window; see output is different

Expected behavior

Maybe need a helper function for mod to convert to in python or something? It's a bit of an edge case.

Remembered they were different in python when responding to https://forum.makecode.com/t/remainder-modulus-operator-returning-negative-values-when-it-shouldnt/1789

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the MakeCode program at https://makecode.microbit.org/_LhfXu3iHigXL and compare its console output for -1 % 4 with a Python REPL. Trace how the modulo operator is translated and determine the intended behavior for negative operands; the issue is done when the output matches that behavior and the edge case is verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, python, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.