MCL Scripting: Math Operators

Introduction

MCL, like other programming languages, has a large number of mathematical operators for performing calculations and for evaluating expressions. This application note documents the full list of operators.

Precedence

Operator precedence is the order in which operators are applied. Each operator has a priority by which it is applied. Caution must be used when using multiple operators for more complex calculations, the order in which the calculation is written out does not specify the order in which operators are applied. Parentheses can be used to control and group operations to make calculations easier to write and understand.
1st NOT, ABS, SIN, COS, – (NEG), SQR, RANDOM, TOINT, TOFLOAT, BIN2BCD, ~(Binary NOT),
!(Binary NOT), NOT(Logical NOT), FABS, FSQRT, FSIN, FCOS, FTAN, FASIN, FACOS, FATAN,
FSINH, FCOSH, FTANH, FATANH, FLN, FEXP
2nd Dig
3rd MAX, MIN
4th *, **, */, /, //
5th +, –
6th <<, >>
7th <, <=, =, >=, >, <>
8th &, |, ^, &/, |/, ^/
9th And, Or, Xor

– Negation

Changes the value of an expression from positive to negative.
Example:

num var word
num = 1235
num = num + -1 ; num variable is now equal to 1234

ABS

Returns the absolute value of an expression.
Example:

num var sword
num = -1234
num = abs(num) ; num is now equal to 1234

SIN

Returns the integer sine of an expression. In MCL SIN and COS integer operations divide a circle in to 256 parts. SIN and COS return an integer value between -127 and 128.
Example:

num var byte
res var byte
num = 64
res = sin num ;

COS

Returns the integer cosine of an expression. In MCL SIN and COS integer operations divide a circle in to 256 parts. SIN and COS return an integer value between -127 and 128.

Example:

num var byte
res var byte
num = 64
res = cos num ;

SQR

Returns the integer part of the square root of a value.
Example:

num var byte
res var byte
num = 64
res = sqr num ; evaluates to 8

BIN2BCD

Converts expression from binary to packed binary coded decimal format.
Example:

num var byte
res var byte
num = 93
res = bin2bcs num

Figure 1: A diagram detailing the bin2bcd conversion.

RANDOM

Returns a random 32 bit number generated from a seed value.
Example:

seed var word
res bar word
seed = 1234
res = random seed

– Subtraction

Used to subtract one value from another.
Example:

num var byte
res var byte
num = 100
res = 100 – 1 ; result is 99

+ Addition

Used to add one value to another.
Example:

num var byte
res var byte
num = 99
res = 99 + 1 ; result is 100

* Multiplication

Used to multiply values together.
Example:

num var word
res var word

num = 100
res = 100 * 2 ; result is 200

/ Division

Use to divide one value by another. Result can be an integer or floating point value depending on values used in the expression.
Example:

num var word
res var word

num = 76
res = num/7 ; result is 10, the fraction part is discarded because integers are used in the expression

f var word
r var word

f = 99.0
r = f/8.0 ; result is 12.375

** High Multiplication

Returns high 32 bits of an integer multiplication result. If two longs are multiplied together this operator returns the upper 32 bits of the result.
Example:

res var long
res = 80000 ** 80000

*/ Fractional Integer Multiplication.

Allows for a value with a fractional part to be multiplied against another value. The value must be a long variable. The integer portion is stored in the high portion of the variable and the fractional part is stored in the low section of the variable. The result of the multiplication is the integer portion of the calculated value.
Example:

res var word
multiplier var long

multiplier.highword = 2
multiplier.lowword = 32782 ; “multiplier” is equal to 2.5

res = 346 */ multiplier ; result is 865

// Modulus

Returns the remainder from integer division.
Example:

num var byte
num = 5 // 2
if num = 1 then
puts 0,[“Dividend is odd”, 13]
else
puts 0,[“Dividend is even”, 13]

MAX

MAX returns the smaller of two expressions.
Example:

