Skip to content
This repository was archived by the owner on Jun 10, 2026. It is now read-only.

fix: reduce sin/cos angle modulo 360 to avoid precision loss#5473

Open
Chessing234 wants to merge 1 commit into
scratchfoundation:developfrom
Chessing234:fix/trig-precision-large-angles
Open

fix: reduce sin/cos angle modulo 360 to avoid precision loss#5473
Chessing234 wants to merge 1 commit into
scratchfoundation:developfrom
Chessing234:fix/trig-precision-large-angles

Conversation

@Chessing234

Copy link
Copy Markdown

Resolves

Resolves #2199

Proposed Changes

operator_mathop's sin and cos cases convert the input angle straight to radians (Math.PI * n / 180). For very large angles this argument accumulates floating-point error, so the result drifts away from the correct periodic value. For example cos(36000000000090) returns 0.000025074 instead of 0, even though the angle is well below Number.MAX_SAFE_INTEGER.

This change reduces the angle modulo 360 before converting to radians. Trigonometric functions are periodic with period 360°, so this is mathematically equivalent for all inputs while keeping the radian argument small and precise. MathUtil.tan already does exactly this (angle = angle % 360), so tan was unaffected — this brings sin/cos in line with it.

Reason for Changes

Since the functions are periodic, they should return the same value for angles that differ by a multiple of 360. Without modular reduction the largest angles silently produce wrong results.

Test Coverage

Added two assertions to the existing mathop unit test verifying that large angles reduce correctly:

t.strictEqual(blocks.mathop({OPERATOR: 'sin', NUM: 36000000000090}), 1);
t.strictEqual(blocks.mathop({OPERATOR: 'cos', NUM: 36000000000090}), 0);

Both fail before the change and pass after. The existing trig assertions (sin 1/90/180, cos 1/180, etc.) are unchanged, confirming no regression for normal angles. npm run test:unit -- test/unit/blocks_operators.js passes and ESLint is clean.

Math.sin and Math.cos lose precision for very large angle inputs because
the radian argument (PI * n / 180) accumulates floating-point error. Since
the trig functions are periodic with period 360 degrees, reduce the angle
modulo 360 before converting to radians, matching the handling already
done in MathUtil.tan.

Fixes scratchfoundation#2199
@github-actions

github-actions Bot commented Jun 1, 2026

Copy link
Copy Markdown


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


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


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Trig functions lose precision at large angles

1 participant