Wrong numbers, no fault: three Siemens SCL math traps and how to avoid them

A shift total goes negative and the CPU never faults. Here's why SCL integer math overflows silently, how typed constants change the result, when Real stops adding, and a small LAD and SCL example that does it right.

Math that's wrong without a fault

A shift total on the HMI reads -20,536 kg. The CPU is in Run, there's no fault, and nothing is flashing. The code multiplies a part count by a part weight, both stored as Int, and the answer was too big for an Int. In Siemens SCL, math like this can give a wrong answer with no fault and no warning, so you have to design the data types up front.

Three traps and how to avoid them

  • The result type comes from the operands, not from where you store it. SCL picks the data type of an expression from its operands: two Int values give an Int result, and mixing Int and DInt gives DInt.1 Storing the answer in a DInt doesn't help if the multiply already overflowed. Convert an operand first (INT_TO_DINT, or CONV in LAD).
  • Typed constants set the math type. INT#1 is a constant with a fixed type; plain 1 is untyped. Mixing a typed constant with a big untyped one can force the whole expression into the smaller type, so INT#1 + 50000 is calculated as an Int and comes out as -15,535. Siemens recommends not mixing typed and untyped constants in one expression.2 Type both constants, or neither.
  • Real runs out of digits. A 32-bit Real holds about 7 significant digits. A flow total that keeps adding small amounts slows down and eventually stops changing. Use LReal on CPUs that support it, or keep totals as integers in a sensible unit and convert only for display. (Our run-hours tutorial shows the same problem on the Rockwell side.)

The part total, done right

One small example: a shift's total kilograms from a part count and a part weight, both Int because that's how the scale sends them. The fix is to widen both to DInt before the multiply.

Ladder

Three LAD networks: convert, convert, multiply, using the Siemens CONV and MUL boxes (parameters IN/OUT and IN1/IN2/OUT). This is a schematic drawing of the networks, not a TIA Portal screenshot; numbering starts at 0 here, while TIA calls them Network 1 to 3.

Schematic only, drawn in our usual ladder style with Siemens instruction names. In TIA Portal LAD each box also has EN/ENO pins and you pick the data type on the box. None of these are Rockwell instructions.

On a phone, swipe sideways to see the whole rung.

Widen the part count to DInt before multiplying0CONVConvert (Int to DInt)IN#PartCount0 ←OUT#PartCount_D0 ←
Widen the part weight too1CONVConvert (Int to DInt)IN#PartWeight_kg0 ←OUT#PartWeight_D0 ←
Multiply in DInt: 300 x 150 = 45,000 fits (an Int stops at 32,767)2MULMultiply (DInt)IN1#PartCount_D0 ←IN2#PartWeight_D0 ←OUT#ShiftKg0 ←
(End)

SCL

The same multiply in SCL, with the two other traps from this page shown as comments and code. Siemens SCL is its version of IEC 61131-3 structured text.

// Temp/static: #PartCount, #PartWeight_kg : Int
//              #ShiftKg : DInt;  #TotalLiters : LReal
// Trap 1: Int * Int is calculated as Int, then stored.
//   300 * 150 = 45000 does not fit in Int (max 32767).
#ShiftKg := #PartCount * #PartWeight_kg;                       // WRONG

// Fix: widen one operand first so the math runs in DInt.
#ShiftKg := INT_TO_DINT(#PartCount) * INT_TO_DINT(#PartWeight_kg);   // OK

// Trap 2: a typed constant sets the data type of the math.
#ShiftKg := INT#1 + 50000;        // WRONG: done in Int, gives -15535
// Don't mix typed and untyped constants. Type both, or neither:
#ShiftKg := INT#1 + DINT#50000;   // OK: done in DInt
#ShiftKg := 1 + 50000;            // OK: untyped, widest type on the CPU

// Trap 3: a Real total that stops adding.
//   Real holds about 7 significant digits. Above 16,777,216
//   adding 1.0 does nothing. Use LReal (S7-1500) for totals.
#TotalLiters := #TotalLiters + #FlowThisScan;                // LReal

Overflow in SCL doesn't stop the CPU. Unless you turn on the block property "Set ENO automatically", SCL doesn't report it through ENO either; it just stores a wrong number.3

Educational example. This code has not been compiled, downloaded, or tested on a controller. It is not production-ready or drop-in code. Treat it as a starting point to review, adapt, and test on your own system. It is standard control logic, not safety logic.

Habits that prevent this

  • Size tags for the worst case, not today's numbers. A count that's never over 300 today can be 3,000 after a line speed change.
  • Turn on "Set ENO automatically" for blocks where a math error must be caught, and check ENO. It adds some run-time overhead, so use it where it matters.3
  • Test the edges. In a simulator, force the inputs to their maximum values and see what the math does before it goes live.
  • Check conversions. Converting a large DInt to Int, or a Real to an integer, can fail or round. Look at the conversion rules for your CPU family.4

Sources

  1. Siemens, STEP 7 (TIA Portal) help, "Arithmetic expressions" (data type of the result): docs.tia.siemens.cloud.
  2. Siemens, STEP 7 help, "Calculating with constants in SCL" (typed and untyped constants): docs.tia.siemens.cloud.
  3. Siemens, STEP 7 help, "Enabling and disabling the property 'Set ENO automatically' in SCL": docs.tia.siemens.cloud.
  4. Siemens, STEP 7 help, "Overview of data type conversion (S7-1500, S7-1200 G2)": docs.tia.siemens.cloud.

Instruction details were checked against the vendor documentation linked above. Behavior can change between software and firmware versions, so check the help for the version you run.

Numbers on the HMI you don't trust?

We troubleshoot Siemens and Rockwell programs, onsite in California's Central Valley and remotely nationwide.