🎒 SchoolNEW 💡 Suggest

REAL and LREAL: IEEE 754 explained

Type a number or a hex value, or click the bits: you see sign, exponent and mantissa, the exact value the PLC really stores, the rounding error and the neighbouring values. The buttons show the special cases.

Special values:
Sign 0Exponent 133 − 127 = 6Mantissa 1.5625
REAL16#42C80000

Indicative results: always verify with a qualified technician and the applicable standards. Legal notice

Sign, exponent, mantissa

A REAL has 32 bits: 1 sign bit, 8 exponent bits and 23 mantissa bits. The value is (−1)sign × 1.mantissa × 2exponent − 127; the 1 before the point is not stored (it is “implicit”). An LREAL has 1 + 11 + 52 bits and the exponent offset (bias) is 1023.

Special values

Exponent all 0 and mantissa 0: ±0 (16#00000000 and 16#80000000, equal in comparisons). Exponent 0 and mantissa ≠ 0: denormals, down to the smallest 1.4E-45 (16#00000001). Exponent all 1 and mantissa 0: ±Inf (16#7F800000, 16#FF800000), for example after a division by zero. Exponent all 1 and mantissa ≠ 0: NaN (usually 16#7FC00000).

Precision limits

The mantissa has 24 useful bits, so integers are exact up to 16777216 (2²⁴): 16777217 does not exist in REAL and becomes 16777216; the neighbouring values are 16777216 and 16777218. In LREAL integers are exact up to 9007199254740992 (2⁵³). For totalisers of litres, kWh or parts use LREAL, or an integer DINT/LINT.

Why 0.1 + 0.2 is not exact

0.1 is a repeating fraction in binary (0.000110011…), like 1/3 in decimal: the PLC rounds it. In REAL 0.1 is exactly 0.100000001490116119384765625. In LREAL 0.1 + 0.2 gives 0.3000000000000000444…, while 0.3 is 0.2999999999999999888…: the comparison 0.1 + 0.2 = 0.3 is false. In REAL the two roundings happen to match (0.300000011920928955078125), but don't rely on it: always compare with a tolerance.

Frequently asked questions

How do you convert a REAL to hex?

Write the number as 1.mantissa × 2^exponent. 100.0 = 1.5625 × 2⁶: sign 0, exponent 6 + 127 = 133 (1000 0101), mantissa .1001 followed by zeros. The 32 bits give 16#42C80000.

Why does 16777217 become 16777216 in a REAL?

A REAL has 24 bits of precision (23 mantissa bits plus the implicit 1): integers are exact only up to 2²⁴ = 16777216. Above that values go in steps of 2, then 4, 8… A REAL totaliser that adds 1 gets stuck at 16777216: use LREAL or DINT.

What is NaN in a PLC?

NaN (“not a number”) is the result of operations such as 0.0/0.0 or the square root of a negative. Every comparison with NaN is false, even NaN = NaN. In TIA Portal the OK and NOT_OK instructions check whether a REAL is valid.

How many digits do REAL and LREAL have?

A REAL is reliable to about 7 significant digits (6 to 9), an LREAL to about 15–16. The maximum is 3.4028235E+38 for REAL and 1.7976931348623157E+308 for LREAL.

How do you compare two REALs?

Never with = : 0.1 + 0.2 in LREAL is 0.30000000000000004. Compare with a tolerance, for example ABS(a − b) < 0.0001, chosen to suit the measured quantity.

What do you think?

Your rating