Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Programming for Dummies 2004.pdf
Скачиваний:
109
Добавлен:
17.08.2013
Размер:
8.05 Mб
Скачать

96

Part II: Learning Programming with Liberty BASIC

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Table 8-1

Mathematical Operators

 

 

 

Mathematical Operation

Symbol to Use

Example

Result

 

 

Addition

+

 

2 + 5

7

 

 

 

 

 

 

 

 

 

 

Subtraction

77

– 43

34

 

 

 

 

 

 

 

 

 

 

Division

/ (forward slash)

20

/ 4

5

 

 

 

 

 

 

 

 

 

Multiplication

*

 

4 * 7

28

 

 

 

 

 

 

 

 

 

Exponentiation

^

 

2 ^ 3

8

 

 

 

 

 

 

 

 

The division symbol (/) usually appears in two places on your keyboard: on the same key as the question mark (?) and on the numeric keypad. The exponentiation symbol (^) appears on the 6 key. You can also use the subtraction symbol (–) to indicate negative numbers, such as –34.5 or –90.

Although you already understand how addition, subtraction, division, and multiplication work, you may be less familiar with exponentiation.

Exponentiation simply multiplies one number by itself several times. The formula 4 ^ 3, for example, tells Liberty BASIC take the number 4 and multiply it by itself three times. So 4 ^ 3 really means 4 * 4 * 4, or 64.

Using variables

Any mathematical calculation (addition, subtraction, division, or multiplication) creates a single value, which you can store in a variable as follows:

TaxYouOwe = 12500 * 1.01

Rather than use specific numbers to create mathematical formulas (such as 12,500 * 1.01), however, you can substitute variables in the following way:

PROMPT “How much money did you make last year”; NetIncome

TaxYouOwe = NetIncome * 1.01

NOTICE “You owe this much in taxes = “; TaxYouOwe

END

To make your mathematical calculations easier to understand, always use variables or constants rather than actual numbers. In the preceding example, you can’t tell what the number 1.01 represents. Rather than use an actual number, substitute a descriptive constant in the formula, as follows:

Chapter 8: Crunching Numbers and Playing with Strings

97

TaxRate = 1.01

PROMPT “How much money did you make last year”; NetIncome TaxYouOwe = NetIncome * TaxRate

NOTICE “You owe this much in taxes = “; TaxYouOwe END

If you use a constant (in this case, making TaxRate represent the number 1.01), you can quickly understand what the number 1.01 means and why to use it in the mathematical formula.

You can use variables in the following two ways in mathematical formulas:

To represent numbers that the mathematical formula uses

To store the value that the mathematical formula calculates

Be careful when naming variables. If you mistype a variable name or mix uppercase with lowercase letters, Liberty BASIC assumes that you’re creating a new variable, and it assigns a value of zero or a blank to the “new” variable. If your program isn’t working right, check to make sure that you spelled the variables correctly and used exactly the same upperand lowercase letters.

Working with precedence

Simple formulas such as NetIncome * TaxRate are easy to understand; you just multiply the values that both variables represent. You can create more powerful mathematical formulas, however, by combining addition, subtraction, division, or multiplication, as in the following example:

TaxYouOwe = PastTaxes + NetIncome * TaxRate

If the value of NetIncome is 50,000, the value of TaxRate is 1.01, and the value of PastTaxes is 2,500, the computer looks at the formula as follows:

TaxYouOwe = 2500 + 50000 * 1.01

So now the question is whether Liberty BASIC adds 2,500 to 50,000 and then multiplies the whole thing by 1.01 (in which case the answer is 53,025) or multiplies 50,000 by 1.01 first and then adds 2,500 (in which case the answer is 53,000).

Because the result of combining addition, subtraction, division, and multiplication in a single formula can confuse the living daylights out of people, programming languages create something mysterious known as precedence that

98

Part II: Learning Programming with Liberty BASIC

tells the computer which mathematical operations to calculate first. Liberty BASIC calculates mathematical operators in the following order, from top (first) to bottom (last):

Exponentiation (^)

Multiplication (*) and (^); division (/)

Addition (+) and subtraction (–)

Before running the following Liberty BASIC program, try to figure out how the computer calculates a result:

MyMoney = 3 + 4 ^ 5 - 8 / 5 * 7

