TRN, RND and MOD: the Rounding That Loses a Count, and the Four Lines That Fix It

A flow totaliser on a CompactLogix 5380 reads 8,928 litres at the end of an eight-hour shift. The meter says 8,976. The difference is 48 litres, the flow was steady at 18.7 L/min all day, and the arithmetic in the controller is one ADD and one MOV in a 100 ms periodic task.

Nothing is broken. The 48 litres are the fractional part of every scan, thrown away 288,000 times.

Three facts settle most of this before the debugging starts. TRN is TRUNC — 1756-RM018A records that in Logix Designer version 36 the mnemonic for this instruction changed from TRN to TRUNC, so the same instruction has two names depending on whose project you opened. There is no RND instruction in the Logix set at all; rounding is something the controller does during a data conversion, not something you ask for. And the rounding it does is not the rounding you were taught: fractions of exactly 0.5 round up or down to the nearest even number, so 2.5 becomes 2 and 3.5 becomes 4.

TRUNC and MOD with their operands and real values, next to the REAL-to-DINT conversion table from the reference

The left pair is what you write. The right column is what happens when a REAL lands in a DINT without either of them, and it is the one nobody configures.

Truncate does one thing and says so

“Truncating does not round the value; rather, the non-fractional part remains the same, regardless of the value of the fractional part.”

That is the whole Description section for TRUNC, and it is worth quoting because the instruction is otherwise almost featureless: one Source, one Destination, no control tag. On a CompactLogix 5370 or ControlLogix 5570 the Source must be a REAL and the Destination can be SINT, INT, DINT or REAL. On a CompactLogix 5380, ControlLogix 5580 or ControlLogix 5590 the Source can also be LREAL and the Destination list opens up to LINT and the four unsigned types. It costs 0.267 µs on a REAL and 0.160 µs on a DINT on a 5580 or 5380, which is to say nothing at all.

It is not available as an instruction in structured text.

What you use instead is TRUNC as an operator inside an expression, which the reference demonstrates as REAL_dest := TRUNC(REAL_src);. Same for MOD: no ST instruction, but MOD works as an operator in an expression. That pattern — ladder and function block get an instruction, structured text gets an operator — covers most of the maths set, and it is the reason a routine translated from ladder to structured text sometimes will not verify until you stop looking for a block.

The operator is there. The block is not, and the error message does not say so.

The eight numbers that decide the argument

Put truncation and conversion side by side on the same values and the difference stops being abstract.

ValueTRUNCconverted to DINT
1.411
1.512
1.612
2.522
3.534
-2.5-2-2
-3.5-3-4

The 2.5 and 3.5 rows are the ones people argue about.

Advertisement

1756-RM018A states the conversion rule in three lines: fractions below 0.5 round down to the nearest whole number, fractions above 0.5 round up to the nearest whole number, and fractions of exactly 0.5 round up or down to the nearest even number. That last rule is why 2.5 goes to 2 and 3.5 goes to 4, and the reference prints both rows so there is no doubt about it. It is the correct choice statistically — rounding every half up biases a long series of conversions upward — and it is a genuine surprise the first time a test rig feeding 0.5 increments produces a sequence that is not monotonic in the way somebody expected.

Truncation always moves toward zero. Conversion moves to the nearest, then to the even.

So TRUNC(-3.5) is -3 and a MOV of -3.5 into a DINT is -4, and the two disagree by a whole unit on exactly the values a scaled analogue signal spends most of its time near. If you have an existing routine that mixes the two, that difference is worth finding before somebody reports the count as wrong.

One of the two is deliberate in your routine. The other one got there by assignment.

The 48 litres, and where they went

The totaliser in the opening did nothing clever, and that is the problem.

Flow of 18.7 L/min is 311.67 mL every second and 31.167 mL in a 100 ms task. Accumulate that into a DINT holding millilitres and each scan adds 31, because the conversion or the truncation takes the rest. The loss is 0.167 mL a scan, ten scans a second, 1.67 mL a second, 6.0 litres an hour — and an eight-hour shift is 48 litres, which is 0.53% of an 8,976 litre total. It is not enough to look like a fault and it is far too much to be inside the meter’s accuracy, so it gets blamed on the meter, and it will keep getting blamed on the meter because every reading is low by the same fraction and the bias looks exactly like a calibration error.

The fix is four lines and it does not need a faster task.

Keep the fraction. Accumulate in a REAL, take the whole units out with TRUNC, and subtract what you took so the remainder rolls into the next scan:

Acc := Acc + Flow_mL_per_scan;      (* REAL, holds the remainder *)
Whole := TRUNC(Acc);                (* DINT, 31 or 32 *)         
Total_mL := Total_mL + Whole;       (* DINT, the number that matters *)
Acc := Acc - Whole;                 (* what was not counted this scan *)

