Topic: DMD0097

Help File Version: 2.9.4.37

Math Subtract Operator


The Math Subtract operator ( - ) will calculate the difference between two or more numbers or expressions by subtracting the value on the right side of the operator from the value on the left side of the operator.

 

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.

 

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.

 

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.

 

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

 

 

Signed Integers: assume D1 = 2000 and D2 = 1024

 

D0 = D1 - D2

D0 = 2000 - 1024
D0 = 976
 

Both values are integer memory locations.

 

The result is placed in an integer memory location.

 

D0 = (D1 - D2) - 251

 

D0 = (2000 - 1024) - 251
D0 = 725

 

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

 

The result is placed in an integer memory location.

 

D0 = D1 - (D2 - 251)

D0 = 2000 - (1024 - 251)

D0 = 2000 - 773
D0 = 1227
 

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

 

The use of parentheses specifies the order of operation which yields a different result than the same values above.

 

The result is placed in an integer memory location.

 

 

Mixing Signed Integers and Floating Point values: assume D1 = 2000 and R1 = 2.51

 

D0 = D1 - R1

D0 = 2000 - 2.51

D0 = 1997
 

One value 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 = 2000 - 2.51

R0 = 1997.49
 

One value 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 = 2

 

One value is a 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 stores values from 0 to 65535. The negative result of -65534 CAN NOT be stored in a 16-bit Unsigned location. The 2's complement value of -65534 is 2.

 

N0 = D3 - 65535

N0 = 1 - 65535

N0 = 2

 

One value is a memory location, the other is an integer constant.

 

The result is -65534, 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 -65534 in 16-bit, 2's complement form is 2.

 

 

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

 

D0 = D1 - T0.Acc

 

D0 = 1000 - 567

D0 = 433

One value is a 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 is ON

 

D0 = D1 - X0

D0 = 1000 - 1

D0 = 999
 

One value is a 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 = 1234.56, and DLV2000 and DLV2001 as a 32-bit Real = 789.01

 

R0 = R1 - DLV2000:RD

 

R0 = 1234.56 - 789.01

R0 = 445.55

One value 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