x var word
y var word
y = 10
x = y max 14 ; x is set to 10

a var word
b var word
b = 20
a = b max 10 ; a is set to 10

MIN

MIN returns the larger of two expressions.
Example:

x var word
y var word
y = 20
x = y min 10 ; x is set to 20

a var word
b var word
b = 5
a = b min 10 ; a is set to 10

DIG

Return the nth digit of a value.
Example:

num var word
res var word
num = 12345
res = num dig 4 ; result is 2

<< Shift Left

Shift all of the bits of a value left by the specified amount.

Example:

num var word
num = 1
num = num << 1 ; num is now 2
num = num << 1 ; num is now 4
num = num << 1 ; num is now 8

>> Shift Right

Example:
Shift all of the bits of a value right by a specified amount.

num var word
num = 8
num = num >> 1 ; num is now 4
num = num >> 1 ; num is now 2
num = num >> 1 ; num is now 1

& Binary AND

Performs the AND operation on the binary representation of values.
Truth table:
0 and 0: 0
0 and 1: 0
1 and 0: 0
1 and 1: 1
Example:

a var byte
b var byte
c var byte

b = 0
c = 1

a = b & c ; a is zero

b = 1
c = 1
a = b & c ; a is one

| Binary OR

Perform the OR operation on the binary representation of values.
Truth table:
0 | 0: 0
0 | 1: 1
1 | 0: 1
1 | 1: 1
Example:

a var byte
b var byte
c var byte

b = 0
c = 0
a = b | c ; a is zero

b = 1
c = 0
a = b | c ; a is one

^ Binary math XOR.

Perform the XOR operation on the binary representation of values.
Truth table:
0 ^ 0: 0
1 ^ 0: 1
0 ^ 1: 1
1 ^ 1: 0
Example:

a var byte
b var byte
c var byte

b = 0
c = 1

a = b ^ c ; a is one

&/ Binary math NAND.

Performs the NAND operation on the binary representation of values.
Truth table:
0 ^ 0: 1
1 ^ 0: 1
0 ^ 1: 1
1 ^ 1: 0
Example:

a var byte
b var byte
c var byte

b = 0
c = 0

a = b &/ c ; a is one

|/ Binary math NOR.

Performs the NOR operation on the binary representation of values.
Truth table:
0 ^ 0: 1
1 ^ 0: 0
0 ^ 1: 0
1 ^ 1: 0
Example:

a var byte
b var byte
c var byte

b = 0
c = 0

a = b |/ c ; a is one

^/ Binary math NXOR.

Performs the NXOR operation on the binary representation of values.
Truth table:
0 ^ 0: 1
1 ^ 0: 0
0 ^ 1: 0
1 ^ 1: 1
Example:

a var byte
b var byte
c var byte

b = 1
c = 1

a = b ^/ c ; a is one

= Is equal to.

Evaluates whether a value is equal to another value.
Example:

num var byte
num = 10
if num = 10 then
puts 0,[“Num is equal to 10”, 13]
else
puts 0,[“Num is not equal to 10”, 13]

<> Is not equal to.

Evalutes whether a value is not equal to another value.
Example:

num var byte
num = 4
if num <> 5 then
puts 0,[“Num is not equal to 5”,13]
else
puts 0,[“Num is equal to 5”,13]

< Is less than.

Evaluates whether a value is less than another value.
Example:

num var byte
num = 4
if num < 5 then
puts 0,[“Num is less than 5”,13]
else
puts 0,[“Num is greater than or equal to 5”]

> Is greater than.

Evaluates whether a value is greater than another value.
Example:

num var byte
num = 5
if num > 4 then
puts 0,[“Num greater than 4”,13]
else
puts 0,[“Num not greater than 4”,13]

<= Is less than or equal to.

Evaluates whether a value is less than or equal to another value.
Example:

num var byte
num = 4
if num <= 5 then
puts 0,[“Num is less than or equal to 5”,13]
else
puts 0,[“Num is not less than or equal to 5”,13]

