fix quatdist clamp: swapped min/max caused function to always return 0#350
Merged
fredinfinite23 merged 1 commit intocollabora:masterfrom Mar 12, 2026
Merged
Conversation
linmath_max(1., linmath_min(-1, rtn)) always clamps the dot product to 1.0: linmath_min(-1, x) is always ≤ -1 for any x, then linmath_max(1, ≤-1) = 1, so acos(|1.0|) = 0 for every input. Fix: linmath_min(1., linmath_max(-1., rtn)) correctly clamps to [-1, 1]. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Contributor
Author
|
I have a series of property tests I can submit to the project if there's interest in them. |
Collaborator
|
Good catch and you're right. |
Collaborator
and forgot about this, yes please ... more test's always good :) |
rocketmark
pushed a commit
to rocketmark/libsurvive
that referenced
this pull request
Mar 13, 2026
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
quatdistcomputes the angular distance between two quaternions as2 * acos(|dot(q1, q2)|). Theacosdomain requires the dot product clamped to[-1, 1], but the clamp arguments are swapped:linmath_min(-1, rtn)returns a value ≤ −1 for any input.linmath_max(1, ≤-1)then clamps to exactly1.0. Soacos(|1.0|) = 0for every input —quatdistalways returns0regardless of how far apart the quaternions are.Demonstration
For a 90° rotation around Z (
q = [0.707107, 0, 0, 0.707107]) vs identity ([1, 0, 0, 0]):For any input where
dot(q1, q2)is positive,linmath_min(-1, rtn)evaluates to−1, guaranteeing the wrong result. The only input that accidentally produces the correct answer isdot = 0(quaternions exactly 180° apart), where both implementations return2 * acos(0) = π.Impact
No existing upstream callers —
quatdistis defined inredist/linmath.cbut never called from any upstream.cfile. Any downstream code using this function to measure angular separation between poses would silently receive0for every query.Change
One file, one line in
redist/linmath.c:Found via
Property-based testing:
QuatDistKnownAngleinquat_props.cchecks thatquatdist(identity, q_θ) ≈ θfor 10,000 random rotation axes and angles in(0, π). Every trial failed immediately, returning0instead of the expected angle.