microsoft / microsoft/pxt-microbit
[python] mod operator is modulus in python, not remainder
Nobody has claimed this yet.
- 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:
- Go to https://makecode.microbit.org/_LhfXu3iHigXL
- see console output
- go to console / terminal window, start python repl (
python) - 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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