Topic: DMD0096

Help File Version: 2.9.4.37

MATH Add Operator


The Math Add operator ( + ) will calculate the sum of two or more numbers or expressions.

 

The numbers and expressions can be any mix of signed integers, unsigned integers, real (floating point) numbers or discrete values. They can be any numeric or discrete memory location or any numeric or discrete structure member.

 

All discrete and 16-bit integer values are promoted to 32-bit 2's complement signed integer values. If a Real value is included in the equation then all of the values will be promoted to Real values and all calculations will be performed using Real numbers.

 

Traditional math precedence rules are used to solve the math expression. The use of parentheses to remove any ambiguity in the processing order is encouraged.

 

Refer to the examples below for caveats when using numbers of different types:

 

 

Signed Integers: assume D1 = 1000 and D2 = 1024

 

D0 = D1 + D2

D0 = 1000 + 1024
D0 = 2024

 

Both addends are integer memory locations.

 

The result is placed in an integer memory location.

 

D0 = D1 + D2 + 251

D0 = 1000 + 1024 + 251
D0 = 2275
 

Two addends are integer memory locations, the third is an integer constant.

 

The result is placed in an integer memory location.

 

 

Mixing Signed Integers and Floating Point values: assume D1 = 1000 and R1 = 2.5

 

D0 = D1 + R1

D0 = 1000 + 2.51

D0 = 1002
 

One addend is an integer memory location, the other is a Real memory location.

 

D1 will be promoted to a Real number then added to R1.

 

The result is placed in an integer which WILL NOT retain the portion after the decimal point.
 

R0 = D1 + R1

R0 = 1000 + 2.51

R0 = 1002.51
 

One addend is a memory location, the other is a Real memory location.

 

D1 will be promoted to a Real number then added to R1.

 

The result is placed in a floating point memory location which WILL retain the portion after the decimal point.
 

 

Mixing 32-bit Integers and 16-bit Integers: assume D3 = 1

 

V0 = D3 + 65535

V0 = 1 + 65535
V0 = 0

 

One addend is an integer memory location, the other is an integer constant.

 

The result is stored in a 16-bit Unsigned memory location.

 

A 16-bit Unsigned memory location can stored values from 0 to 65535. The value of 65536 CAN NOT be stored in a 16-bit location, only the lowest 16 bits of the result will be stored in the Unsigned memory location.

 

N0 = D3 + 32767

N0 = 1 + 32768

N0 = -1

 

One addend is an integer memory location, the other is an integer constant.

 

The result is 32768, but will be stored in a 16-bit Signed memory location.

 

A 16-bit Signed memory location stores 2's-complement values from -32768 to 32767. The result's value of 32768 in 16-bit, 2's complement form is -1.

 

 

Numeric Structure Members: assume D1 = 1000 and T0-Acc = 567ms

 

D0 = D1 + T0.Acc

 

D0 = 1000 + 567

D0 = 1567

 

One addend is an integer memory location, the other is a structure member.

 

The .Acc member of a Timer structure contains the amount of time in milliseconds that has accumulated in a Timer.

 

The result is placed in an integer memory location.
 

 

Discrete memory locations: assume D1 = 1000 and X0 = ON

 

D0 = D1 + X0

D0 = 1000 + 1

D0 = 1001
 

One addend is an integer memory location, the other is a discrete input.

 

The result is placed in an integer memory location.
 

A discrete location will have a value of 0 if the status of that location is OFF.

A discrete location will have a value of 1 if the status of that location is ON.

 

 

Casting Numeric Values: assume R1 = 2.51, and DLV2000 and DLV2001 as a 32-bit Real = 1234.56

 

R0 = R1 + DLV2000:RD

 

R0 = 2.51 + 1234.56

R0 = 1237.07

 

One addend is a floating point memory location, the other is using the 'Real and DWord' cast operators.

 

The result is placed in a floating point memory location.

 

DLV memory locations are 16-bit Unsigned used by external DirectLOGIC devices. In this example a remote DirectLOGIC CPU has written a 32-bit floating point value into two successive DLV memory locations. Using the :RD cast operator will interpret the 32-bit value in DLV2000 and DLV2001 as a REAL number.

 

 


See Also:

Add

Subtract

Multiply

Divide


Modulus / Remainder

Raise to a Power


Less Than

Less than or Equal To

 

Equal To

Not Equal To

 

Greater Than

Greater Than or Equal To


Logical AND

Logical OR


Bit-wise AND

Bit-wise OR

Bit-wise XOR


Shift Left

Shift Right

Unsigned Shift Right

 

Negate

Bit-wise Invert

Logical NOT

 


Related Topics:

MATH - Calculate Expression