PLCcheck

SCL Mathematics: Arithmetic, Comparison, Logic, and Conversion Operators

Complete SCL operator reference: arithmetic (+, -, *, /, MOD, **), comparison (<, >, =, <>), logic (AND, OR, XOR, NOT), type conversion (INT_TO_REAL, DINT_TO_REAL), and numeric functions (ABS, SQRT, SIN, COS). With precedence rules and data type behavior.

·10 min read
SCLoperatorsarithmeticcomparisonlogicconversionINT_TO_REALmathIEC 61131-3TIA Portal

Diesen Artikel auf Deutsch lesen

SCL Mathematics: Arithmetic, Comparison, Logic, and Conversion Operators

This reference covers all mathematical and logical operators available in Siemens SCL (Structured Control Language), including operator precedence, data type rules, and type conversion functions. SCL follows the IEC 61131-3 Structured Text standard with Siemens-specific extensions.

Arithmetic Operators

OperatorMeaningExampleResult Type
+Additiona + bLarger of the two operand types
-Subtractiona - bLarger of the two operand types
*Multiplicationa * bLarger of the two operand types
/Divisiona / bLarger type; INT / INT = INT (truncated)
MODModulo (remainder)17 MOD 5 → 2INT or DINT
DIVInteger division17 DIV 5 → 3INT or DINT
**Power (exponent)2.0 ** 3.0 → 8.0REAL
+ (unary)Positive sign+aSame as operand
- (unary)Negative sign-aSame as operand

Data type rules for arithmetic:

Example — Integer vs. REAL division:

// WRONG: integer division truncates
result_int := 7 / 2;           // result_int = 3 (not 3.5)

// RIGHT: convert to REAL first
result_real := INT_TO_REAL(7) / INT_TO_REAL(2);  // result_real = 3.5

Comparison Operators

All comparison operators return BOOL (TRUE or FALSE).

OperatorMeaningExample
=Equal toIF a = 10 THEN
<>Not equal toIF a <> 0 THEN
<Less thanIF temp < 100.0 THEN
>Greater thanIF pressure > 5.0 THEN
<=Less than or equalIF level <= max_level THEN
>=Greater than or equalIF count >= preset THEN

Data type rules for comparison:

Example — Floating-point comparison with tolerance:

// WRONG: exact equality on REAL is unreliable
IF temperature = 100.0 THEN ...    // may never be exactly 100.0

// RIGHT: use tolerance band
IF ABS(temperature - 100.0) < 0.1 THEN ...    // within 0.1 degrees

Logical Operators

Logical operators work on BOOL values. When applied to WORD/DWORD, they operate bitwise.

OperatorMeaningExamplePrecedence
NOTNegationNOT aHighest (7)
AND or &Logical ANDa AND b8
XORExclusive ORa XOR b9
ORLogical ORa OR bLowest (10)

BOOL examples:

// Motor runs if start pressed AND no fault AND not stopped
Motor := Start AND NOT Fault AND NOT Stop;

// Alarm if temperature too high OR pressure too high
Alarm := (Temp > 80.0) OR (Pressure > 6.0);

// Toggle: changes state on each call when trigger is TRUE
IF Trigger AND NOT Trigger_Old THEN
    Toggle := NOT Toggle;
END_IF;
Trigger_Old := Trigger;

Bitwise example (WORD):

// Mask bits 0-3 of a WORD
masked := input_word AND W#16#000F;   // keeps only lower 4 bits

// Set bit 7
output_word := input_word OR W#16#0080;

// Toggle bit 4
output_word := input_word XOR W#16#0010;

Operator Precedence (Highest to Lowest)

PriorityOperatorCategory
1( )Parentheses
2+ - (unary)Sign
3NOTNegation
4**Power
5* / MOD DIVMultiplicative
6+ -Additive
7< > <= >=Relational
8= <>Equality
9AND &Logical AND
10XORExclusive OR
11ORLogical OR
12:=Assignment (lowest)

