Basic Control Engine scripts allows expressions to involve data of different types. When this occurs, the two arguments are converted to be of the same type by promoting the less precise operand to the same type as the more precise operand. For example, the Basic Control Engine will promote the value of i% to a Double in the following expression:
result# = i% * d#
In some cases, the data type to which each operand is promoted is different than that of the most precise operand. This is dependent on the operator and the data types of the two operands and is noted in the description of each operator.
If an operation is performed between a numeric expression and a String expression, then the String expression is usually converted to be of the same type as the numeric expression. For example, the following expression converts the String expression to an Integer before performing the multiplication:
result = 10 * "2" 'Result is equal to 20.
There are exceptions to this rule as noted in the description of the individual operators.
Type Coercion
The Basic Control Engine performs numeric type conversion automatically. Automatic conversions sometimes result in overflow errors, as shown in the following example:
d# = 45354
i% = d#
In this example, an overflow error is generated because the value contained in d# is larger than the maximum size of an Integer.
Rounding
When floating-point values (Single or Double) are converted to integer values (Integer or Long), the fractional part of the floating-point number is lost, rounding to the nearest integer value. The Basic Control Engine uses Baker's rounding:
If the fractional part is larger than .5, the number is rounded up.
If the fractional part is smaller than .5, the number is rounded down.
If the fractional part is equal to .5, then the number is rounded up if it is odd and down if it is even.
The following table shows sample values before and after rounding:
Before Rounding |
After Rounding to Whole Number |
2.1 |
2 |
4.6 |
5 |
2.5 |
2 |
3.5 |
4 |
Default Properties
When an OLE object variable or an Object variant is used with numerical operators such as addition or subtraction, then the default property of that object is automatically retrieved. For example, consider the following:
Dim Excel As Object
Set Excel = GetObject(,"Excel.Application")
MsgBox "This application is " & Excel
The above example displays This application is Microsoft Excel in a dialog box. When the variable Excel is used within the expression, the default property is automatically retrieved, which, in this case, is the string Microsoft Excel. Considering that the default property of the Excel object is .Value, then the following two statements are equivalent:
MsgBox "This application is " & Excel
MsgBox "This application is " & Excel.Value
E |