Floating Point Values

Logix controllers handle floating point values according to the IEEE 754 standard for floating-point arithmetic. This standard defines how floating point numbers are stored and calculated. The IEEE 754 standard for floating point math was designed to provide speed and the ability to handle very large numbers in a reasonable amount of storage space.
A REAL tag stores a single-precision, normalized floating-point number.
An LREAL tag stores a double-precision, normalized floating-point number.
The controllers support these elementary data types:
  • REAL
  • LREAL
Denormalized numbers and -0.0 are treated as 0.0
If a computation results in a NAN value, the sign bit could be positive or negative. In this situation, the software displays 1#.NAN with no sign.
Not all decimal values can be exactly represented in this standard format, which results in a loss of precision. For example, if you subtract 10 from 10.1, you expect the result to be 0.1. In a Logix controller, the result could very well be 0.10000038. In this example, the difference between 0.1 and 0.10000038 is .000038%, or practically zero. For most operations, this small inaccuracy is insignificant. To put things in perspective, if you were sending a floating point value to an analog output module, there would be no difference in the output voltage for a value being sent to the module that differs by .000038%.
Guidelines for Floating-point Math Operations
Follow these guidelines:
When performing certain floating-point math operations, there may be a loss of precision due to rounding error. Floating-point processors have their own internal precision that can impact resultant values.
Do not use floating point math for money values or for totalizer functions. Use INT or DINT values, scale the values up, and keep track of the decimal place (or use one INT or DINT value for dollars, and a second INT or DINT value for cents).
Do not compare floating-point numbers. Instead, check for values within a range. The LIMIT instruction is provided specifically for this purpose.
Totalizer Examples
The precision of the REAL data type affects totalization applications such that errors occur when adding very small numbers to very large numbers.
For example, add 1 to a number over a period of time. At some point the add will no longer affect the result because the running sum is much greater than 1, and there are not enough bits to store the entire result. The add stores as many upper bits as possible and discards the remaining lower bits.
To work around this, do math on small numbers until the results get large. Then, transfer them to another location for additional large-number math. For example:
  • x is the small incremented variable.
  • y is the large incremented variable.
  • z is the total current count that can be used anywhere.
  • x = x+1;
  • if x = 100,000;
  • {
  • y = y + 100,000;
  • x = 0;
  • }
  • z = y + x;
Or another example:
  • x = x + some_tiny_number;
  • if (x >= 100)
  • {
  • z = z + 100;
  • x = x - 100; // there might be a tiny remainder
  • }
Provide Feedback
Have questions or feedback about this documentation? Please submit your feedback here.