Skip to content

Arithmetic that differs from Python: % with negatives, ** grouping, round halves, bool as int, e #224

Open

Description

@sspickle

While fixing #170 I ran a 122-expression arithmetic program through Web VPython 3.2 and compared every result with CPython. 107 match. The rest differ. All of these differences are older than #170: the release before it has every one of them.

Expression Web VPython Python Cause
7 % -3 1 -2 % is JavaScript's remainder, which takes the sign of the left operand. Python's takes the sign of the right one.
-7 % 3 -1 2 same
-7.5 % 2 -1.5 0.5 same
a = 7; a %= -3 1 -2 same
2**3**2 64 512 ** groups left to right (RapydScript emits Math.pow(Math.pow(2, 3), 2)). Python groups it right to left.
2**-1**2 0.25 0.5 same
round(2.5), round(-2.5) 3, -3 2, -2 Halves round away from zero. Python rounds them to the even neighbour.
int(True), True + 1 error 1, 2 Booleans aren't treated as the integers 0 and 1.
log(e) error 1 e isn't defined (pi is).
a **= 2 compile error "Unexpected token: operator «=»"

The division-by-a-negative cases from #170, true and floor division, chained comparisons, abs/min/max/sum/pow/sqrt/divmod, range with negative steps, the trig and log functions, and integers up to 2**52 all match Python.

I also found a regression introduced by #170. Fixed with a test in #225. Before #170, the "-" → "-1*" rewrite hid a RapydScript bug: it printed - -x as --x, which in JavaScript is a decrement (wrong answer, and x changes). - -3 didn't compile at all.

Related: #217 (refreshing the embedded RapydScript). I haven't checked which of these upstream RapydScript-NG has fixed; the ** grouping is still the same at upstream 3e69ba8.

The Trinket deploy suite pins these as known differences (PICUP-Physics/trinket-oss#369, vpython-arithmetic.spec.js), so a fix here will show up there.

🤖 Generated with Claude Code

Activity

  1. sspickle commented on Nov 3, 2026

    @sspickle
    CollaboratorAuthor

    Update: the parser fix in #225 also corrects the ** grouping items here. 2**3**2 now gives 512 and 2**-1**2 gives 0.5. That leaves % with negatives, round halves, int(True)/True + 1, e, and a **= 2.

    While testing #225 I found a second #223 regression, now fixed there: -(x+1)**2 came out positive. glowscript.org is rolled back to the version before #223 until #225 is merged.

    🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions