Control Solutions is your source for LonWorks I/O.
Tutorial #3

Assignment Statements and Math: Plain Old "a=b+c"

Simply begin an assignment statement with the variable you wish to assign. For example:

OurAverage = (YourVariable + MyVariable) / 2;
A = B + C;
phi = sin (theAngle);

The only thing you need to watch is type casting. Types "int" and "float" may be mixed as operands in an expression; however, lesser types such as uint8 or uint16 cannot be operands without explicit conversion to int or float. The following example illustrates acceptable mixing of int and float.

program test
declare
thatVar: int;
otherVar: float;

begin
otherVar = thatVar * otherVar;
thatVar = thatVar * otherVar;
end

Math Functions & Operators

The following is a summary of math operators recognized in PL/i. These operate on either int or float data types.

+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Modulus
abs(n)
Returns absolute value of 'n'

The following is a summary of math functions recognized in PL/i. These operate on int or float data types, but always return a float data type (except rand). The trigonometry functions operate on units of radians (not degrees - see conversion example below).

acos(n)
Calculates arc cosine of 'n'
asin(n)
Calculates arc sine of 'n'
atan(n)
Calculates arc tangent of 'n'
cos(n)
Calculates cosine of 'n'
ln(n)
Produces the natural logarithm of n
log(n)
Products the logarithm of n
pow(x,y)
Produces x raised to the power of y
sin(n)
Calculates sine of 'n'
sqrt(n)
Calculates square root of 'n'
tan(n)
Calculates tangent of 'n'
rand()
Provides a random integer in the range of 0 to 2,147,483,647 on the server, or 0..32,767 on the I/O coprocessor
(use srand(n) to seed random number generator)

Logic Operators for data

The following is a summary of logic operators recognized in PL/i for use on integer data types (operate on int, return int).

&
Logical AND, bitwise, e.g., c = a & b;
~
Logical NOT, bitwise (invert each bit), e.g., a = ~b;
|
Logical OR, bitwise , e.g., c = a | b;
^
Logical Exclusive OR, bitwise , e.g., c = a ^ b;
<<
Logical Shift Left, bitwise,
e.g., a = a << 3; will shift the contents of "a" left 3 bits
>>
Logical Shift Right, bitwise,
e.g., a = a >> 5; will shift the contents of "a" right 5 bits

The following example illustrates the use of a couple of trig functions, plus a couple of user functions, and conversion between degrees and radians.

program test
declare
varDeg1: float;
varDeg2: float;
varRad: float;
varSin: float;
error: int;

procedure radians (deg: float; var rad: float)
declare
begin
rad = deg * 3.14159 / 180.0;
end;

procedure degrees (rad: float; var deg: float)
declare
begin
deg = rad * 180.0 / 3.14159;
end;

begin
varDeg1 = 90.0;
radians(varDeg1, varRad);
varSin = sin(varRad);
varRad = asin(varSin);
degrees(varRad, varDeg2);
if varDeg1 <> varDeg2 then error = -1;
else error = 0;
endif;
end