Repository navigation
Stabilize Vector.magnitude and normalize for extreme components - #29
Merged
Merged
Conversation
Scale by the largest absolute component before summing squares so normalize returns a unit vector for very large or very small inputs instead of collapsing to zero. Add VectorSuite coverage for both cases. Co-authored-by: Barry G Becker <barrybecker4@yahoo.com>
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.
Fixes #17
Vector.magnitudepreviously computedsqrt(sum of squares)directly. When components were around1e200, the squares overflowed to infinity andnormalizereturned the zero vector instead of a unit vector. Very small components (around1e-200) underflowed in the same way and also collapsed to zero.Behavior
magnitudenow scales by the largest absolute component before summing squares, so ordinary values stay correct and extreme magnitudes remain finite and usable.normalizeof a non-zero vector with extreme component sizes returns an approximate unit vector (for example(1e200, 1e200)→ about(1/√2, 1/√2)), not(0, 0).normalizeis unchanged.Tests
VectorSuitenow covers large and tiny equal-component cases for bothnormalizeandmagnitude, in addition to the existing ordinary-value, zero-vector, equality, andnormalizedDotcoverage.Downstream
bb4callers oflinear.Vector(mainly gradient tooling inbb4-optimization) use ordinary-scale values; nothing found that depends on the old overflow-to-zero result.