PRINT MyMoney

END

This Liberty BASIC program tells the computer to do the following:

1. The first line tells the computer to create the variable MyMoney.

Because the computer calculates exponential values first, Liberty BASIC calculates the value of 4 ^ 5, which is 1,024. The formula now looks as follows:

MyMoney = 3 + 1024 - 8 / 5 * 7

Next, the computer calculates all multiplication and division (/). Because multiplication and division have equal precedence, the computer starts calculating with the first multiplication or division (/) operator that it finds, moving from left to right. The computer calculates the value of 8 / 5 first (1.6) and then multiplies it by 7. So the formula now looks as follows:

MyMoney = 3 + 1024 - 11.2

Finally, the computer calculates all addition and subtraction, moving from left to right. First it calculates the value of 3 + 1,024 (which is 1,027); it then subtracts 11.2 from it. Thus the final answer looks as follows:

MyMoney = 1015.8

2.The second line tells the computer to print the value that the MyMoney variable stores, which is 1015.8.

3.The third line tells the computer that the program is at an end.

The computer always calculates operators from left to right if operators are equal in precedence, such as multiplication and division or addition and subtraction.

Chapter 8: Crunching Numbers and Playing with Strings

99

Using parentheses

Trying to remember the precedence of different mathematical operators can prove confusing. Even worse is that the ordinary precedence of mathematical operators can mess up the way that you want the computer to calculate a result. Suppose, for example, that you type the following:

BigValue = 3 + 4 ^ 5

PRINT BigValue

END

With this program, the computer first calculates the exponential value of 4 ^ 5 (which is 1,024), and then it adds 3 to it, for a total of 1,027.

But what if you really want the computer to add 3 to 4 and then perform the exponential? In this case, you must use parentheses to tell the computer, “Hey, add 3 to 4 first and then calculate the exponential,” as in the following example:

BigValue = (3 + 4) ^ 5

PRINT BigValue

END

This program adds 3 and 4 to get 7, so the formula becomes BigValue = 7 ^ 5, or 16,807.

Anytime that the computer sees something trapped within parentheses, it calculates those values first. Then it uses its normal rules of precedence to figure out how to calculate the rest of the formula.

Use parentheses to enclose only one mathematical operator at a time, such as (3 + 4). Although you can use parentheses to enclose multiple mathematical operators, such as (3 + 4 ^ 5), doing so essentially defeats the purpose of using parentheses to make clear what the computer is to calculate first. You can, of course, use multiple parentheses to create fairly complex formulas, such as in the following formula:

EasyTaxCode = ((3 + 4) ^ 5 / 3 - 8) / 5 * -7

(Without the parentheses in the preceding formula, Liberty BASIC calculates an entirely different result.)

100 Part II: Learning Programming with Liberty BASIC

Using Liberty BASIC’s Built-In

Math Functions

By combining mathematical operators, you can create practically any type of mathematical formula. But creating some mathematical formulas may prove too cumbersome, so as a shortcut, Liberty BASIC (and many other programming languages) provides built-in mathematical functions that you can use, as shown in Table 8-2.

Table 8-2

Liberty BASIC’s Built-in Mathematical Functions

Function

What It Does

ABS (x)

Returns the absolute value of x

 

 

ACS (x)

Returns the arccosine of x

 

 

ASN (x)

Returns the arcsine of x

 

 

ATN (x)

Returns the arctangent of x

 

 

COS (x)

Returns the cosine of x

 

 

EXP (x)

Returns a number raised to a specified power (x)

 

 

INT (x)

Returns the largest integer less than or equal to a specific

 

number or expression

 

 

LOG (x)

Returns the natural logarithm of x (Note: The value of x

 

must be a positive, nonzero number.)

 

 

SIN (x)

Returns the sine of x

 

 

SQR (x)

Returns the square root of x

 

 

TAN (x)

Returns the tangent of x

 

 

If you don’t understand terms like arcsine or logarithm, you probably don’t need to use them anyway. The important point to remember is that all programming languages, such as Liberty BASIC, include built-in mathematical functions that you can use if you need them.

The preceding equation calculates the square root of nine (9), which is three (3).

To see how the Liberty BASIC mathematical functions work, run the following program and type different numbers (negative, positive, decimal, and so on) between 0 and 1.0 to see how the program works: