Math Functions
Calculations & Algorithms
From simple addition to complex equations, math functions transform your raw data into actionable information for controlling your processes.
What are Math Functions in TIA Portal?
Math functions in TIA Portal allow you to perform arithmetic, trigonometric, and logarithmic calculations on different data types (Integers, Reals). They are essential for sensor scaling, trajectory calculation, or PID loop control.
TIA Portal stands out with the CALCULATE instruction, which allows you to write a complex expression (e.g., (in1 + in2) * in3 / in4) in a single block, improving readability compared to a series of individual ADD/MUL boxes.
Basic Arithmetic
Fundamental operations for data processing
CALCULATE — Calculate
CALCWhat it does
Allows defining a free mathematical expression combining several inputs (IN1...INn). You enter the formula, and TIA Portal generates the corresponding pins.
When to use it
As soon as a calculation requires more than two steps. Ideal for flow, volume calculations, or any complex physical formula.
Pro Tips
You can use functions like ABS, SQRT, or SIN directly within the CALCULATE expression.
Always check the output data type (OUT) to avoid overflows.
In SCL, it's the natural equivalent of a direct assignment: out := (in1 + in2) * in3;
ADD / SUB / MUL / DIV
+What they do
Standard arithmetic instructions: Addition, Subtraction, Multiplication, and Division.
When to use them
For simple and fast operations between two values.
Pro Tips
In division (DIV), always use the |OK| test or check that the divisor is not zero to avoid a CPU stop.
The ADD instruction can accept more than two inputs by clicking the yellow icon on the block.
For integers, DIV returns the integer quotient. Use MOD for the remainder.
MOD — Modulo
MODWhat it does
Returns the remainder of integer division between IN1 and IN2.
When to use it
Managing ring buffers, alternating cycles (e.g., pumps 1 and 2), or unit conversion (e.g., seconds to minutes/seconds).
Pro Tips
Very useful for triggering an action every 'N' cycles.
Only available for integer data types (Int, DInt, etc.).
SCL: remainder := value1 MOD value2;
Sign & Increment
Sign management and fast unit operations
INC / DEC — Increment / Decrement
++What it does
Adds or subtracts 1 from the specified variable (InOut).
When to use it
Simple part counters, loop indexes, queue management.
Pro Tips
Faster to write than a full ADD or SUB block.
Watch the data type: an increment on an Int at 32767 will wrap to -32768.
In SCL: i := i + 1; or INC(i);
ABS — Absolute Value
ABSWhat it does
Returns the positive value of a number, whether it was positive or negative to start with.
When to use it
Calculating deviation between a measurement and a setpoint, distance measurement, PID error calculation.
Pro Tips
Essential for calculating absolute error (|setpoint - measurement|).
Supports both integers and reals.
SCL: positiveVal := ABS(originalVal);
Min / Max / Limit
Value selection and bounding
MIN / MAX — Minimum / Maximum
MINWhat it does
Compares N values and returns the smallest (MIN) or largest (MAX).
When to use it
Selecting the highest temperature among several probes, determining the minimum allowed speed.
Pro Tips
You can add as many inputs as needed to the block.
Very useful for multi-sensor diagnostics.
SCL: peakValue := MAX(val1, val2, val3);
LIMIT — Limit
LIMITWhat it does
Bounds (clamps) an input value (IN) between a minimum (MN) and a maximum (MX).
When to use it
Safety on analog outputs (e.g., preventing a valve from opening more than 80%), capping user setpoints.
Pro Tips
Advantageously replaces a combination of MIN and MAX.
If MN > MX, the output depends on the CPU type, but usually MX is taken.
SCL: safeVal := LIMIT(MN:=0.0, IN:=rawVal, MX:=100.0);
Powers & Roots
Exponential and radical calculations
SQRT / SQR / EXPT
SQRTWhat they do
SQRT (Square root), SQR (Square), EXPT (Custom power).
When to use them
Geometry calculations, non-linear signal conversion, fluid physics.
Pro Tips
SQRT of a negative number gives NaN. Always test the input or use ABS.
EXPT is CPU-intensive, use SQR for simple squares.
SCL: area := 3.14 * SQR(radius);
Trigonometry
Angle and rotation calculations
SIN / COS / TAN / ASIN...
SINWhat they do
Standard trigonometric calculations and their inverses (arcsin, arccos, arctan). Angles are always in RADIANS.
When to use them
Robot kinematics, angular positioning calculations, oscillating signal processing.
Pro Tips
To convert degrees to radians: Radians = Degrees * (PI / 180).
TAN(PI/2) tends toward infinity, watch your inputs.
Use system constants for PI to ensure precision.
Advanced Functions
Logarithms and fractional parts
LN / EXP / FRAC
LNWhat they do
LN (Natural Log), EXP (Exponential e^x), FRAC (Extracts the decimal part).
When to use them
Chemical process modeling, exponential growth, or unit separation (FRAC).
Pro Tips
FRAC(12.34) returns 0.34. Very handy for time precision calculations.
EXP is the exact inverse of LN.
SCL: fraction := FRAC(totalValue);
Comparison of Calculation Approaches
LAD vs SCL — Which method to choose?
| Calculation Type | Recommended Method | Why? |
|---|---|---|
| Simple addition | ADD Block (LAD) | Visual and easy to follow |
| Complex formula | CALCULATE Block | Compact and reduces number of networks |
| Mathematical algorithm | SCL Language | Syntax close to C/Pascal, ideal for formulas |
| Trigonometric calculation | SCL or CALCULATE | Individual boxes are too bulky |
| Signal bounding | LIMIT Block | Siemens standard, very robust |
| Index reset | INC/DEC Instruction | Very fast to implement |
Frequently Asked Questions
What is the advantage of CALCULATE over multiple ADD/MUL blocks?
CALCULATE significantly reduces the space occupied in the program. Instead of 5 networks with intermediate temporary variables, you have a single block with a readable formula. It's also easier to maintain if the formula changes.
How does TIA Portal handle calculation errors (e.g., division by zero)?
On S7-1200/1500, a mathematical error doesn't necessarily stop the CPU (depending on config), but the result becomes NaN. It is CRITICAL to use the |OK| instruction after a sensitive calculation to validate the result before using it.
Do trigonometric functions use degrees or radians?
All functions (SIN, COS, TAN, etc.) exclusively use RADIANS. If your HMI provides degrees, you must multiply the value by 0.01745329 (PI/180) before injecting it into the block.
Can I perform calculations on 64 bits (LReal)?
Yes, S7-1500s natively support the LReal (Long Real) type for increased precision. Most math functions accept this type.