Topic: DMD0119

Help File Version: 2.9.4.37

MATH Divide Operator


The Math Divide operator ( / ) will calculate the quotient of a numerator and a denominator.

 

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

 

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. The most common mistake occurs when dividing two integer values but the desired result is a fractional real result. Use the TOREAL() math function to promote an integer value or integer expression to its Real equivalent

 

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 = 10000 and D2 = 25

 

D0 = D1 / D2

D0 = 10000 / 25
D0 = 400

 

Both the numerator and denominator are integer memory locations.

 

The quotient is placed in an integer memory location.

 

D0 = (D1 / D2) / 25

 

D0 = (10000 / 25) / 25
D0 = 16

 

The numerator and both denominators are integer memory locations, the third is an integer constant.

 

The quotient is placed in an integer memory location.

 

D0 = D1 / (D2 / 25)

 

D0 = 10000 / (25 / 25)
D0 = 10000 / 1

D0 = 10000

 

The numerator and both denominators are integer 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 quotient is placed in an integer memory location.

 

 

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

 

D0 = D1 / R1

D0 = 1000.0 / 2.51

D0 = 398
 

The numerator is an integer memory location, the denominator is a Real memory location.

 

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

 

The quotient is placed in a Signed integer location which WILL NOT retain the portion after the decimal point.
 

R0 = D1 / R1

 

R0 = 1000.0 / 2.51

R0 = 1002.4064
 

The numerator is an integer memory location, the denominator is a Real memory location.

 

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

 

The quotient 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 D1 = 5

 

V0 = D1 / 2

V0 = 5 / 2

V0 = 2

 

 

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

 

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

 

The result will NOT be the expected 2.5 because the calculation will be performed using integer math which will not preserve the remainder portion of the calculation.

 

R0 = D1 / 2

 

R0 = 5 / 2

R0 = 2.0

 

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

 

The quotient is stored in a Real memory location.

 

The result will NOT be the expected 2.5 because the calculation will be performed using integer math which will not preserve the remainder portion of the calculation even though the result is stored in a Real location.

 

R0 = TOREAL(D1) / 2

 

R0 = 5.0 / 2

R0 = 2.5

 

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

 

The quotient is stored in a Real memory location.

 

The TOREAL() function will promote the value in D1 to a Real number which will force the calculation to be performed using Real math. Doing this will retain the remainder portion of the calculation when it is stored as a Real value.

 

 

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

 

D0 = D1 / T0.Acc

 

D0 = 1000 / 500

D0 = 2

 

The numerator is an integer memory location, 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 quotient is placed in an integer memory location.
 

 

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 = 1.564695

 

The numerator is a floating point memory location, the denominator is using the 'Real and DWord' cast operators.

 

The quotient 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