CHAPTER 5

NUMERICAL EXPRESSIONS AND OPERATIONS


FLOATING POINT NUMBERS


In Standard BASIC, the Sorcerer handles all numbers as decimals. Even the integers (whole numbers) are considered to be decimal numbers with zero fractional parts.

Example:

The integer     is handled as

      1           1.00000

      0           0.00000

     -3          -3.00000

Ordinary decimal notation is sometimes called fixed point notation because we consider the decimal point fixed, and place the digits around the point, according to the size of the number.

Examples of numbers in fixed point notation:

 1.23456       -1.0506

 0.0          238.5

-0.000123    4560.0

When its Standard BASIC ROM PAC is plugged in, the Sorcerer handles all decimal numbers in a different way, called floating point notation. You may have seen this notation on so-called "scientific calculators," where it is called "scientific notation" or "E notation." It is also called "exponential notation," and is written with an integer followed, if necessary, by a decimal point and up to five decimal places, an E and then a two-digit number with a plus sign or a minus sign. E xx is the same as 10**.


REMARK

Standard BASIC supports six significant digits.


Examples:

This fixed point            is written this              to convert from floating

number                      way in floating              to fixed point, move the

                            point notation               decimal point


    1.23                      1.23E+00                   zero places

  123.0                       1.23E+02                   2 places to the right

  -74.5                      -7.45E+01                   1 place to the right

    0.07324                   7.324E-02                  2 places to the left

   -0.00003                  -3E-5                       5 places to the left

 128000000                    1.28E+08                   8 places to the right

 123456000000000              1.23456E+14               14 places to the right

-123456000000                -1.23456E+11               11 places to the right

 -.000000000123456           -1.23456E-10               10 places to the left

The E notation tells you how many places to move the decimal point left or right to convert a floating point number to fixed point notation. Moving the point to the right is the same as multiplying by positive powers of 10 and moving the point to the left is the same as multiplying by negative powers of 10.

Examples:

12345600 = 1.23456E+07 = 1.23456 x 10000000 = 1.23456 x 10^7

.00123456 = 1.23456E-03 = 1.23456 x 1/1000 = 1.23456 x 10^3

The first (left-most) non-zero digit of a fixed point number (which is its first digit in floating point notation) is called the first significant digit. Standard BASIC only pays attention to the first six significant digits (that is, the first non-zero digit and the next five digits). If you give it a number with fewer than six significant digits, the Sorcerer fills in enough zeros to make six digits. If you give it a number with more than six significant digits, the Sorcerer rounds off to six.


REMARK

To round a number, look at the next lower power of ten. If this number is 5 or greater, add I to the previous digit. For instance, 10. 1 rounded to the nearest one is 10 (since 1 is less than 5); 8.7 rounded to the nearest one is 9 (7 is greater than 5, so add 1 to 8), 86 rounded to the nearest ten is 90; 134 rounded to the nearest hundred is 100: etc.


Examples:

You enter              Sorcerer uses

this number            this number


    1.23               1.23000E+00

   91.82736            9.18274E+01

  123                  1.23000E+02

  -74.5               -7.45000E+01

    0.07654321         7.65432E-02

   -0.003             -3.00000E-03

13010.09               1.30101E+04

    0.0001000009       1.00001E-04

The largest positive number that Standard BASIC accepts is 1 .70141 E+38 (and the smallest negative number -- by "smallest" we mean "farthest from zero," but we might better say "with largest absolute value" -- is -1.7014E+38). The smallest positive number that Standard BASIC accepts is 9.40396E-39 (and the largest negative number is -9.40396E-39; by largest negative number we mean, of course, that number closest to zero).

If you use too large a number in any step of a calculation you get the OV (overflow) error message. Even though all the data you have entered are acceptable, in combination the terms may produce too large a number. The only way to check this is to go over your calculations step by step. Sometimes doing the arithmetic in a different order clears up the problem.

Example:

10^20 x 10^20
------------
10^20 x 10^20

The answer to this problem is obviously 1, a number the Sorcerer can easily handle. If you enter it as

(1E+20*1E+20)/(1E+20*1E+20)

you get the OV message. (The * is the symbol that Standard BASIC uses for multiplication; the / is the symbol that Standard BASIC uses for division.) Enter it as

(1E+20/1E+20)*(1E+20/1E+20)

and you have no problems.

An underflow (too small a number) gives zero as the result and execution continues with no error message.

The practical limits of the numbers you can use in Standard BASIC are 1E+38 and 1E-38. Numbers outside these bounds may produce strange results.

Though the Sorcerer handles all numbers in floating point notation, it does not print all numbers that way.

  1. Negative numbers are printed with a minus sign (-) before the first digit. Positive numbers are printed with a space (not a + sign) before the first digit.
  2. Whole numbers (i.e., those with zero fractional part) between -999999 and 999999 are printed as integers (no decimal point).
  3. Non-integer numbers between .01 and 999999 or between -.01 and -999999 are printed in fixed point notation (no E notation).
  4. All other numbers are printed in floating point notation.

