IMDHMIControls LibraryShop main store

Troubleshooting

Why Do-more MATH truncates your integer division result

PLC programming Published 6 min read

Short answer

A Do-more MATH expression whose operands are all integers is evaluated entirely in integer math, and integer division truncates instead of rounding. Write one operand as REAL — 1440.0 instead of 1440 — or wrap the operand in TOREAL(), and every remaining operation runs in floating point.

On this page
  1. Why does GPM x 1440/1000000 give a different answer than GPM x 0.00144?
  2. When does Do-more switch an expression to floating point?
  3. How do you fix the MATH expression so nothing truncates?
  4. How much error does integer truncation cause at real flows?
  5. Which Do-more functions return REAL whatever you feed them?
  6. Should every MATH box just use REAL constants?
  7. Questions asked

A Do-more MATH box computing GPM x 1440 / 1000000 returns 0 at 350 GPM where GPM x 0.00144 returns 0.504, and the instruction is working exactly as designed. When every operand in a MATH expression is an integer type, the Do-more MATH instruction evaluates the whole expression in integer arithmetic, and integer division truncates instead of rounding. The constant 0.00144 is REAL, so the second expression multiplies in floating point from the first operation and keeps every decimal. The fix is one decimal point: write 1440.0 / 1000000.0, or wrap the operand in TOREAL(). The behavior described here is the MATH instruction in Do-more Designer; other platforms apply their own type promotion rules.

Why does GPM x 1440/1000000 give a different answer than GPM x 0.00144? #

Because the MATH instruction evaluates an all-integer expression entirely in integer arithmetic, and integer division truncates: at 350 GPM, GPM x 1440 / 1000000 returns 0 while GPM x 0.00144 returns 0.504.

The instruction works through the expression left to right. With GPM = 350: 350 x 1440 = 504,000, an exact integer product; 504,000 / 1,000,000 is 0.504 in real arithmetic but 0 in integer arithmetic, because everything below 1 is discarded rather than rounded to nearest. The same rule shows up at any small quotient:

  • 1 / 2 = 0
  • 2 / 3 = 0
  • 9 / 10 = 0
  • 10 / 10 = 1

Rounding would return 1 for 9/10; truncation returns 0. The constant 0.00144 is REAL, so the second expression runs its multiply in floating point from the start and never enters integer division. If the GPM value itself is a scaled analog reading, check that conversion for the same fraction loss — scaling a 4-20 mA analog input to engineering units in CPM2 ladder covers the input end of that chain.

When does Do-more switch an expression to floating point? #

Do-more runs an expression in integer math until it meets a REAL operand; from that point every remaining calculation runs in floating point, so the position of that operand decides how much survives.

Four expressions with the same numbers show where the boundary sits:

  • (1 + 4) / 2 = 2
  • (1 + 4) / 2.0 = 2.5
  • (1 + 4.0) / 2 = 2.5
  • TOREAL(1 + 4) / 2 = 2.5

The trap is putting the REAL operand too late. GPM x 1440 / 1000000 x 1.0 still returns 0.0 at 5 GPM: 5 x 1440 = 7200, then 7200 / 1000000 = 0 with the fraction already gone, then 0 x 1.0 = 0.0. The decimal point has to arrive before the division — put it on the divisor or on anything to its left.

How do you fix the MATH expression so nothing truncates? #

Make one operand before the division REAL — write GPM x 1440.0 / 1000000.0 — or force the conversion with TOREAL(), and check that the destination is a REAL location rather than an integer one.

  1. Add decimal points to the constants: GPM x 1440.0 / 1000000.0. One REAL operand ahead of the division is enough, and marking every constant REAL tells the next reader the whole expression is floating point.
  2. Wrap the source in TOREAL(): TOREAL(GPM) x 1440 / 1000000 converts GPM before anything else uses it, so the division runs in REAL.
  3. Start with a REAL-returning function when one belongs in the calculation anyway — SQRT, RAD and DEG return REAL regardless of what you pass them, and everything after them goes floating point.
  4. Check the destination. A REAL result stored into an integer location drops its fraction, a second truncation that quietly defeats the first three fixes.

Prove it before download. In the simulator, set an integer source D0 to an initial value of 350 and compare two MATH boxes: D0 x 1440 / 1000000 into an integer destination stores 0, while D0 x 1440.0 / 1000000.0 into a REAL destination stores 0.504. Step D0 through 0, 350 and 695 and watch the integer version sit at 0 until the third step. When the REAL result has to go back out as integer counts, scale a PID real output to analog output counts covers that conversion and its rounding choices.

How much error does integer truncation cause at real flows? #

Everything below 1 disappears: the integer expression returns 0 for any flow up to 694 GPM, first reads 1 at 695 GPM, and at 350 GPM reports 0 instead of the true 0.504 million gallons per day.

