Mathematics

Introduction

The script interpreter understands numbers, both real numbers (fractionals or floats, such as 1.23) and whole (integers such as 68).

With these numbers, which may be either literals (hard-coded in script) or read from a teletext frame, you can perform a variety of actions and also you can use the numbers in decision-making.

There are 26 variables available for use; that is, there are twenty six bits of memory in which you can store numbers. They are called A to Z, each letter of the alphabet is a variable.
The first sixteen (A through P) are integer variables and can store only whole numbers. The remaining ten (Q through Z) are float variables and can store numbers with up to nine decimal places of precision.

 

Specifying numbers

You can specify numbers in basic form just by writing it:
set R to 123.45
add(R, 32)

You can specify integer numbers in hex (base 16) by prefixing with an ampersand '&' symbol:

channelid(A)
if (A ! &EF01) error("Wrong channel!")

You can specify integer numbers in binary (base 2) by prefixing with a percent '%' symbol:

set M to %11110000
and(A, %1111)

The following examples all set 'A' to 123:

set A to 123
set A to &7B
set A to %1111011

For negatives, simply prefix with a minus symbol:

set A to -123
set A to -&7B
set A to -%1111011

All variables are zero when the script begins.

 

Basic mathematics

Maths plays a part in all scripting when you get beyond the really basic things.

You have a number of mathematical possibilities:

Addition C = A + B set C to A
add(C, B)
For increments, you could use "C++".
Subtraction C = A - B set C to A
sub(C, B)
For decrements, you could use "C--".
Multiplication C = A × B mul(C, A, B)  
Division C = A ÷ B div(C, A, B)  
Modulus C = A MOD B mod(C, A, B)  
With these, most calculations can be performed.

 

More complex maths

More complicated maths requires more complicated commands:

 

Boolean logic

And C = A & B and(C, A, B)  
Exclusive Or C = A % B eor(C, A, B) Windows: you could use "xor(C, A, B)".
Left shift C = A << B lsl(C, A, B)  
Right shift C = A >> B lsr(C, A, B)  
Not C = NOT A not(C, A)  
Or C = A | B or(C, A, B)  

 

Rounding

The set command does not round.
set R to 12.75
set A to R
will result in A being set to 12.

The roundcast command rounds to nearest.

set R to 12.75
roundcast(A, R)
will result in A being set to the expected 13.

 

Internal accuracy

Some languages (Psion OPL, Microsoft VisualBasic) try to work with integer maths for speed unless certain rules suggest otherwise. The teletext script interpreter is different in that all internal calculations are floating point.

Windows: All calculations are performed to 'double' floating point accuracy, and are then clipped to nine decimal places (with rounding) to try to ensure the same results as the RISC OS version.
RISC OS: All calculations are performed with floating point accuracy.

If you specify floats as the destination variable, the internal result is copied across. If you specify integers as the destination, the internal result is rounded to the nearest whole number.