Skip to content

Math library accuracy + performance improvements#512

Open
ecc521 wants to merge 1 commit intoscratchfoundation:developfrom
ecc521:no-stringified-math-ops
Open

Math library accuracy + performance improvements#512
ecc521 wants to merge 1 commit intoscratchfoundation:developfrom
ecc521:no-stringified-math-ops

Conversation

@ecc521
Copy link
Copy Markdown

@ecc521 ecc521 commented Apr 6, 2026

This corresponds to a 2019 PR that should have been merged a log time ago. It addresses two performance & accuracy issues in the previous scratch-vm repository that remain in this repository (2198 and 2199) .

scratch3_operators.js & math-util.js: Floating-point bounding for trigonometric operations (sin/cos/tan) was relying on parseFloat(..., 10). This is replaced with scaled floating-point arithmetic Math.round(... * 1e10) / 1e10. Operators are additionally bounded by % 360 to prevent IEEE-754 precision loss at large scale values.

  • This is pretty much a no-brainer in my book. It's better math + performance improvements. The changes shouldn't break any existing projects since only large angles are affected here (the error doesn't exceed 0.01% until 66 trillion degrees).

cast.js: The Cast.isInt boundary check utilized parseInt(val, 10). For stringified values in scientific notation (e.g. 1e22), parseInt parsed out "1", erroneously evaluating the float as non-integer. Modulo constraint (val % 1 === 0) evaluates correctly without stringification. This change is practically irrelevant but technically a bug fix.

Verification
Unit parity with Scratch native tests verified (npm run tap:unit).
blocks_operators and blocks_operators_infinity tests fully pass, ensuring proper edge case constraints for non-standard numerics (NaN, Infinity, -0).

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 6, 2026

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@ecc521
Copy link
Copy Markdown
Author

ecc521 commented Apr 6, 2026

I have read the CLA Document and I hereby sign the CLA

@ecc521 ecc521 force-pushed the no-stringified-math-ops branch from 6d3a22c to 8694f87 Compare April 6, 2026 04:15
@ecc521 ecc521 marked this pull request as ready for review April 6, 2026 04:20
@ecc521 ecc521 requested a review from a team as a code owner April 6, 2026 04:20
@ecc521 ecc521 force-pushed the no-stringified-math-ops branch from 30e30aa to 402deef Compare April 6, 2026 04:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant