-
Notifications
You must be signed in to change notification settings - Fork 18
pymath.normalize
Daniel Flassig edited this page Jul 17, 2026
·
2 revisions
Returns the given vector scaled to unit length, or nil if the vector is too short to normalize reliably.
u, len = pymath.normalize(v [, threshold])| Parameter | Type | Description |
|---|---|---|
v |
vector | A vector of any dimension. |
threshold |
number (optional)
|
Non-negative length at or below which the vector counts as too short. Default: an implementation defined tolerance. |
| Type | Description |
|---|---|
u |
A new vector `v / |
len |
The original length of v (only on success). |
-
vis not modified. - Use the
nilreturn to catch degenerate input (e.g. a zero direction vector) instead of dividing by a near-zero length yourself. - The length computation is overflow-safe, like
pymath.length.nilis also returned if the length overflows or the input is not finite. - The components of the result are guaranteed to lie in
[-1, 1]even in floating point, so they can be passed straight toACOS/ASIN. - A
thresholdof0is allowed and rejects exactly the zero vector: even vectors of subnormal length are normalized correctly
local dir, len = pymath.normalize({3, 4})
-- dir == {0.6, 0.8}, len == 5.0
pymath.normalize({0, 0, 0})
-- nil -- the zero vector cannot be normalized
pymath.normalize({1e-9, 0, 0}, 1e-6)
-- nil -- below the explicit thresholdMinimum PYTHA Version: V27
pymath, pymath.normalize_inplace, pymath.length, pymath.cross