Classic Computer Magazine Archive ANTIC VOL. 5, NO. 3 / JULY 1986

starting out


Lesson 5: Number Crunching

by DAVID PLOTKIN, Antic Contributing Editor

Learn the essentials of Atari number crunching. To become an effective programmer, you need to know how to make your computer perform standard calculations. This BASIC tutorial includes a type-in listing that will run on all 8-bit Atari computers of any memory size, with disk or cassette. David Plotkin 's course in beginner's BASIC programming started in the February, 1986 issue of Antic.

Your Atari home computer can do word processing, play games (of course), and keep track of data for you. But computers weren't invented to do any of these tasks. They were invented to compute the results of numeric formulas (hence the name "computer"). This month, I'll show you how to use your Atari to "crunch" numbers, a vital element in many kinds of programming.

Atari BASIC's set of mathematical functions is detailed below. I am assuming that you understand mathematical functions such as sine and log. If you don't, refer to a math textbook.

Arithmetic operators are the symbols used to add, multiply, subtract, divide, and exponentiate (raise to a power):

   +  addition

-  subtraction

*  multiplication

/  division

^  exponentiation

   In general, these symbols should look familiar to you, as they are the same symbols used to write a formula down on paper. Two are a little different, however. The multiplication symbol is an asterisk [*] since the multiplication symbol you use on paper looks just like an "x". The exponentiation symbol is also different, due to the limits of computer displays. Normally, when you raise a number to a power, you would write it like this: XY.

This means x raised to the y power. In Atari BASIC, you would write it as X^Y Here are a few examples of simple math formulas:

   10 X=2*6

20 Y= 3^3:REM (3 to the third power=9)

30 Z=X+Y/3

   Let's look at a variation of line 30 for a moment:

30 Z=3+6/3

   It is not obvious whether the answer is 5 or 3. If you first divide 6 by 3 and then add 3, the answer is 5. If you add 3 to 6 (making 9) and then divide by 3, then the answer is 3. The problem here is determining the order the calculations should be performed in. There are two ways to resolve it.

The first way is to use parentheses, as you would in regular math. BASIC will first evaluate what is inside a pair of parentheses, then evaluate what is outside the parentheses:

   10 Z=(3+6)/9:REM clearly evaluates to 1

   If you don't use parentheses, then BASIC will evaluate an equation in the following order:

   1. The results of all function calls such as Sine, Cosine and Log will be evaluated. These will be discussed in a little while.

2. Exponentiation. That is, all numbers will be raised to the assigned power.

3. All multiplications and divisions. If there is more than one multiplication and/or division, they will be evaluated, in order, from left to right.

4. All additions and subtractions. If there is more than one addition and/or subtraction, they will be evaluated from left to right.

Thus, the following two equations will give the same answer. (Note that the second version is easier to figure out!):

   2+6/3*2^2 and 2+((6/3)*(2^2))

   In addition to the arithmetic operators used above, Atari BASIC offers several groups of powerful functions already built-in and ready for use. I have broadly broken these up into three categories:

Trigonometric functions, Transcendental functions, and Other functions. (So, I ran out of names!)

The trigonometric functions supported by BASIC are as follows:

   SIN sine of an angle

COS cosine of an angle

ATN arctangent of an angle

   Here's how these functions might look in a BASIC program:

   10 A=SIN(X)

20 B=COS(X)

30 C=ATN(X)

   The results of each of these functions can either be in degrees or radians. To select which way you want to input your data and get your results, use the commands DEG for degrees and RAD for radians.

   10 Z=SIN(180*2):REM SIN(360) (This happens to be zero.)

There are only three transcendental functions, and the first two are the opposite of each other. The first is LOG, which takes the natural logarithm of the statement in parentheses:

   10 Z=LOG(2*3+4):REM LOG(10)

   Note that this is not the logarithm in base 10, but in the "natural" base, a number otherwise known as "e", which is approximately equal to 2.71828. If you don't know what this means, chances are you won't ever need it anyway. The transcendental functions show up most often in compound interest calculations where interest is compounded continuously.

   EXP is the second function in this category. It raises "e" to the power shown in the parentheses:

   10 Z=EXP(1):REM this is 2.71828...

   Note that EXP(LOG(X)) = X because EXP and LOG are opposite operations.

