TIA Portal Instructions

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

CALC

What 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

MOD

What 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

ABS

What 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

MIN

What 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

LIMIT

What 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

SQRT

What 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...

SIN

What 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

LN

What 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 TypeRecommended MethodWhy?
Simple additionADD Block (LAD)Visual and easy to follow
Complex formulaCALCULATE BlockCompact and reduces number of networks
Mathematical algorithmSCL LanguageSyntax close to C/Pascal, ideal for formulas
Trigonometric calculationSCL or CALCULATEIndividual boxes are too bulky
Signal boundingLIMIT BlockSiemens standard, very robust
Index resetINC/DEC InstructionVery 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.

Simplify your complex calculations

Describe your physical formula or algorithm, T-IA Connect will generate the CALCULATE block or optimized SCL code with data validation.