Classic Computer Magazine Archive COMPUTE! ISSUE 65 / OCTOBER 1985 / PAGE 112

The Beginners Page

Tom R. Halfhill, Editor


Clearing Up Variable Cloudiness

If you're just learning to program, variables can be confusing at first - especially because there are so many varieties of variables. Last month's column introduced the concept of numeric variables. But, depending on your computer's BASIC, there are also integer variables, double-precision variables, string variables, numeric array variables, and string array variables. This month we'll cover integer variables and tackle the rest later.
    Numeric variables, you'll recall, represent ordinary numbers. For instance, you can store the number 10 in the variable X with the BASIC 'statement X = 10. Numeric variables can represent fractions just as easily, as in X = 98.6. An integer variable is similar, but with one important difference. As the term implies, integer variables can only represent integers-whole numbers. Fractions like 98.6 aren't allowed. There's one other limitation, too. In most BASICs which allow integer variables, the value cannot range beyond a maximum of 32,767 or a minimum of -32,768.
    At first, these restrictions may seem odd. What's the advantage of limiting a variable to a whole number, and especially a whole number within a relatively narrow range?
    The answer has to do with the way computers manipulate numbers. Internally, they use the binary numbering system instead of our everyday decimal system. Translating decimal numbers into binary gets tricky when the decimal number is a fraction, or floating point number (so-called because the decimal point can "float" to the left or right, as in 98.6 or 9.86). The conversion process requires a few valuable microseconds, and it takes several bytes of memory just to store a single floating point number.

Are Integers Faster?
    Integer variables can greatly simplefy matters for a computer. Because fractions aren't allowed, the operating system doesn't have to spin its wheels performing lengthy floating point conversions. And when the integers are limited to a range of -32,768 to 32,767, each number can be stored in only two bytes of memory.
    Saving a few bytes of memory isn't a terribly important consideration anymore, now that nearly all personal computers come with at least 64K of RAM. But on certain computers, integer variables can help your programs run faster - often significantly faster.
    In Commodore BASIC, Applesoft, and IBM BASIC, you declare an integer variable by appending a percent symbol (%) to the variable name, as in X% = 10. (Integer variables are not available in TI BASIC or Atari BASIC, but are supported in Atari Microsoft BASIC.) A common mistake is to accidentally omit the % symbol in a statement somewhere, often leading to a mysterious error or unexpected result. Keep in mind that two variable names such as X and X% are treated by the computer as completely separate variables-they can store independent values and are as different as A and Z.
    To test the performance of integer variables versus regular variables on your computer, enter this simple program:

10 FOR X=1 TO 32000
20 Y=Y+1
30 NEXT X
40 PRINT Y

    Use a watch to measure how long this program takes to execute. Jot down the result, then change all three occurrences of Y to integer variables by adding the % symbol. Now run the program and time it again.

Surprising Results
what happened? If you have an IBM PC or PCjr, the program should run measurably faster. But if you have a Commodore or Apple, the program actually runs slower. What's going on?
    Integer variables are indeed faster and more memory-efficient on IBM computers. But on Commodore and Apple computers, integer variables actually execute slower and consume just as much memory as regular variables. This is true even though all three computers have versions of Microsoft BASIC. The reason is that the math routines in the Commodore and Apple are designed to handle floating-point numbers only. Therefore, the computer must convert integer variables into floating-point values perform the math requested by the program, and then convert the results back into integers. All this conversion takes so long (in computer terms) that integer variables really aren't any faster than regular variables on Commodore or Apple computers.
    It would seem, then, that integer variables are useless if you have a Commodore or Apple. But in fact, they can speed up your programs and save memory when used to construct arrays-a future column topic.
    In the meantime, let's clear up another mystery raised by the above program. If you examine it closely, you might wonder why converting Y to Y% makes it run faster even on the IBM. Since the FOR-NEXT loop is incrementing Y by steps of one, Y is never a fraction, anyway-it's always a whole number. But computers handle all numeric variables as floating point numbers, even when the value is a whole number and not a fraction. Defining a variable as an integer variable forces the IBM to treat it as an integer.