Writing "IF temperature = 50.0 THEN" often never fires — the PLC is not broken; it is the nature of floating point: binary formats cannot store most decimal fractions, 0.1 + 0.2 does not equal 0.3, and a worked-up 50.0 may really be 49.99999. Equality tests must become tolerance-band comparisons. The sections below explain the mechanism, the correct coding, and the engineering alternatives.
PLC floating point is mostly single precision (32-bit REAL, about 7 significant digits). The decimal 0.1 is an infinite repeating fraction in binary and is stored as an approximation; repeated additions accumulate error; even operation order changes the result. So a computed "50.0" may really be 49.9999976, and the equality test against the constant 50.0 fails.
| Operation | Theory | Floating-point reality | = test |
|---|---|---|---|
| 0.1 × 3 | 0.3 | 0.30000001… | Fails |
| 0.1 added 10 times | 1.0 | 0.99999989… | Fails |
| 100.0 − 99.99 | 0.01 | 0.010000005… | Fails |
Set the tolerance ε by engineering meaning, not mathematics: "reached 50 °C" allows 0.5 °C; "positioned at 100.000 mm" allows 0.005 mm (half the tolerance band); takt statistics allow 1 ms. ε is "how close is close enough for me" — take it straight from the process tolerance and the comparison becomes both reliable and semantically correct. Wrap it in an EQ_F(A, B, E) function block reused project-wide, so ε never varies call to call.
| Approach | Method | Fits |
|---|---|---|
| Fixed-point integers | Store 50.00 mm as the integer 5000 (×100), all-integer arithmetic | Limited digits, no wide-range multiply/divide — the sturdiest |
| Band tests | IF value ≥ lower AND value < upper | Stages and bands — naturally not equality |
| Position-done signals | Judge arrival by the machine's sensor, not numeric equality | Position control's first choice — a physical signal beats arithmetic |
Fixed-point integers are especially recommended for money, counts and calibration factors where "one bit off is not acceptable": keep everything integer internally (in cents, microns) and convert to floating point only at display/comms boundaries. Integer add, subtract and multiply have no precision issue (division: watch quotients and remainders) — the floating-point comparison problem is bypassed entirely.
① Avoid big-eats-small: 1e7 + 0.01 − 1e7 loses the small number — never let one accumulator span huge magnitude ranges; ② replace long running sums with pairwise sums or periodic "recalibration" (recompute the total from the parts to cancel drift); ③ comparisons, sorting and table matching all use ε — one uniform "close enough" semantics; ④ separate displayed values from stored values: store full precision, round only for display, and never feed the rounded value back into control comparisons; ⑤ floating-point rounding differs slightly across platforms — cross-platform reconciliation keeps a tolerance and never assumes bit-equality.
| Symptom | Cause | Fix |
|---|---|---|
| "Reached setpoint" never triggers | Direct = on floats | Tolerance-band comparison |
| Accumulator drifts further and further | Floating-point summation error | Integer accumulation or periodic recalibration |
| Two machines disagree on the same data | Bit-equality assumed across rounding | Reconcile with tolerance |
| Repeatability poor | In-position judged by numeric equality | Use the position-done sensor |
| Display and control disagree | Rounded display values fed into control | Separate display from storage |
The floating-point issue is that "mathematical equality" and "engineering equality" differ: in engineering, 49.999 and 50.0 are equal. Write the process tolerance into the comparison (tolerance bands), or remove precision from the equation with integers, and floating point retires to its two honest jobs: display and wide-range calculation.
Format and arithmetic behaviour follow IEEE 754 (single-precision REAL representation and rounding); PLC floating-point instructions (EADD, ECMP etc.) follow the programming manual; tolerance-band design follows process tolerances and machine accuracy documents; in-position judgement safety follows GB 5226.1 / IEC 60204-1 (arrival must not rest on numeric computation alone).
Two team rules: floating-point equality only through the unified tolerance-band block; precise accumulation always in fixed-point integers. With both, precision-class bugs leave the project for good.