## 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

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:
• To determine the 'absolute' value of a number, that is the number in positive form (i.e. 123 is 123 and -456 is 456), use:
abs(C, A)
• To 'flatten' a variable to a specific number of decimal places, use:
flatten(C, 2)
where the second value is the number of decimal places to retain. This does not round.
• To round and optionally cast one type of variable to another, use: roundcast(C, R)
Unlike set C to R, assigning 8.7 will result in '9' (using set would result in 8); it rounds-to-nearest.
RISC OS: There is a bug in that assigning to a float or assigning a float to a float will result in 0.5 being added.
• To determine the 'sign' value of a number, use:
sign(C, A)
The result variable (in this case 'C') will be -1 if the number is negative, 1 if the number is positive, and zero if it is zero.
• To determine the 'square' value of a number, use:
sqr(C, A)
And then 'C' will be the square of 'A'.

### 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.