Skip to content

Fix cosine range reduction for large rotation angles - #1948

Merged
elalish merged 1 commit into
elalish:masterfrom
Afloat16:fix/large-angle-cosine-20261112
Nov 14, 2026
Merged

elalish merged 1 commit into
elalish:masterfrom
Afloat16:fix/large-angle-cosine-20261112

Conversation

@Afloat16

Copy link
Copy Markdown
Contributor

Problem

cosd(x) calculates sind(x + 90). At large finite angles, adding 90 loses precision before range reduction, so sine and cosine no longer form an orthonormal rotation. A rotation can therefore change a solid's volume or a cross section's area.

Change

Reduce the absolute angle modulo 360 before applying the cosine-to-sine phase shift. Preserve exact quadrant boundaries and return NaN for nonfinite input.

Validation

  • Three new regressions fail on the unchanged base: large-angle cosine accuracy, preservation of 3D volume and preservation of 2D area.
  • The quadrant/nonfinite control passes before and after the change.
  • Tests cover positive and negative angles up to the largest finite double, compare against independently reduced angles, and check the sine/cosine norm.
  • All 632 native CTest cases pass after the change, including the C bindings.
  • clang-format and git diff --check pass.

@codecov

codecov Bot commented Nov 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.33%. Comparing base (ca68eb1) to head (14494f0).
⚠️ Report is 4 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1948   +/-   ##
=======================================
  Coverage   92.33%   92.33%           
=======================================
  Files          45       45           
  Lines        9923     9931    +8     
=======================================
+ Hits         9162     9170    +8     
  Misses        761      761           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Afloat16

Copy link
Copy Markdown
Contributor Author

All 44 checks for 14494f0 are now passing, including the Linux, macOS, Windows and WASM matrix, cross-platform determinism, ASan/UBSan, TSAN, and the base-vs-head performance guard.

Validation: CI matrix, TSAN, and formatting. The regression cases cover large positive and negative angles while retaining the existing quadrant-boundary controls.

This is ready for maintainer review. Please let me know if any additional range-reduction cases would help.

@zmerlynn zmerlynn left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked this locally against a long double reference and it looks right to me. The old x + 90.0 rounds whenever the sum crosses a power of two, so the error isn't limited to huge angles. Near 4194304 degrees, sin^2 + cos^2 was off by 6.6e-12, and below 3600 degrees the max error was 3.75e-15. With this change the max error is 1.88e-16 everywhere I tried. Multiples of 90 and 45 still come out exact.

One small thing: I think the isfinite check is redundant, since fmod of an infinity is NaN and sind already returns NaN for that. It's harmless though.

@elalish elalish left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea when this would be a practical problem, but sure, thanks.

@elalish
elalish merged commit e53f4ac into elalish:master Nov 14, 2026
44 checks passed
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.

3 participants