Skip to content

fix: clamp acos argument in Rotation3::euler_angles_ordered to avoid NaN (#1481)#1611

Open
teddytennant wants to merge 1 commit into
dimforge:mainfrom
teddytennant:fix/euler-angles-ordered-clamp-acos-1481
Open

fix: clamp acos argument in Rotation3::euler_angles_ordered to avoid NaN (#1481)#1611
teddytennant wants to merge 1 commit into
dimforge:mainfrom
teddytennant:fix/euler-angles-ordered-clamp-acos-1481

Conversation

@teddytennant

Copy link
Copy Markdown

What

Rotation3::euler_angles_ordered computes the middle Euler angle as angles[1] = o_t.m33.acos(). This clamps o_t.m33 into [-1, 1] before calling acos:

angles[1] = o_t.m33.clamp(-T::one(), T::one()).acos();

Why

For a valid rotation matrix m33 lies in [-1, 1], but floating-point rounding in the chained matrix products o_t = c * self.matrix() * (c.transpose() * r1l) can push m33 slightly outside that range (e.g. -1.0000000000000002 for a near-nadir orientation). acos of an out-of-domain argument returns NaN, which silently poisons the returned angle array.

This mirrors the existing precedent in Matrix::angle, which already clamps the cosine via simd_clamp(-one, one) before calling acos.

Testing

Added a regression test euler_angles_ordered_issue_1481 in tests/geometry/rotation.rs using a unit quaternion whose rotation matrix has m33 just below -1. Before the fix the middle angle comes back as NaN ([2.677945044588987, NaN, 0.0]); after the fix all three angles are finite.

cargo test --features debug,compare,rand,macros --test lib euler_angles_ordered_issue_1481

cargo fmt --check and cargo clippy are both clean (no new warnings introduced by this change).

Fixes #1481.

Floating-point rounding in the chained matrix products computing `o_t`
can push `o_t.m33` slightly outside `[-1, 1]` (e.g. -1.0000000000000002
for a near-nadir orientation), so `acos` returns NaN and silently
poisons the whole returned angle array. Clamp the argument into the
valid range before calling `acos`, mirroring the existing handling in
`Matrix::angle`.

Fixes dimforge#1481.
Copilot AI review requested due to automatic review settings July 8, 2026 16:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

Certain unit quaternions produce invalid rotation matrices

2 participants