Note that the Sorcerer evaluates numbers entered as fractions or calculations in PRINT statements and prints the result.

Examples:

You enter: PRINT 1/16

Sorcerer replies: .0625

You enter: PRINT 1/128

Sorcerer replies: 7.8125E-03

Put this program into your Sorcerer:

10 REM THIS PROGRAM SHOWS HOW THE SORCERER PRINTS

20 REM NUMBERS IN STANDARD BASIC

30 INPUT "WHAT IS YOUR NUMBER";X

40 PRINT

50 PRINT "I PRINT YOUR NUMBER AS";X

60 PRINT

70 GO TO 30

Now run the program and try as many different numbers as you can think of. (Leave out commas when entering numbers; the Sorcerer thinks commas are being used to separate inputs. That is, for the number one million enter 1000000, not 1,000,000.)

Example:

Sorcerer: WHAT IS YOUR NUMBER?

You: -.003

Sorcerer: I PRINT YOUR NUMBER AS 3E-03
WHAT IS YOUR NUMBER?

Try these numbers on the program.

+1            -1             6523

-23.460       1E20           -12.345E-7

1.234567E-10  1000000        999999

.1            .01            .001

.000123       -.00012345678  123456789

Floating point numbers are only approximations of the real numbers they represent. A computer assigns numbers a fixed number of significant digits, and does all calculations in hexadecimal form, not decimal. All of the digits may not print on the screen. The computer may carry the extra digits internally as overflow. Nonetheless, each number is represented by a finite number of digits. The Sorcerer works with only six significant digits at a time. You have to be careful with computations that depend on accuracy greater than six significant digits. Also, since you don't know what digits are being stored beyond the six significant digits, you may come up with some strange answers to certain computations.

Try these numbers on the previous program:

.9999972    .99999373   .50000046

.500000459  .11111124   .11111125

Apparently, as far as the Sorcerer is concerned, .11111124 and .11111125 are the same. But, try the next program with the two numbers as inputs, and compare the results:

10 INPUT "PICK A NUMBER";X

20 PRINT "YOUR NUMBER IS";X

30 PRINT "DOUBLE YOUR NUMBER IS";X*2

40 PRINT

50 GOTO 10

So you think that twice .999998 is 1.99996? Try it!

The problems lie with what we call rounding errors.

Also see the discussion at the end of Chapter 7.


NUMERICAL CONSTANTS AND VARIABLES


A numerical variable is a mathematical quantity which can be assigned different number values, but has no definite value until one is assigned. When you use a variable in a program and assign it no value, Standard BASIC initializes its value to 0. You can regard a variable as analagous to an unknown in algebra. A numerical constant is a definite number, such as 17, 34.5, 3E-06, and so forth.

When you use a variable in a program, the Sorcerer finds an empty storage location in its memory and assigns your variable as a name for that location. If you use the letter X as a variable, the Sorcerer finds a storage location and calls it X. When you assign a value to the variable with a LET statement (or a statement of equality without the word LET; recall from Chapter 1 that LET X = 1 and X = 1 are the same), the Sorcerer puts the proper number into the location named X.

You can use any combination of letters and numbers as a numerical variable, provided that:

  1. The first character of the name is a letter.
  2. No reserved words appear in the name.
  3. The length of the variable name does not exceed one program line. (In fact, the variable name must fit on the program line together with whatever else is on the line.)

Example:

You can use these                           You cannot

as variables:                               use these:


A1                                          1A         first character

                                                       is a number

OSCAR                                       TOM        contains the

                                                       reserved word

                                                       TO

OSCAR1                                      ATOM       contains the

                                                       reserved word

                                                       TO

RUM                                         RUN        contains the

                                                       reserved word RUN

Q                                           &          not a letter or

                                                       number

UNBUREAUCRATICALLY                          FLOCCIPAUCINIHILIPILIFICATION

                                                       contains the

                                                       reserved words IF

                                                       and ON

The Sorcerer only pays attention to the first two characters of a variable, so make certain no two variables in a program have the same first two characters.

Example:

The Sorcerer cannot tell

the difference

between            and

OSCAR              OSWALD

A7                 A71

JAB                JA2

(In a way, however, the Sorcerer can distinguish between COMPUTER and COMMAND. It rejects the latter because COMMAND contains the reserved AND.)


NOTE

Now, even though Standard BASIC (and many other versions of BASIC written for various microcomputers) allow variable names to exceed two characters in length, it is poor programming practice to use such variable names. You may confuse yourself or introduce errors into a program by, for example, using avariable such as COUNTER and then using COMPOUNDER later -- you get no error messages, but may not figure out why your data gets all mixed up. You think you've used two completely different variables, but they are the same to the Sorcerer. Also, you want programs you write to be easily adaptable to other systems, and many versions of BASIC limit variable names to two characters. And, you may inadvertently include a reserved word as part of your variable name -- maybe not reserved in this version of BASIC, but reserved in another. For these reasons, even though Standard BASIC allows the use of variable names of more than two characters, we will henceforth limit the names of all variables to two characters.


