Topic: DMD0120

Help File Version: 2.10.0.636

MATH Modulus / Remainder Operator


The Modulus / Remainder operator ( % ) will perform modulo division on a numerator and a denominator. The result of a modulo division is the remainder of an integer division of the given positive numerator and denominator. All calculations are performed using integer division meaning that even if the numerator or denominator are Real numbers only the integer portion of the Real value is used in the calculation.

 

Modulo division using negative integer values for either the numerator or denominator is undefined, so if the parameters are memory locations consider using the ABS() math function to guarantee that only positive integers are used in the division calculation. Attempting to perform a division calculation when the denominator is 0 will generate a quotient of 0 and a "Divide by Zero - $DivideByZero (ST141)" error.

 

The numerator and denominator 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.

 

Refer to the examples below:

 

Signed Integers: assume D1 = 7 and D2 = 3 and D3 = -3

 

D0 = 7 % 3

D0 = 1

 

Both the numerator and denominator are integer constants

 

The result is placed in an integer memory location.

 

D0 = 7 % ABS(D3)

 

D0 = 7 % 3
D0 = 1

 

The numerator is an integer constant and the denominator is an integer memory location.

 

The absolute value of the denominator will be used in the modulo calculation.

 

The result is placed in an integer memory location.

 

D0 = 7.5 % 3.67

 

D0 = 7 % 3

D0 = 1

 

The numerator and both denominators are Real constants.

 

Modulo division only operates on positive integers, so both Real values will be demoted to 32-bit signed integers before the modulo calculation is done.

 

The result is placed in an integer memory location.

 

 

Numeric Structure Members: Timer T0.Acc = 30ms

 

D0 = 70 % T0.Acc

 

D0 = 70 % 30

D0 = 10

 

The numerator is an integer constant, the denominator 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.
 

 

Casting Numeric Values: assume D1 = 123456 and DLV2000 and DLV2001 as a 32-bit Integer = 78901

 

D0 = D1 % DLV2000:D

 

D0 = 123456 % 789

D0 = 372

 

The numerator is an integer memory location, the denominator is using the DWord cast operator.

 

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 integer value into two successive DLV memory locations. Using the :D cast operator will interpret the two successive 16-bit values in DLV2000 and DLV2001 as a single 32-bit integer 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