6-107
6 Programming
NJ-series CPU Unit Software User’s Manual (W501)
6-5 Programming Languages
6
6-5-3 Structured Text Language
Casting Rules When You Assign the Right-hand Value to the Left-hand Side
In the following chart, a cast is performed if an arrow connects the data type of the source to the
data type of the assignment destination. Any combination that is not connected will cause a building
error.
Even if the arrow does not connect directly to a data type, you can still perform assignments for the
data types. For example, SINT->USINT->UINT->UDINT->ULINT are all connected, so you can write
an assignment such as ULINT:=SINT.
Observe the following precautions when casting UDINT to ULINT data, DINT to LINT data, or
DINT to LREAL data.
All of these are casts from 32-bit data to 64-bit data. If the result of the calculation of the right
side of the assignment statement exceeds the range of 32-bit data, the correct value may not be
assigned.
Example: For the following assignment statements, the result of the addition in the third state-
ment exceeds the range of 32-bit data. An overflow will result and 0 will be assigned
to LintVar.
UdintVar := UDINT#16#FFFF_FFFF; // Upper limit of 32-bit data
DintVar := DINT#1; // 1
LintVar := (UdintVar + DintVar)/DINT#2; // (Upper limit of 32-bit data + 1)/2
In a case like this one, convert the data to 64-bit data before you perform the calculation. To do
this for the above example, change the assignment status as shown below.
LintTmp1 := UDINT_TO_LINT(UDINT#16#FFFF_FFFF); //Convert UDINT to LINT data.
LintTmp2 := DINT_TO_LINT(DINT#1); // Convert DINT to LINT data.
LintVar := (LintTmp1 + LintTmp2) / DINT#2;
U
SINT
U
DINT
U
LINT
S
INT
D
INT
UINT
I
NT
L
INT
R
EAL
L
REAL
When you assign the value, the sign and absolute value of the number do not change.
When you assign the value, the sign and absolute value of the number may change.
Example: intVar := -1; (* intVar := 16#FFFF *)
uintVar := 1;
uintVar := intVar; (* uintVar:= 16#FFFF, or −1 was
assigned but the result is 65535 *)