IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

clamp

def clamp(val: Int, lower_bound: Int, upper_bound: Int) -> Int

Clamps the integer value vector to be in a certain range.

Args:

  • val (Int): The value to clamp.
  • lower_bound (Int): Minimum of the range to clamp to.
  • upper_bound (Int): Maximum of the range to clamp to.

Returns:

Int: An integer clamped to be within lower_bound and upper_bound.

def clamp[dtype: DType, width: SIMDSize, //](val: SIMD[dtype, width], lower_bound: SIMD[dtype, width], upper_bound: SIMD[dtype, width]) -> SIMD[dtype, width]

Clamps the values in a SIMD vector to be in a certain range.

Clamp cuts values in the input SIMD vector off at the upper bound and lower bound values. For example, SIMD vector [0, 1, 2, 3] clamped to a lower bound of 1 and an upper bound of 2 would return [1, 1, 2, 2].

Parameters:

  • dtype (DType): The dtype of the input and output SIMD vector.
  • width (SIMDSize): The width of the input and output SIMD vector.

Args:

Returns:

SIMD[dtype, width]: A SIMD vector containing x clamped to be within lower_bound and upper_bound.