Skip to content

pymath.normalize_inplace

Daniel Flassig edited this page Jul 17, 2026 · 1 revision

In-place variant of pymath.normalize: scales the vector to unit length by overwriting its entries instead of allocating a new table.

v, len = pymath.normalize_inplace(v [, threshold])
Parameter Type Description
v vector A vector of any dimension; receives the normalized components.
threshold number (optional) Non-negative length at or below which the vector counts as too short. Default: an implementation defined tolerance.

Return value

Type Description
v The same table that was passed in, now of unit length (returned for convenience) — or nil if the length of v is not above the threshold.
len The original length of v before normalization (only on success).

Notes:

  • On failure (nil returned) the entries of v are left unchanged.
  • nil holes in v are filled with 0.0, so the vector is fully dense afterwards (also on failure).
  • All other guarantees of pymath.normalize apply: overflow-safe length computation, nil for non-finite input, a threshold of 0 rejects exactly the zero vector, and the result components lie in [-1, 1].

Example:

local dir = {3, 4}
local _, len = pymath.normalize_inplace(dir)
-- dir == {0.6, 0.8}, len == 5.0

-- the nil return makes degenerate input easy to catch
if not pymath.normalize_inplace(axis) then
    axis = {0, 0, 1}   -- fall back to a default axis
end

Version Support:

Minimum PYTHA Version: V27

See also:

pymath, pymath.normalize, pymath.length

Clone this wiki locally