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
Open
fix: reduce sin/cos angle modulo 360 to avoid precision loss#5473Chessing234 wants to merge 1 commit into
Chessing234 wants to merge 1 commit into
Conversation
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
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves
Resolves #2199
Proposed Changes
operator_mathop'ssinandcoscases 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 examplecos(36000000000090)returns0.000025074instead of0, even though the angle is well belowNumber.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.tanalready does exactly this (angle = angle % 360), sotanwas unaffected — this bringssin/cosin 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
mathopunit test verifying that large angles reduce correctly: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.jspasses and ESLint is clean.