The worked example, with units carried through:

  • Source value: GPM = 350, an integer count in gallons per minute.
  • Integer path: 350 x 1440 = 504,000 gallons per day; 504,000 / 1,000,000 = 0.504 MGD in real arithmetic, 0 after truncation.
  • REAL path: 350 x 1440.0 = 504,000.0; 504,000.0 / 1000000.0 = 0.504 MGD.
  • Wake-up point: the integer result first reaches 1 when GPM x 1440 crosses 1,000,000, at 1,000,000 / 1440 = 694.4, so GPM = 695 (1.0008 truncated to 1); it reads 2 from GPM = 1389 (2,000,160 / 1,000,000 = 2.00016).

Overflow is not the culprit at these numbers: the intermediate 504,000 sits far below the 2,147,483,647 ceiling of a 32-bit signed integer, so nothing wrapped. The decision follows from the dead band: any total used for reporting or display runs REAL end to end, and if the total feeds chemical dosing or custody numbers, test the expression at zero, mid-range and full flow before it runs the plant.

Which Do-more functions return REAL whatever you feed them? #

SQRT, RAD and DEG return a REAL result whether their parameter expression is integer or REAL — STDEVR and STDEVPR behave the same — so any one of them promotes everything after it to floating point.

That cuts both ways. Used deliberately, a SQRT(...) term ahead of a division buys REAL arithmetic without touching the constants. Hit accidentally, an expression you read as integer turns REAL midway and lands somewhere you did not expect, and everything before the function already executed in integer math, so the promotion never repairs an earlier truncation. For readability, prefer the decimal point: the next technician sees 1440.0 instantly, where a function's return type has to be traced.

Should every MATH box just use REAL constants? #

No — integer math is the right tool for counters, indices and accumulator arithmetic; put REAL into an expression only where fractions matter, and when they do, make the whole expression REAL from its first operand.

Instruction-set documentation for compute and math instructions makes the same point from the speed side: an instruction executes faster and requires less memory when all of its operands use the same optimal data type, typically DINT or REAL. Mixed-type expressions are the slow case and, as this fault shows, the surprising case. Decide per expression:

  • Counting, indexing, .Acc arithmetic and address math: integer end to end.
  • Scaling, totalization, flow and statistical results: REAL end to end, destination included.
  • The two types meet only at a deliberate, tested conversion point.

The bug came from accidental mixing — integer constants beside an integer register, evaluated as integers. The fix is not REAL everywhere; it is consistency in the type each expression actually needs.

Questions asked

Where does the 1440 in the conversion come from?

One day holds 24 hours times 60 minutes, which is 1440 minutes, so gallons per minute times 1440 gives gallons per day, and dividing by 1,000,000 converts gallons to million gallons. With REAL constants, 350 GPM works out to 350 x 1440.0 / 1000000.0 = 0.504 million gallons per day.

What can TOREAL() take as its parameter?

It is normally used on the integer register families D, V and timer or counter .Acc values, but it accepts any expression: TOREAL(1 + 4) evaluates the integer sum first, converts 5 to REAL, and the division by 2 that follows then runs in floating point and returns 2.5.

Do addition, subtraction and multiplication truncate the same way?

They cannot create a fraction from integer operands, so nothing is discarded: their results are exact as long as they stay inside the range the integer type holds. Division is the only one of the four that produces a fractional result from integers, which is why truncation shows up there.

Why does a ROUND function turn 10.5 into 10?

PLC math function references document that a REAL exactly halfway between two integers rounds to the even integer, so 10.5 goes to 10 and 11.5 goes to 12. Keep comparison and totalization boundaries off exact half values, because the direction at a half is not the round-up most people expect.

How deep can a MATH expression nest?

Parentheses nest to 8 levels in the Do-more MATH instruction, and its function set includes ten REAL functions, among them natural log, log, e, PI and square root. Deep nesting is legal, but a chain another technician cannot check in one read becomes its own troubleshooting risk.

Keep reading

  • Troubleshooting

    Reading IO-Link ISDU parameters over PROFINET acyclic record data

    If cyclic IO-Link process data exchanges normally, leave out the connect section of IOL_CALL and address the identification and ISDU calls with the hardware ID of the proxy submodule for the target port, read straight from the PROFINET device configuration. A valid connect result such as 8 proves only that a lookup succeeded, not that it selected your port's submodule.

    PLC programming

  • How-to

    Run Logix 5000 PID loops in a periodic task

    No. Thirty PID instructions in a FOR loop in a continuous task, with no timer, execute once per scan at a varying interval, so their loop update time setting never matches reality. Move the routine into a periodic task whose period equals the shared update time, and write that same period into every loop's .UPD.

    PLC programming

  • Troubleshooting

    When a CODESYS TON works in structured text but not ladder

    The structured text routine counts because its four statements run in sequence inside one scan; the ladder version fails when the reset network clears Timer.Q before the addition samples it, or an unconditional reset restarts the timer every cycle. Put the call, the Q test, the addition and the reset of the same TON instance in one ladder network, left to right in that order, and it counts exactly one B per interval.

    PLC programming