LIMIT

Clamps a value to a specified range.

Signature

    ┌─────────┐
MN ─┤         │
IN ─┤  LIMIT  ├─ OUT
MX ─┤         │
    └─────────┘
FUNCTION LIMIT : ANY
  VAR_INPUT
    MN : ANY;
    IN : ANY;
    MX : ANY;
  END_VAR
END_FUNCTION

The return type matches the input type. LIMIT accepts SINT, INT, DINT, LINT, USINT, UINT, UDINT, ULINT, REAL, LREAL. All three inputs must share the same type.

Inputs

Name

Type

Description

MN

ANY_MAGNITUDE

Lower bound of the range.

IN

ANY_MAGNITUDE

Value to clamp.

MX

ANY_MAGNITUDE

Upper bound of the range.

Outputs

Name

Type

Description

Return value

ANY_MAGNITUDE

IN clamped to [MN, MX]. Same type as the inputs.

Description

LIMIT(MN, IN, MX) clamps IN to the range [MN, MX]. The function returns:

  • MN if IN < MN

  • MX if IN > MX

  • IN otherwise

The behavior is undefined if MN > MX.

Example

result := LIMIT(0, 50, 100);    (* result = 50 *)
result := LIMIT(0, -10, 100);   (* result = 0 *)
result := LIMIT(0, 200, 100);   (* result = 100 *)

See Also

  • MAX — maximum of two values

  • MIN — minimum of two values

  • SEL — binary selection

References