beautypg.com

Type declaration characters, Type conversion (promotion), Effects of type conversions on accuracy – BrightSign BrightScript 2 Reference Guide User Manual

Page 10

background image

10

This example will return a type mismatch (a$ is a string, and can not contain “invalid”).
Many functions that return objects can return invalid as well


Type Declaration Characters

A type declaration character may be used at the end of a variable or literal to fix its type.
Variables with the same identifier but separate types are separate variables. For example,
a, a$, and a% are all independent.

Character Type

Examples

$

String

A$, ZZ$

%

Integer

A1%, SUM%

!

Single-Precision (float)

B!, N1!

#

Double-Precision (double)

A#, 1/3#, 2#

Type Conversion (Promotion)

When operations are performed on one or two numbers, the result must be typed as
integer, double or single-precision (float). When a +, -, or * operation is performed, the
result will have the same degree of precision as the most precise operand. For example, if
one operand is integer, and the other double-precision, the result will be double precision.
Only when both operands are integers will a result be integer. If the result of an integer *,
-, or + operation is outside the integer range, the operation will be done in double
precision and the result will be double precision.

Division follows the same rules as +, * and -, except that it is never done at the integer
level: when both operators are integers, the operation is done as float with a float result
.
During a compare operation (< , >,=,etc.) the operands are converted to the same type
before they are compared. The less precise type will always be converted to the more
precise type.

The logical operators AND, OR and NOT first convert their operands to Boolean. The
result of a logical operation is always a Boolean.

Effects of Type Conversions on Accuracy

When a number is converted to integer type, it is "rounded down"; i.e., the largest integer,
which is not greater than the number is used. (This is the same thing that happens when
the INT function is applied to the number.)

When a number is converted from double to single precision, it is "4/5 rounded" (the least
significant digit is rounded up if the fractional part > =5. Otherwise, it is left unchanged).