NUMERICAL OPERATIONS


Standard BASIC uses the ordinary arithmetical operations (addition, subtraction, multiplication and division) together with the operations of negation (changing the sign of a number) and exponentiation (raising a number to a power). The symbols that perform these operations are called operators.

To add or subtract two numbers, use the + and - signs on the main keyboard or the 16-key pad. To multiply two numbers, use the x sign on the 16-key pad or the * sign (asterisk) on the main keyboard. To divide two numbers, use the ÷ (division) sign on the 16-key pad or the / (slash) on the main keyboard. (Most other microcomputers do not have a separate numerical keypad and thus use only the * key for multiplication and only the / key for division.)

When you use the division operator in an instruction, be sure that the divisor (the expression you are dividing by) does not have the value 0 at the time the instruction is executed. If this happens, your program stops and you get a /0 error message.

To group expressions and keep operations in the proper order, use parentheses.

Examples:

(1+A)/2     means  add 1 to A, then divide by 2
1+A/2 means divide A by 2, then add 1

To negate an expression put a minus sign (-) in front of it. (Negation just changes the sign of a number from positive to negative or from negative to positive.)

Examples:

-(-1)    changes -1 to 1
-(A+B) means add A and B, and then change the sign of the result

For exponentiation, use the ^ sign on the main keyboard.

Examples:

A^2       means A² (that is, A x A, or A squared)
A^3 means A³ (that is, A x A x A, or A cubed)
A^X means A to the Xth power
A^.5 is the same as the square root of A
2^(1/3) is the same as the cubed root of 2
B^-A is the same as 1/B^A
2^A is 2 to the Ath power
A^(B/C) is the Cth root of A to the Bth power

Any number to the power 0 is 1 (that is, X ^ 0 = 1). Trying to raise 0 to a negative power (as, 0 ^ -1) causes a /0 error.


ORDER OF PREFERENCE


Even fairly simple numerical expressions may contain several operators. When you compute the value of an expression, the result you get often depends on which operations you perform first.

Take the expression

3/2 + 1 (Divide 3 by 2 plus 1)

--If you divide 3 by 2 first, and then add 1, the result is 2.5: (3/2)+ 1.

--If you add 1 to 2 first, and then divide into 3, the result is 1: 3/(2+ 1).

To avoid this kind of confusion, the Sorcerer performs numerical operations in a certain order of preference. This insures that each numerical expression has a single, well-defined value (when values are assigned to all variables in the expression).

Operations are done in this order:

  1. () Expressions inside parentheses.
  2. ^ Exponentiation.
  3. - Negation.
  4. * and / Multiplication and division.
  5. + and - Addition and subtraction.

When two or more operations in an expression have the same precedence, the Sorcerer does them in order from left to right.

Examples:

(1 + 4)* 3   first add 1 + 4, then multiply by 3
1 + 4 * 3 first multiply 4 * 3, the add 1
2*3^4 first compute 3 ^ 4, then multiply by 2
(2*3)^4 first multiply 2 * 3, then raise the result to the 4th power
-3^2 first square 3, then negate the answer (this produces
-9, whereas squaring -3 would produce +9); the same
as -(3 ^ 2)

THE CONDITIONAL GOTO


The conditional GOTO branches a program into several different paths, instead of just the two of the IF... THEN statement. The format is:

ON <numerical expression> GOTO <list of line numbers>

When the Sorcerer sees this instruction, it evaluates the expression (and throws away any fractional part of the result). This produces an integer, say J. Now the Sorcerer looks at the Jth item in the list, and jumps directly to that line number. If, J has value 0 or is greater than the number of items on the list, the Sorcerer goes to the next instruction after the ON ... GOTO statement. If J is less than 0 or greater than 255, you get an FC error message (ILLEGAL FUNCTION CALL).

Example:

10 PRINT
20 INPUT "PICK A NUMBER FROM 1 TO 5";X 30 PRINT "YOUR NUMBER IS "; 40 ON X GOTO 80,100,120,140,160 50 IF X=0 THEN PRINT "ZERO.":END 60 PRINT "NOT IN THE PROPER RANGE." 70 GOTO 20 80 PRINT "ONE." 90 GOTO 10 100 PRINT "TWO." 110 GOTO 10 120 PRINT "THREE." 130 GOTO 10 140 PRINT "FOUR." 150 GOTO 10 160 PRINT "FIVE." 170 GOTO 10

Note that this program has a "fail-safe" to prevent the operator frorn entering numbers other than those from 1 to 5. That is, if the input is less than 1 or greater than 5, the program continues to line 50 and to 60 if the input is not 0. Also, to terminate the program, the operator enters 0 at the prompt.


Table of Contents | Prev | Next

The Trailing Edge