Comparison operators

Operators

OperatorMeaningOperand typesResult
=EqualANY_NUM, BOOL, STRING, TIME, … (any) — both sameBOOL
<>Not equalas aboveBOOL
<Less thanANY_NUM, STRING, TIME, DATE, TODBOOL
<=Less than or equalas aboveBOOL
>Greater thanas aboveBOOL
>=Greater than or equalas aboveBOOL

= and <> work on every comparable type; </<=/>/>= require an ordering, so they’re undefined on BOOL and on custom enumerated types without an ordering hint.

Type-matching rule

Both operands must share the same IEC type. matiec rejects implicit promotion (see llm_signals). Always wrap one side in the explicit conversion:

(* INT vs UINT — fails *)
(* IF iA = uB THEN ... END_IF; *)

(* INT vs UINT — works after promotion *)
IF iA = UINT_TO_INT(uB) THEN ... END_IF;

(* INT vs DINT — promote INT to DINT *)
IF INT_TO_DINT(iA) >= dwBig THEN ... END_IF;

REAL comparison hazard

REAL and LREAL are IEEE-754 floats. Direct equality is flaky:

(* Don't do this *)
IF rValue = 0.1 + 0.2 THEN ... END_IF;     (* FALSE in IEEE-754 *)

(* Do this *)
IF ABS(rValue - 0.3) < 0.0001 THEN ... END_IF;

For comparisons that should treat “equal within tolerance” as equal, use ABS(a - b) < epsilon. Pick epsilon based on the magnitude of the values: a temperature in degrees Celsius can tolerate 0.01; a normalised position in [0..1] should use something smaller.

STRING comparison

String operators compare lexicographically by character ordinal. 'apple' < 'banana' is TRUE. Comparison is case-sensitive — 'Apple' = 'apple' is FALSE. For case-insensitive comparison, normalise both sides first (IEC has no built-in tolower; use a per-project FUN).

IEC reference

IEC 61131-3 third edition (2013), clause 6.6.1 Table 55.

matiec conformance

matiec implements all six comparison operators correctly. The type-strictness rule is enforced more aggressively than some other vendor dialects — code ported from those tools often needs explicit conversions inserted at every comparison site.