Compile-time Calculations

Top  Previous  Next

Tibbo Basic always tries to pre-calculate everything that can be pre-calculated during compilation. For example, if you write the following code:

 

 

dim x,y as byte

y=5

x=5+10+y 'compiler will precalculate 5+10 and then the Virtual Machine will only have to do 15+y

 

 

Pre-calculation reduces program size and speeds up execution.

When processing a string like "x=50000" compiler first determines the variable of what type would be necessary to hold the fixed value and chooses the smallest sufficient variable type. For example, for 50000 it is, obviously, word. Next, compiler applies the same rules of type conversion as between two actual variables. Hence, in our example this will be like performing byte= word.

One additional detail. Large fixed values assigned to variables of real type must be written with fractional part, even if this fractional part is 0. Consider the following example:

 

 

dim r as real

r=12345678901 'try to compile this and you will get "constant too big" error.

 

 

Compiler will notice that this constant does not fit even into dword and will generate an error. Now, try this:

 

 

dim r as real

r=12345678901.0 'that will work!

 

 

On seeing ".0" at the end of the value compiler will realize that the value must be treated as a real one (floating-point format) and process the value correctly.

So, why is it possible for compiler to automatically process values that fit into 8, 16, and 32 bits, but at the same time requires your conscious effort to specify that the value needs to be treated as a floating-point one?

This is because floating-point calculations are imprecise. The value "12345678901", when converted into a floating-point format, will not be exactly the same! The floating-point value will only approximate the value we intended to have!

For this reason we require you, the programmer, to make a conscious choice when specifying such values. By adding ".0" you acknowledge that you understand potential imprecision of the result.