RISCOS.com

www.riscos.com Technical Support:
BBC BASIC Reference Manual

 


Numeric variables


This chapter tells you how to perform arithmetic operations using numeric variables. If you want to know more about the different types of numeric variable which BBC BASIC uses, and how they are represented, see Appendix A: Numeric implementation.

Integers and floating point numbers

Integer variables are specified by placing a percent sign (%) at the end of the name. Floating point variables have no percent sign at the end. For instance, a variable called number% is an integer variable, whereas a variable called number is a floating point variable.

Floating point variables can represent both whole numbers (integers) and decimal fractions, but integer variables can only store whole numbers. For example, the assignments

LET number  = 4/3
LET number% = 4/3

leave the variables with the following values:

number is 1.33333333
number% is 1

In the case of the integer variable, the decimal fraction part has been lost. The advantages, however, of using integer variables are:

  • they are processed more quickly by the computer;
  • they occupy less memory;
  • they are precise.
Assigning values to variables

The value assigned to a numeric (floating point or integer) variable can be specified as:

  • a single number
  • the current value of another variable
  • an expression
  • the result of a function.

For example:

LET base   = 3
LET height = 4
LET area   = (base * height)/2
LET hypot  = SQR(base*base + height*height)

(base * height)/2 is a mathematical expression consisting of the variables base and height, and arithmetic operations to be performed on them.

SQR is a function which returns the square root of a number, in this case the expression (base*base + height*height).

The above assignments leave the variables with the following values:

base is 3
height is 4
area is 6
hypot is 5

Note that giving a new value to base or height does not automatically update area or hypot. Once the expression is evaluated using the values of base and height current at that time, it is forgotten. In other words, area and hypot only know what value they contain, not how it was obtained.

The use of LET is optional. For example,

LET x = x+1

is equivalent to:

x = x+1

Using LET, however, makes it easier initially to understand what is happening. On its own x = x+1 looks, to a mathematician, like an unbalanced equation. Using LET makes it clear that the = is not being used in its usual algebraic sense but as shorthand for 'become equal'. LET x = x+1 can be read as 'let x become equal to its old value with one added to it'.

In BBC BASIC, it is usual not to use LET at all; it is principally allowed to provide compatibility with other BASICs which require its presence.

An alternative way of expressing an addition in an assignment is to use:

x += 1

This means 'let x become equal to itself with one added to it'.

Similarly,

x -= 3

means 'let x become equal to itself with three subtracted from it'.

Special integer variables

The 27 integer variables A% to Z% and @% are treated slightly differently from the others. They are called 'resident' integer variables because they are not cleared when the program is run, or when NEW is used. This means that they can be used to pass values from one program to another.

A special integer pseudo-variable is TIME. TIME is an elapsed time clock which is incremented every hundredth of a second while the computer is switched on. It can be used to find out how long something takes by putting the following statements around a program:

T% = TIME
.........
PRINT (TIME - T%)/100 : REM Time in seconds

TIME may be assigned a starting value just like any other variable. So, for example, the statement above could be replaced by:

TIME = 0
........

PRINT TIME/100

Note that you cannot use LET with TIME.

Arithmetic operators

The full list of arithmetic operators and logical operators is given in the following table. Each operator is assigned a priority. When an expression is being evaluated, this priority determines the order in which the operators are executed. Priority 1 operators are acted upon first, and priority 7 last.

Priority Operator Meaning
1 - Unary minus
+ Unary plus
NOT Logical NOT
FN Functions
( ) Brackets
? Byte indirection
! Word indirection
$ String indirection
| Floating point indirection
2 ^ Raise to the power
3 * Multiplication
/ Division
DIV Integer division
MOD Integer remainder
4 + Addition
- Subtraction
5 = Equal to
<> Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
<< Shift left
>> Arithmetic shift right
>>> Logical shift right
6 AND Logical and bitwise AND
7 OR Logical and bitwise OR
EOR Logical and bitwise Exclusive OR

For example, 12+3*4^2 is evaluated as 12+(3*(4^2)) and produces the result 60.

Operators with the same priority are executed left to right, as they appear in the expression. Thus, 22 MOD 3/7 is evaluated as (22 MOD 3)/7.

Note that the shift operators are entered by typing two (or three) > or < symbols, and should not be confused with the «and » characters in the ISO Latin1 alphabet. Note also that although you can say 1+2+3, you couldn't write 1<<2<<3. This would have to be bracketed thus: (1<<2)<<3. This is because you may only use one group 5 operator per (unbracketed) expression.

This edition Copyright © 3QD Developments Ltd 2015
Last Edit: Tue,03 Nov 2015