>= Is greater than or equal to.

Evaluates whether a values is greater than or equal to another value.
Example:

num bar byte
num = 5
if num >= 4 then
puts 0,[“Num is greater than or equal to 5”]
else
puts 0,[“Num is not greater than or equal to 5”,13]

AND Logical AND.

Performs a logical AND operation on two values. In MCL any non-zero values is considered TRUE, a value of zero is considered false.
Truth Table:
True True: True
True False: False
False True: False
False False: False
Example:

foo var byte
bar var byte
foo = 1
bar = 10

if foo = 1 and bar bar = 10 then
; do something

OR Logical OR.

Performs a logical OR operation on two values. In MCL any non-zero values is considered TRUE, a value of zero is considered false.
Truth Table:
True True: True
True False: True
False True: True
False False: False
Example:

foo var byte
foo = 10

if foo = 10 or foo = 20 then
; do something

XOR Logical XOR.

Performs a logical XOR operation on two values. In MCL any non-zero values is considered TRUE, a value of zero is considered false.
Truth Table:
True True: False
True False: True
False True: True
False False: False
Example:

foo var byte
bar var byte

foo = 1
bar = 0

if foo xor bar then
; do something

NOT Logical NOT.

Performs a logical NOT operation on a value. In MCL any non-zero values is considered TRUE, a value of zero is considered false.
Example:

num var byte
num = 0

if not num then
; do something

TOINT Convert a Floating Point value to a Integer

Convert a floating point value to an integer value, the fractional part of the floating point value is truncated.
Example:

num var word
numf var float

numf = 36.14
num = toint numf

TOFLOAT Convert an Integer value to a Floating Point

Converts an integer value to a floating point value.
Example:

num var word
numf var float

num = 1234
numf = tofloat num

FABS Floating Point Absolute Value

Calculates the absolute value of a floating point value.
Example:

num var float
num -1234.56
num = fabs(num)

FSQRT Floating Point Square Root

Calculates the square root of a floating point value.
Example:

num var float
num = 36.00
num = fsqrt num

FSIN Floating Point Sine

Calculates the sine of a floating point value in radians.
Example:

num var float
num = 2.0
num = fsin num

FCOS Floating Point Cosine

Calculates the cosine of a floating point value in radians.
Example:

num var float
num = 2.0
num = fcos num

FTAN Floating Point Tangent

Calculates the tangent of a floating point value in radians.
Example:

num var float
num = 2.0
num = ftan num

FASIN Floating Point ArcSine

Calculates the floating point arc sine of a value in radians.
Example:

num var float
num = 2.0
num = fasin num

FACOS Floating Point ArcCosine

Calculates the floating point arc cosine of a value in radians.
Example:

num var float
num = 2.0
num = facos num

FATAN Floating Point Arc Tangent

Calculates the floating point arc tangent of a value in radians.
Example:

num var float
num = 2.0
num = fatan num

FSINH Floating Point Hyperbolic Sine

Calculate the floating point hyperbolic sine of a value.
Example:

num var float
num = 2.0
num = fsinh num

FCOSH Floating Point Hyperbolic Cosine

Calculate the floating point hyperbolic cosine of a value.
Example:

num var float
num = 2.0
num = fcosh num

FTANH Floating Point Hyperbolic Tangent

Calculate the floating point hyperbolic tangent of a value.
Example:

num var float
num = 2.0
num = ftanh num

FATANH Floating Point Hyperbolic ArcTangent

Calculate the floating point hyperbolic arc tangent of a value.
Example:

num var float
num = 2.0
num = fatanh num

FLN Floating Point Natural Log

Calculates the floating point natural log of a value.
Example:

num var float
num = 2.0
num = fln num

FEXP Floating Point Exponent

Calculates the floating point natural exponent of a value.
Example:

num var float
num = 0.693115
num = fexp num