Skip to content

pymath.clamp

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

Clamps a number into an interval, or a vector component-wise.

r = pymath.clamp(t)             -- clamp into [0, 1]
r = pymath.clamp(t, min, max)   -- clamp into [min, max]
Parameter Type Description
t number or vector The value to clamp.
min number, vector or nil Lower bound. A number applies to every component, a vector bounds each component individually. nil (as the argument, or as a single vector entry) means no lower bound.
max number, vector or nil Upper bound, like min.

Return value

Type Description
r The clamped number, or a new vector with each component clamped.

Notes:

  • With a single argument, the value is clamped into [0, 1].
  • For a scalar t the bounds must be numbers (or nil).
  • t is not modified.

Example:

pymath.clamp(1.4)                            -- 1.0
pymath.clamp(-0.2)                           -- 0.0
pymath.clamp({-1, 0.5, 2})                   -- {0, 0.5, 1}

pymath.clamp(12, nil, 10)                    -- 10   (no lower bound)
pymath.clamp({1, 5, 9}, 2, 8)                -- {2, 5, 8}
pymath.clamp({1, 5, 9}, {2, nil, nil}, nil)  -- {2, 5, 9}

Version Support:

Minimum PYTHA Version: V27

See also:

pymath, pymath.smooth_step, pymath.interpolate_linear

Clone this wiki locally