Nothing is discarded. Acc never grows past 1.0, so it never runs into a precision problem of its own, and Total_mL counts every millilitre that went through the meter. The same four lines are the answer to any “increment by a fraction” problem — counting cartons where the encoder gives 37.5 pulses per part, a runtime meter accumulating tenths of a minute, a production count derived from a scaled analogue rate.

The litres never counted growing from zero to 48 over an eight-hour shift, with the two shift totals beside it

The trace is the difference between the two routines, not either total, because at this scale the two totals draw on top of each other. The slope is a constant 6.0 litres an hour.

MOD, and the algorithm printed underneath it

MOD

Advertisement
gives you the remainder of a division, and the reference does not leave you to guess how:

Dest = Source A - (truncate (Source A / Source B) * Source B)

Read that carefully, because the word in the middle is truncate and not round, and it decides the sign of every negative result. Work it through with -7 and 3: -7 divided by 3 is -2.333, truncated toward zero that is -2, times 3 is -6, and -7 minus -6 is -1. Not 2. The result takes the sign of Source A, which is the same convention C uses and the opposite of the mathematical modulo people remember from school. That matters exactly once and then it matters permanently, because the place MOD earns its keep is wrapping an index around a circular buffer:

Next := (Pos + Step) MOD 16;

With a positive Step that produces 0 to 15 forever and is perfect. With a Step that can go negative — a shuttle that indexes backwards, a recipe pointer that steps back a station, a indirectly addressed lookup that walks a table in reverse — it produces -1, and the next array access on it is a major fault, type 4 code 20. The fix is one more term: Next := ((Pos + Step) MOD 16 + 16) MOD 16;, which costs a second MOD at 0.323 µs and never returns a negative.

Five index calculations through MOD 16, three of which return a negative position, and the same five through the corrected expression

Every row on the left is what the printed algorithm produces. The three shaded ones are array indices, and the next read on any of them is a major fault.

Write it that way from the start, because the negative case shows up on a night shift.

Division has a related trap in the same paragraph of the reference.

For integer destination and source operands the result of a DIV is truncated, so 7 / 2 in a DINT expression is 3 and not 3.5 and not 4. And if Source B is zero the result is Source A for integer divides and 1.$ for floating point divides, a condition the manual says can also cause minor overflow faults. That 1.$ is how Logix displays infinity, and it propagates: once one REAL in a chain is 1.$, everything downstream of it is too, which is why a scaled value that suddenly reads 1.$ on a faceplate is nearly always a divide by a span that came back zero rather than anything wrong with the transmitter. The minor fault codes are where the overflow shows up.

Two more conversions that lose data without saying anything

The first is narrowing an integer, and it does not round at all.

To convert a DINT to a SINT or an INT the controller truncates the upper portion and stores the lower bits that fit, generating an overflow if the value is too large. The reference’s own example is the one to remember: 16#0001_0081, which is 65,665, becomes 16#0081 or 129 as an INT, and 16#81 or -127 as a SINT. The sign flip is not a bug, it is bit 7 of the surviving byte becoming the sign bit, and it is what makes a production count that has been quietly moved into an INT for a Modbus register start going negative at 32,768.

The second is the one that catches long-running totalisers, and it is a REAL problem in both senses.

A REAL stores 24 bits of significand — 23 stored plus a hidden bit — so it can represent every whole number up to 16,777,216 and not one past it. 1756-RM018A says it from the conversion side: if a DINT value requires more than 24 significant bits it might not convert to the same REAL value, and if it will not, the controller stores the uppermost 24 bits rounded to the nearest even value. The consequence for a totaliser held in a REAL is that once it passes 16,777,216, adding 1 to it leaves it exactly where it was. At 1,122 litres an hour that is about 1.7 years of continuous running, which is precisely long enough for nobody to connect the two events. Keep running totals in a DINT or a LINT, and keep the fractional remainder in a separate small REAL — which is what the four lines above already do.

One more difference is worth knowing if you move projects between controller families.

1756-RM018A carries a note that the Logix Designer application interprets numbers differently depending on the controller model: for a 5×70 controller it interprets the literal 2 as a REAL, and for a 5×80 controller it interprets the same literal 2 as a DINT. So an expression written on a 1756-L75 and imported to a 1756-L83E can change which conversions happen inside it, without a single character of the expression changing. Put a decimal point on any literal you intend to be floating point — 2.0, not 2 — and the question never arises.

Two characters, and one whole class of migration surprise stops existing.

Advertisement

What to check first

Find every place a REAL lands in a DINT and ask what happens to the fraction. A MOV, a COP, an assignment in structured text and an Output parameter on an Add-On Instruction all do the conversion silently, and none of them is wrong — they are just each throwing away up to a whole unit, every scan, in a place nobody thought of as arithmetic.

Then look at the accumulators specifically. Any tag whose job is to count something that arrives in fractions — litres, minutes, parts, metres of web — needs the remainder carried forward or it will read low for ever by a fixed percentage. If the scaling upstream of it is what produced the fraction in the first place, scaling a 4-20 mA transmitter on a 5380 is the place to check the units before you touch the maths.