beautypg.com

B&B Electronics VFG3000 - Manual User Manual

Page 193

background image

W

RITING

E

XPRESSIONS

T

YPE

C

ONVERSION

R

EVISION

1

P

AGE

177

T

YPE

C

ONVERSION

Normally, Fieldbus Gateway Manager will automatically decide when to switch from
evaluating an expression in integer math to evaluating it using floating-point. For example, if
you divide an integer value by a floating-point value, the integer will be converted to
floating-point before the division is carried out. However, there will be some situations where
you want to force a conversion to take place.

For example, suppose you are adding together three integers that represent the levels in three
tanks, and then dividing the total by the tank count to obtain the average level. If you use an
expression such as

(Tank1+Tank2+Tank3)/3

then your result may not be as accurate as you

demand, as the division will take place using integer math, and the average will not contain
any decimal places. To force Fieldbus Gateway Manager to evaluate the result using
floating-point math, the simplest technique is to change the

3

to

3.0

, thereby forcing Fieldbus

Gateway Manager to convert the sum to floating-point before the division is performed. A
slightly more complex technique is to use syntax such as

float(Tank1+Tank2+Tank3)/3

.

This invokes what is known as a “type cast” on the term in parentheses, manually converting
it to floating-point.

Type casts may also be used to convert a floating-point value to an integer value, perhaps
deliberately giving-up some precision from an intermediate value before storing it in a PLC
register. For example, the expression

int(cos(Theta)*100)

will calculate the cosine of an

angle, multiply this value by 100 using floating-point math, and then convert it to an integer,
dropping any digits after the decimal place.

C

OMPARING

V

ALUES

You will quite often find that you wish to compare the value of one data with another, and
make a decision based on the result. For example, you may wish to define a flag formula to
show when a tank exceeds a particular value, or you may wish to use an

if

statement in a

program to execute some code when a motor reaches its desired speed. The following
comparison operators are provided…

O

PERATOR

P

RIORITY

E

XAMPLE

Equal To

Group 7

Data == 100

Not Equal To

Group 7

Data != 100

Greater Than

Group 6

Data > 100

Greater Than or Equal To

Group 6

Data >= 100

Less Than

Group 6

Data < 100

Less Than or Equal To

Group 6

Data <= 100

Each operator produces a value of 0 or 1, depending on the condition it tests. The operators
can be used on integers, floating-point values, or text strings. If strings are compared, the
comparison is case-insensitive ie. “abc” is considered equal to “ABC”.