Topic: DMD0121

Help File Version: 2.10.0.636

Math Raise to a Power Operator


The Math Raise to a Power operator ( ** ) will calculate the result of a base raised to the power of an exponent. The result of this calculation will always be a Real (floating point) value and should be placed in a Real memory location to preserve the fractional portion. This is especially important if the exponent is negative or if the exponent is a Real value,

 

Any value raised to the 0 power is 1. The value 0 raised to any power is 0. Note: 0 raised to the 0 power is 1.

 

The base and exponent 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 = 2 and D2 = 3

 

D0 = D1 ** D2

D0 = 2 ** 3
D0 = 8

 

Both the base and the exponent are integer memory locations.

 

The result is placed in an integer memory location.

 

D0 = (D1 ** D2) ** D1

 

D0 = (2 ** 3) ** 2
D0 = 8 ** 2

D0 = 64

 

The base and exponents are integer memory locations.

 

The result is placed in an integer memory location.

 

D0 = D1 ** (D2 ** D1)

 

D0 = 2 ** (3 ** 2)
D0 = 2 ** 9

D0 = 512

 

The base and exponents are integer memory locations.

 

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.

 

R0 = D1 ** 0.5

R0 = 2 ** 0.5

R0 = 1.414

The base is an integer memory locations, the exponent is a Real constant.

 

The exponent is non-integer, which will generate a non-integer result. Place the result in a Real memory location to preserve the fractional portion.

 

The result is placed in a Real memory location.

 

R0 = D1 ** -3

R0 = 2 ** -3

R0 = 0.1250

The base is an integer memory locations, the exponent is an integer constant.

 

The exponent is negative, which will generate a non-integer result. Place the result in a Real memory location to preserve the fractional portion.

 

The result is placed in a Real memory location.

 

 

Numeric Structure Members: assume D1 = 10 and Timer T0.Acc = 5ms

 

D0 = D1 ** T0.Acc

 

D0 = 10 ** 5

D0 = 100000

 

The base is an integer memory location, the exponent 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 DLV2000 and DLV2001 as a 32-bit Real = 123.45

 

R0 = DLV2000:RD ** 2

 

R0 = 123.45 ** 2

R0 = 15239.9

 

The base is using the 'Real and DWord' cast operators, the exponent is an integer constant.

 

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