Skip to content

Stabilize Vector.magnitude and normalize for extreme components - #29

Merged
barrybecker4 merged 1 commit into
masterfrom
cursor/vector-extreme-magnitude-dc34
Nov 8, 2026
Merged

barrybecker4 merged 1 commit into
masterfrom
cursor/vector-extreme-magnitude-dc34

Conversation

@barrybecker4

Copy link
Copy Markdown
Member

Fixes #17

Vector.magnitude previously computed sqrt(sum of squares) directly. When components were around 1e200, the squares overflowed to infinity and normalize returned the zero vector instead of a unit vector. Very small components (around 1e-200) underflowed in the same way and also collapsed to zero.

Behavior

  • magnitude now scales by the largest absolute component before summing squares, so ordinary values stay correct and extreme magnitudes remain finite and usable.
  • normalize of 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).
  • Zero-vector normalize is unchanged.

Tests

VectorSuite now covers large and tiny equal-component cases for both normalize and magnitude, in addition to the existing ordinary-value, zero-vector, equality, and normalizedDot coverage.

Downstream bb4 callers of linear.Vector (mainly gradient tooling in bb4-optimization) use ordinary-scale values; nothing found that depends on the old overflow-to-zero result.

Open in Web Open in Cursor 

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>
@barrybecker4
barrybecker4 merged commit e9598fa into master Nov 8, 2026
1 check passed
@barrybecker4
barrybecker4 deleted the cursor/vector-extreme-magnitude-dc34 branch November 8, 2026 14:23
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.

Add numerical stability tests for Vector.magnitude and normalize

2 participants