Hello, and welcome to the fifth part of the ST User Runtime magazine Basic Tutorials. This month, Johnny Johnson continues exploring variables. FIRST STEPS IN BASIC Last month we saw how variables can be used to hold information. Our simple programs introduced the concept of storing user input in a variable and then working with that number. BASIC languages on the ST generally allow two types of variable to be used. Integer variables can contain only whole numbers like 3 or 5 but not 3.5. The second is a real number, which can contain both an integer part (a whole number) and a fractional part. 3.14, the common expression of the value of pi, is a real number. The integer variable is the most widely used type of variable in programming. Because they contain no fractional part, they are fast and easy to work with. The result of a calculation using integer variables will always report a whole number (unless it is a division, in which case the result may be truncated or rounded). Real numbers are used only where absolute precision is needed. Computers can only work with binary integers, ones and zeros, so the conversion from real numbers must be made by software. It stands to reason that this conversion must take time, thus slowing down the overall speed of the operation. ROUND AND ROUND Before we go any further, its time to review a little basic maths. One of the first things we all learned in maths class was how to round decimal numbers up or down.BASIC uses rounding quite a lot, so it's important we know what BASIC is doing when it rounds numbers. Here are the basic rules for rounding numbers: LESS THAN .5 = ROUND DOWN MORE THAN OR EQUAL TO .5 = ROUND UP Remember that rounding removes the decimal part of a real number and then adjusts the integer part accordingly. Here are a few quick examples: 5.4 = 5 (The number is rounded down) 6.6 = 7 (The number is rounded up) 9.5 = 10 (The number is rounded up) 4.4 = 4 (The number is rounded down) For a bit of practice on your own, try converting these real numbers into integers to make sure you can remember how rounding works. The answers are given at the end of this article: 5.5 12.3 1.6 5.4 9.3 6.8 DEFINE YOURSELF BASIC languages on the ST have two ways of defining an integer variable. The first, and simplest, is just to use the percentage sign after the variable letter, like this: A% = 2 However, if you want to define a range of integer variables, BASIC provides the DEFINT command to help you. 10 DEFINT A -D This command will then define any variable that begins with the letters A to D inclusive as being integer variables. DEFINT is not case sensitive, so variables beginning a to d would also be included. DEFINT can only be used inside a program, and should be one of the first commands you use in any program that uses variables. (See the section on declaring variables in last months column). Integer arithmetic is the simplest form of maths your ST can perform, but there can be problems if you attempt to mix integer and real numbers in the same calculation. To illustrate the problem, try these two programs. PROGRAM ONE 10 A% = 2 20 B = 2 30 PRINT A% + B As you would expect, the answer comes out as 4. Now try the following program: PROGRAM TWO 10 A% = 2.5 20 B = 2.5 30 PRINT A% + B BASIC prints the answer 5.5, which is obviously wrong. The question is, why has BASIC arrived at this answer? To understand why the answer was 5.5 and not the 6 we expected, you have to remember what the % sign does. BASIC is treating variable A as an integer variable, and so it is following the rules for rounding numbers and rounding A% up to the nearest whole number, in this case 2.5 becomes 3. So the sum BASIC performs is in fact 3 + 2.5 which does equal 5.5. This sort of rounding operation is also cumulative. If the result of this sum were also used as an integer variable, like this: PROGRAM THREE 10 A% = 2.5 20 B = 2.5 30 C% = A% + B 40 PRINT C% The integer C% would be rounded from 5.5 to 6, the nearest whole number. As you can see from these examples, it is very important to know how BASIC will treat numbers. This sort of cumulative error in calculations could mean that the results from a program are completely wrong. The golden rule is never attempt to mix real and integer variables. Incidentally, if you use DEFINT to define a range of integer variables, say from A to F and then decide you want to use the variable B as a real number and not an integer, you can override the DEFINT statement by using the exclamation mark (!) with the variable in the same way as a percent sign, like this: B! = 2.9 Normally the DEFINT statement would try to force this variable to be an integer, and would round the number up to 3. The exclamation mark informs BASIC to let the 2.9 stand as a real number. INT AND CINT As if to confuse matters even further, BASIC provides two more statements for dealing with real numbers. The first, INT, slices off the fractional part of a real number, leaving only the whole integer. Try this program to see how INT works: PROGRAM FOUR 10 A = 2.9 20 B% = A 30 PRINT "VALUE = ";A 40 PRINT "ROUNDED = ";B% 50 PRINT "TRUNCATED = ";INT(A) This program will print the following results: VALUE = 2.9 ROUNDED = 3 TRUNCATED = 2 We've already seen how the % sign will force a number to be rounded up or down, depending of the value of the fraction part. The INT statement truncates the number, completely ignoring the fractional part and just printing the whole, or integer part of the original number. The second statement is called CINT and it does the opposite of INT. It works like the % sign, and rounds the number up or down, depending on the size of the fractional part. PROGRAM FIVE 10 A = 2.9 20 PRINT "VALUE = ";A 30 PRINT "ROUNDED = ";CINT(A) 40 PRINT "TRUNCATED = ";INT(A) This program will print the following results: VALUE = 2.9 ROUNDED = 3 TRUNCATED = 2 These results are exactly the same as for Program Four, but they are achieved in one line less code. The shorter program involves less stages, and so it is more efficient when run. Because the variable B% is not used, it remains available for other purposes, which could be useful if you were writing a long program that used a lot of variables. BEFORE NEXT TIME... Before the next article, try writing a program that rounds and truncates variables. Experiment with converting real numbers into integer numbers in a program, just to see how cumulative error can affect the results of mathematical operations. Next month we'll be taking what we've learned here one stage further and working with strings, so remember to get next month's User. (Incidentally, remember the little bit of practice on rounding real numbers I set earlier? Well, for those of you who were wondering, here are the correct answers: 6 12 2 5 9 7 I hope you all managed those all right. If you didn't, go back and review the section on rounding numbers again.)

## Takaisin

(C) Marko, Suomen Atari-sivut / ArkiSTo 2003