648 Rockwell Automation Publication 1756-RM003N-EN-P - October 2011
Appendix A Common Attributes
Totalizer Examples
The IEEE 754 standard affects totalization applications such that errors occur
when adding very small numbers to very large numbers. The standard requires
exponents in the two operands to be the same. Since the fractional component is
only 23 bits, as the exponent gets larger, the fractional component approaches
zero.
This can be seen when adding 1 to a number over a period of time. When the sum
is in the 16 million range, the number 1 becomes 0 because the exponent is so
large, a 1 is insignificant, and gets shifted out of the equation. The result is that a
0 is added instead of a 1.
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 += 100;
x -= 100; // there might be a tiny remainder …
}