MOULDLKnowledge BaseElectrical Knowledge Base3D Model DownloadsMembership
中文版Model Store »
Knowledge Base › Electrical Knowledge Base › Why is comparing floating-point values for equality unreliable, and how do you handle precision?
HomeModels
Table of contents

On this page

PLC & Programming (100 articles)

Quick Reference

  • PLC & Programming
  • Electrical & Electronic Basics (monthly)(每月更新中)
  • Electrical Control & Drawings (monthly)(每月更新中)
  • Drives & Actuators (monthly)(每月更新中)
  • Sensors & Detection (monthly)(每月更新中)
  • Industrial Communication & Networks (monthly)(每月更新中)
  • HMI & Host Systems (monthly)(每月更新中)
  • Industrial Robots & Motion Control (monthly)(每月更新中)
  • Mechatronic System Integration (monthly)(每月更新中)
  • Fault Diagnosis & Standards (monthly)(每月更新中)

Why is comparing floating-point values for equality unreliable, and how do you handle precision?

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.

1. The mechanism: binary cannot hold decimal fractions

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.

OperationTheoryFloating-point reality= test
0.1 × 30.30.30000001…Fails
0.1 added 10 times1.00.99999989…Fails
100.0 − 99.990.010.010000005…Fails

2. The correct coding: tolerance-band comparison

IF ABS(actual − target) < tolerance THEN treat as equal

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.

3. Three engineering alternatives

ApproachMethodFits
Fixed-point integersStore 50.00 mm as the integer 5000 (×100), all-integer arithmeticLimited digits, no wide-range multiply/divide — the sturdiest
Band testsIF value ≥ lower AND value < upperStages and bands — naturally not equality
Position-done signalsJudge arrival by the machine's sensor, not numeric equalityPosition 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.

4. Care points for floating-point arithmetic itself

① 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.

5. Common errors

SymptomCauseFix
"Reached setpoint" never triggersDirect = on floatsTolerance-band comparison
Accumulator drifts further and furtherFloating-point summation errorInteger accumulation or periodic recalibration
Two machines disagree on the same dataBit-equality assumed across roundingReconcile with tolerance
Repeatability poorIn-position judged by numeric equalityUse the position-done sensor
Display and control disagreeRounded display values fed into controlSeparate display from storage

6. Notes

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.

7. References

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).

8. Cautions

⚠️ Three traps to avoid:
① ε differing from place to place — the same "equal" means five different things across the program;
② Control comparisons fed by rounded display values — control jitters with the display;
③ A floating-point accumulator spanning huge magnitudes — the small digits quietly vanish.

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.

Related reading

Related 3D models: Ready to build it? Download free 3D models & CAD drawings at MOULDL »
MOULDL · Electrical Knowledge Base · free self-study reference for electrical & automation engineers, 3D CAD models at MOULDL