www.riscos.com Technical Support:
Before you can perform any arithmetic operations, you need to know how the computer handles numbers, and what limitations there are on their use.
This appendix describes the different types of numbers you can use with BBC BASIC, tells you how they are stored and manipulated, and explains what limitations this places on your programs.
You can use the following numeric types with BBC BASIC:
These are whole numbers, which can be represented exactly by the computer, for example:
These are real numbers expressed as a decimal fraction, for example:
These are real numbers expressed as a decimal fraction, but with a fixed number of places after the decimal point. For example:
are fixed point numbers accurate to four decimal places.
The most important factor governing numeric types is the amount of memory used to store them. For the purposes of this description, we will only consider integers and floating point numbers.
BASIC VI uses the following storage sizes for numeric types:
|Numeric type||Storage size|
|Integers||4 bytes (32 bits)|
|Floating point numbers||8 bytes (64 bits)|
Remember that BASIC V only supports integers and 5-byte reals (we shall use the term n-byte reals to mean n-byte floating point numbers). The following figures show how the storage for each numeric type is organised.
The storage size of a numeric type affects the following things:
For example, integers occupy less space than real numbers, and are handled much more quickly. 8-byte reals use more memory than 5-byte reals, and are therefore more accurate. The computer can represent larger numbers in the 8-byte format.
The effect on memory usage is very important if, for instance, your program uses arrays of real numbers. Consider an array with 100 elements in, each element being a 5-byte real. This will occupy 500 bytes of memory, whereas it would occupy 800 bytes if the elements were 8-byte reals. This is a trivial example, but the effects can become severely limiting if you use very large arrays.
The following two subsections explain range and accuracy of representation in more detail.
The greater the storage size of a given numeric type, the greater the range of numbers of that type that the computer can represent. For instance, integers are stored in 4 bytes or 32 bits. The maximum positive integer that the computer can represent is given by
2 ^ (32 - 1)
which means 2 raised to the power of 31, and is equal to 2147483647.
The maximum positive real number that the computer can represent depends on which type of real number you specify. For instance, the maximum positive 5-byte real that the computer can represent is
1.7 x 1038
The accuracy of a number is determined by how many significant figures of the number that the computer can show. The computer can show all the significant figures in an integer (as long as it is within the representable range). However, it must lose some of the significant figures of a floating point number.
For instance, the value of PI shown to three significant figures is 3.14. Shown to six significant figures, it is 3.14159. BASIC VI can show up to 17 significant figures of a floating point number, but this does not mean it is completely accurate. PI has an infinite number of digits after the decimal point, and so the computer can only print an approximation to it, by chopping off the trailing digits.
The table below summarises the numerical representation of BBC BASIC.
|5-byte reals||±1.7x1038||to||±1.5x10-39||9 sig figs||5 bytes|
|8-byte reals||±1.7x10308||to||±1.5x10-323||17 sig figs||8 bytes|
The rest of this appendix explains the two methods used by BASIC VI for implementing 8-byte floating point arithmetic to IEEE standard 754. BASIC V only employs one of these methods, and does perform its 5-byte arithmetic to the IEEE standard.
Floating point arithmetic is the process by which real numbers are manipulated, as a result of your instructions to the computer. For example, a computer cannot add two numbers together in the way we can. It must first convert the numbers into binary form, and then add them using Boolean operations.
Every arithmetic operation can be reduced, at the lowest level, to a group of Boolean operations. It is more convenient, however, to represent these groups by a set of mnemonics, called the floating point instruction set. The BBC BASIC floating point instruction set is given in full in the RISC OS Programmer's Reference Manual.
BBC BASIC VI uses two methods to implement floating point arithmetic. They are as follows:
The advantage of hardware implementation is that it is much faster.
When you instruct the computer to add two real numbers A and B together, the following sequence of events takes place:
The floating point emulator is a software module that provides floating point support. It emulates a hardware floating point coprocessor. It is this module that provides the floating point instruction set, extending the existing instruction set of the ARM processor.
You cannot use floating point instructions directly, as the BASIC interpreter does not understand them. However, you can include them in an assembly language module which is called from your program. The description of the CALL statement (in the Keywords chapter) explains this.
The floating point coprocessor is an optional hardware device that performs floating point arithmetic to IEEE standard 754. The coprocessor only directly supports a subset of the floating point instruction set. If a particular instruction is not supported by the coprocessor, it is performed by the emulator instead.
It does not matter to your program whether a coprocessor is present or not. The user interface ensures that programs run in exactly the same way in either case. The only difference you will see is in the speed at which your program runs.
Floating point instructions are performed much faster in hardware, although the actual improvement in performance depends on what equipment you are using, and which processor you have.