The last function in this category is CLOG, which takes the common (base 10) logarithm of the statement in parentheses:

   100 Z=CLOG(100):REM, which is 2.

Each of the remaining functions listed below operates on the argument in the parentheses:

1. ABS returns the absolute value of the argument. If the argument is positive or zero, it is equal to its absolute value. If the argument is negative, ABS returns the positive equivalent: ABS(5)=5 and ABS(-5)=5.

2. SQR returns the square root of the argument. That is, the number which when multiplied by itself, gives the argument: SQR(9) =3 because 3*3=9.

3. INT returns the integer portion of a real number. I'll explain. Normally, when you use your Atari to calculate a number, the answer is a number containing a decimal point and a certain number of decimal places. For example, 5/2=2.5, and 1/3=0.333333.. . The INT function removes the decimal point and all numbers to the right of the decimal point, leaving only the whole number. So, INT(5/2)=2, and INT(1/3)= 0. Note that INT always returns the next lower whole number, so INT(-l/3)=-1, since -1 is the next lower whole number of the answer, -0.3333... The INT function is especially useful when doing screen graphics, since every point on the screen is a whole number, not a fraction.

4. RND(0) is the random number generator. It generates a random number between 0 and 1. While you may sometimes want a random number between 0 and 1, more often you will want a random number that falls into a range which you specify. For example, let's say you want a random number that falls between A and B:

   10 Z=A+(B-A)*RND(0):REM uses this formula.

   If A=10 and B=20, then this formula will provide random numbers between 10 and 20, never equaling either 10 or 20. RND(0) returns a number between 0 and 1, multiplying it by A-B or 10 gives a number between 0 and 10, then adds 10 to (A) to finish things up.

Getting a random INTeger which falls between integers A and B (inclusive) is just a bit trickier:

   10 Z=A+INT((B-A+1)*RND(0))

   If A=10 and B=20, this equation will evaluate as follows: RND(0) will return a number greater than 0 and less than 1. Multiplying this number by 11 (B-A + 1) returns a number greater than 0 and less than 11. Taking the integer of this number returns an integer between 0 and 10. Finally, adding this number to 10 (A) returns an integer between 10 and 20. Choosing a random integer within a specified range is very useful in generating explosions using the SOUND statement, flashing screen colors, and plotting random points on the screen. These commands will be covered in future columns.

One final note about functions. As you may have noticed from the various examples, you may use functions within functions-that is, nesting functions:

10 Z=SQR(ABS(SIN(LOG(COS(20)))))+2*4 is perfectly legal.

This month's listing is a calculator program that makes your Atari perform a function it's vastly overqualified for-manipulating numbers just like an inexpensive pocket calculator. The program also demonstrates how to use variations of the math functions discussed in the column. It does the standard five arithmetic functions, as well as trigonometric and transcendental functions.

Type in Listing 1, NEWOWN5.BAS. And by now you should be able to check it with TYPO II, following the instructions at the beginning of the Software Library listings. Be sure to SAVE a copy of NEWOWN5.BAS before you RUN it.

To select numbers, just move the cursor around with your joystick, and press the button to select a number or function. The intermediate and final answers will appear in the calculation window of the calculator. If you make an error (like trying to divide by zero), the calculation window will read ERROR. You will then have to CLEAR the calculator and start your calculation over again. Study the listing to see examples of how to program with the math operators and functions. There are several items you have not yet seen, like arrays (such as NUMPAD$). Don't worry, these will be covered in a future column.

Beginning programmers will find more information about topics covered in this column in Lon Poole's book, Your Atari Computer, $17.95 from Osborne/McGraw-Hill Publishing Berkeley CA.

Listing 1   NEWOWN5.BAS Download