Skip to content

Clamp ​

Constrains x to the range [lower, upper].

Clamp(x, lower, upper)x if it's already in [lower,upper][\mathrm{lower}, \mathrm{upper}], else the nearer bound.
Clamp(x)x clamped to the default range [−1,1][-1, 1], Wolfram's Clip[x].
Clamp(x, lower, upper, vLower, vUpper)Wolfram's Clip[x, {lower, upper}, {vLower, vUpper}]: a replacement value outside the range, rather than the nearer bound.
Typed signatures
TypePackageApplies
(real | signed_infinity, real | signed_infinity, real | signed_infinity) -> real | signed_infinitycompute-enginealways
(any, any?, any?, any?, any?) -> anycombinatoricsreplaces compute-enginealways
(any, any?, any?, any?, any?) -> anystructuresreplaces combinatoricsalways

Domain: Arithmetic

Details
  • Constrains a value to [lower,upper][\mathrm{lower}, \mathrm{upper}]: below lower it returns lower, above upper it returns upper, otherwise x unchanged.
  • A 1-argument Clamp(x)\mathrm{Clamp}(x) defaults to [−1,1][-1, 1], matching Wolfram's Clip[x].
  • Idempotent: clamping an already-clamped value changes nothing.
  • Doesn't validate that lower ≤\le upper; with the bounds swapped it just falls through whichever comparison fires first.

Examples

3 more kept as data, checked against the oracles
Basic #
live ↗
InClamp(5,0,3)\mathrm{Clamp}(5, 0, 3)Out33
live ↗
InClamp(−1,0,3)\mathrm{Clamp}(-1, 0, 3)Out00

Values already inside the range pass through unchanged

live ↗
InClamp(2,0,3)\mathrm{Clamp}(2, 0, 3)Out22
live ↗
InClamp(2,0,3,−1,10)\mathrm{Clamp}(2, 0, 3, -1, 10)Out22
Scope #

A 1-argument Clamp(x)\mathrm{Clamp}(x) clamps to the default range [−1,1][-1, 1]

live ↗
InClamp(1.5)\mathrm{Clamp}(1.5)Out11

Threads element-wise over a list

live ↗
InClamp([−2,0.5,3],0,1)\mathrm{Clamp}(\bigl\lbrack-2, 0.5, 3\bigr\rbrack, 0, 1)Out[0,0.5,1]\bigl\lbrack0, 0.5, 1\bigr\rbrack

Exact numeric constants are compared numerically

live ↗
InClamp(π,0,3)\mathrm{Clamp}(π, 0, 3)Out33
live ↗
InClamp(72,1,3)\mathrm{Clamp}(\frac{7}{2}, 1, 3)Out33

Infinities clamp to the nearer bound

live ↗
InClamp(∞,0,3)\mathrm{Clamp}(∞, 0, 3)Out33

Wolfram's Clip[x,{min,max},{vmin,vmax}]\mathrm{Clip}[x, \{min, max\}, \{v_{min}, v_{max}\}] returns replacement values outside the range -- here 10 above it

live ↗
InClamp(5,0,3,−1,10)\mathrm{Clamp}(5, 0, 3, -1, 10)Out1010
Applications #

Capping a percentage-like value at 100

live ↗
InClamp(112,0,100)\mathrm{Clamp}(112, 0, 100)Out100100
Properties #

Idempotent: clamping an already-clamped value changes nothing

live ↗
InClamp(Clamp(5,0,3),0,3)=Clamp(5,0,3)\mathrm{Clamp}(\mathrm{Clamp}(5, 0, 3), 0, 3)=\mathrm{Clamp}(5, 0, 3)OutTrue\mathrm{True}
Possible issues #

No validation that lower ≤ upper -- with the bounds swapped, this just falls through the upper-bound check

live ↗
InClamp(5,3,0)\mathrm{Clamp}(5, 3, 0)Out00

A symbolic value is left unevaluated rather than assumed to already lie in range

live ↗
InClamp(x,0,3)\mathrm{Clamp}(x, 0, 3)OutClamp(x,0,3)\mathrm{Clamp}(x, 0, 3)
About
Examples17
Checked againstWolfram Language 17 agree
CrosswalkWikipediaWikidata

See also: Min, Max