LIMIT

Clamps a value to a specified range.

Signature

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

IEC 61131-3 declares the inputs as any elementary type (ANY_ELEMENTARY). IronPLC compares integers, reals, bit strings, BOOL, times and dates, and strings: FALSE is less than TRUE, a time or date is ordered by the duration or instant it stands for, and strings are ordered as < orders them. An enumeration, a structure or an array is not elementary, and is an error (P4026). Every input is evaluated. Of strings, the function is supported where it is the whole value assigned, and each input is a literal or a variable. Using it inside an expression, such as an argument of CONCAT, or with inputs that call a function or use a computed array index, is not yet supported.

The return type is the type that every compared or selected input widens to. The compiler converts a narrower input to that type first, so an INT and a LINT give a LINT. When no input’s type holds every other input, as with a DINT and a UDINT, the call is an error (P4049).

Inputs

Name

Type

Description

MN

ANY_ELEMENTARY

Lower bound of the range.

IN

ANY_ELEMENTARY

Value to clamp.

MX

ANY_ELEMENTARY

Upper bound of the range.

Outputs

Name

Type

Description

Return value

ANY_ELEMENTARY

IN clamped to [MN, MX].

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

LIMIT is defined as MIN(MAX(IN, MN), MX), so when MN > MX the result is MX.

Example

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

A period kept within its range:

period := LIMIT(T#100ms, T#10s, T#5s);    (* period = T#5s *)

See Also

  • MAX — maximum of two values

  • MIN — minimum of two values

  • SEL — binary selection

References