Numeric Comparisons¶
Numbers in PLC code look simple, but their type matters. Integer values behave differently from floating-point values.
Integers¶
Integer comparisons are exact:
xboCounterReached := (inCounter = 10);
This is safe because Int, UInt, DInt, and similar integer types represent whole numbers.
REAL Values¶
REAL values should normally be compared with a tolerance.
_reAbsValue := ABS(reMeasuredValue);
xboValueIsNearZero := (_reAbsValue < 0.001);
Avoid this:
xboValueIsZero := (reMeasuredValue = 0.0);
Analog scaling, filtering, and calculations often produce small rounding differences. Comparing a
REAL to exact zero can create conditions that almost never become true.
Range Checks¶
For analog process values, write comparisons as ranges where possible.
xboTemperatureInRange :=
(reTemperatureDegC >= reMinimumTemperatureDegC)
AND (reTemperatureDegC <= reMaximumTemperatureDegC);
For live control logic, prefer named limits over magic numbers.
xboSpeedNearStandstill := (ABS(reActualSpeedRpm) < reStandstillToleranceRpm);
This tells the next engineer that the tolerance is a design decision, not a random constant.
Numeric Checklist¶
- Use exact equality for integers when exact equality is the real condition.
- Use tolerances for
REALnear-zero and near-target checks. - Put units in variable names when units matter.
- Use named limits and tolerances instead of unexplained constants.
- Be careful when converting between integer telegram values and engineering units.