Rule of thumb: Arithmetic first, then comparison, then logic. When in doubt, use parentheses — they make the code more readable and prevent precedence errors.

Type Conversion Functions

SCL requires explicit conversion between incompatible types. On S7-1500, many conversions happen implicitly, but explicit conversion is best practice for portability and clarity.

Numeric Conversions

FunctionFrom → ToExample
INT_TO_REALINT → REALr := INT_TO_REAL(i);
INT_TO_DINTINT → DINTd := INT_TO_DINT(i);
DINT_TO_REALDINT → REALr := DINT_TO_REAL(d);
REAL_TO_INTREAL → INTi := REAL_TO_INT(r); (rounded)
REAL_TO_DINTREAL → DINTd := REAL_TO_DINT(r); (rounded)
DINT_TO_INTDINT → INTi := DINT_TO_INT(d); (truncated if > 32767)

BCD Conversions (Legacy Compatibility)

FunctionFrom → ToUse Case
BCD_TO_INTBCD WORD → INTReading S5-style counter/timer values
INT_TO_BCDINT → BCD WORDWriting to BCD displays
DINT_TO_BCD_DWORDDINT → BCD DWORDLarge BCD values

Bit/Byte Conversions

FunctionFrom → To
BOOL_TO_BYTEBOOL → BYTE
BYTE_TO_WORDBYTE → WORD
WORD_TO_DWORDWORD → DWORD
BYTE_TO_INTBYTE → INT
WORD_TO_INTWORD → INT
DWORD_TO_DINTDWORD → DINT

S7-1500 universal conversion: On S7-1500, you can also use the CONVERT instruction or implicit conversion in many cases.

Numeric Functions (Math Library)

FunctionDescriptionExampleResult
ABS(x)Absolute valueABS(-5)5
SQR(x)Square (x²)SQR(4.0)16.0
SQRT(x)Square rootSQRT(16.0)4.0
EXP(x)e^xEXP(1.0)2.7183
EXPD(x)10^xEXPD(2.0)100.0
LN(x)Natural logarithmLN(2.7183)~1.0
LOG(x)Common logarithm (base 10)LOG(100.0)2.0
SIN(x)Sine (x in radians)SIN(1.5708)~1.0
COS(x)Cosine (x in radians)COS(0.0)1.0
TAN(x)Tangent (x in radians)TAN(0.7854)~1.0
ASIN(x)Arc sineASIN(1.0)~1.5708
ACOS(x)Arc cosineACOS(0.0)~1.5708
ATAN(x)Arc tangentATAN(1.0)~0.7854

All trigonometric functions use radians, not degrees. To convert: radians := degrees * 3.14159 / 180.0;

Practical Example: Analog Scaling

A common real-world application combining arithmetic, conversion, and comparison:

// Scale raw analog input (0–27648) to engineering units (0.0–100.0 bar)
VAR
    raw_value   : INT;      // From analog input module
    pressure    : REAL;     // Engineering units
    high_alarm  : BOOL;
END_VAR

pressure := INT_TO_REAL(raw_value) * 100.0 / 27648.0;

high_alarm := pressure > 85.0;  // Alarm at 85 bar

Analyze Your S5 Arithmetic Code

PLCcheck Pro converts S5 arithmetic operations (+F, -F, ×F, :F) to clean SCL with correct type handling and overflow protection.

Upload code for conversion → | S5→S7 Migration Guide →

Part of the SCL Reference. Maintained by PLCcheck.ai. Not affiliated with Siemens AG.

Related Articles

Analyze your PLC code with AI

PLCcheck Pro explains, documents, optimizes, and migrates PLC code — automatically.

Try PLCcheck Pro →
← Back to Blog

Not affiliated with Siemens AG. S5, S7, STEP 5, STEP 7, and TIA Portal are trademarks of Siemens AG.