Practice your coding with the BASin coding teacher.

Try to code at least one program using the five codewords you have learned in this lesson, and any other codewords you have learned from the previous lessons.

Now we are going to learn some codewords that are useful for making mathematical calculations in a program. If you do not know some of the mathematical words in this lesson, don’t worry. Just ask your maths teacher what they mean, or if you prefer you can skip the codeword and come back to it later when you’ve learned the meaning.

**SQR** This is the __SQ__ua__R__e root codeword. It tells the computer that your program wants to know the square root of a number.

**SGN** This is the __S__i__GN__ codeword. It tells the computer that your program wants to know if a number is positive (which means it is greater than 0), or if it is negative (less than 0), or if it is 0.

**PI** The PI codeword tells the computer that your program wants to use the value of π (the Greek letter “pi” which is used quite a lot in mathematics). The value of π is 3.1415926 . . . . (never ending).

**ABS** The ABS codeword tells the computer that your program wants to know the “__ABS__olute” value of a number. That is the value without a “+” or a “-“ sign.

**INT** The INT codeword changes numbers that are __not__ whole numbers __into__ whole numbers. A whole number is called an INTeger, which is where the name INT comes from.

** **

The SQR codeword is a function.

SQR tells the computer to calculate the square root of a number.

The SQR codeword must be followed by the number or value whose square root you want the computer to calculate. For example:

**70 LET houses = SQR 16**

** **

Line 70 tells the computer to calculate the square root of the number 16 (which is 4) and assign that value to the variable **houses**.

You must always remember *not* to ask the computer to calculate the square root of a negative number. That would be a bug!

Another way to use the SQR codeword is to tell the computer to calculate the square root of the value of a variable. For example:

**71 LET houses = SQR street**

would tell the computer to calculate the square root of the variable **street** and assign that square root value to the variable **houses**.

The SGN codeword is a function.

SGN means “sign”, which is usually either positive (+) or negative (-). The SGN codeword tells the computer to indicate whether a number is positive, or negative, or zero.

SGN tells us which direction on the number line the number is pointing. Positive (1) is to the right, and negative (-1) is to the left, with zero in the middle having no sign.

The SGN codeword must be followed by the number or value for which you want the computer to determine its sign.

If the computer finds that the sign of the number or value is positive, it gives your program the value 1. (We say that the computer “returns” the value 1.)

If it finds that the value is negative it returns the value -1.

If it finds that the value is 0 it returns the value 0.

For example, the program line:

**50 LET x = SGN 7**

** **

will return the value 1 and assign it to x

while the line:

**51 LET x = SGN -7**

will assign to x the value -1.

The PI codeword is a function.

The PI codeword tells the computer that your program wants to use the value of π (the Greek letter “pi” which is used quite a lot in mathematics). The value of π is 3.1415926535 . . . . (never ending).

The PI codeword gives the value of *pi* (π) for use in calculations. PI is pronounced like “pie” (which is what we call the Greek letter π) – it is the ratio of the circumference of a circle to its diameter.

In BASIC, PI returns a value of 3.1415927. For example, look at this short program:

**80 INPUT radius**

**81 LET circumference = 2 * PI * radius**

**82 PRINT “circumference =”**

**83 PRINT circumference**

Line 80 tells the computer to take in a value and assign it to the variable radius.

Line 81 tells the computer to calculate the value of the variable circumference. The computer does this from the expression: 2 x π x radius.

Lines 82 and 83 tell the computer to display “circumference =” on the screen, followed by the calculated value of the circumference for a circle whose radius has been input at line 80.

The ABS codeword is a function.

ABS means “ABSolute value of”, which is the value of a number without a positive or negative sign. So the absolute value of 12 is 12. The absolute value of -15 is 15.

The ABS codeword must be followed by the number or value for which you want the computer to determine its absolute value.

For example, the program line:

**52 LET x = ABS -8**

** **

will return the value 8 and assign it to x

while the line:

**53 LET x = ABS y**

will assign to x the absolute value of the variable y

and the line:

**53 LET c = ABS (a – b)**

will assign to the variable c the absolute value of the expression a-b. Note here that when we tell the computer to determine the absolute value of an expression, rather than just a single variable, we must put that expression inside brackets as we have done in line 54.

The INT codeword is a function.

The INT codeword tells the computer to change numbers that are not whole numbers into integers (which is what we call whole numbers).

The INT codeword must be followed by the number or value for which you want the computer to determine its integer value.

For example, the program line:

**55 LET x = INT 8.35**

** **

will return the value 8 and assign it to x

while the line:

**56 LET x = INT y**

will assign to x the integer value of the variable y

and the line:

**57 LET c = INT (a – b)**

will assign to the variable c the integer value of the expression a-b. Note here that when we tell the computer to determine the integer value of an expression, rather than just a single variable, we must put that expression inside brackets as we have done in line 57.

If you want the computer to determine the integer value of a negative number which is not already an integer, the computer will return the value of the next integer below the number you have asked it to work on. For example:

**58 PRINT INT -7.66**

will tell the computer to display -8, because 8 is the next integer lower than -7.66.

This program draws a table with some calculations.

Lines 700 to 720 asks for a number between -1000 and 1000.

Lines 730 to 760 draw the table headers.

In lines 770 to 820 we display the calculations for SQR,SGN,PI,ABS and INT. Lines 860 to 870 are a subroutine to round up a calculation to 2 decimal places.

```
680 LET i$="------": LET y=2
681 REM Tell the computer the start position for our table and the error message string
690 FOR n=1 TO 3
691 REM We use a loop to ask the user for 3 numbers
700 INPUT "Enter a number ";v
701 REM Ask for a number
710 IF v>=-1000 AND v<=1000 THEN GO TO 730
711 REM If the number is OK jump to line 730
720 INPUT "Enter a number between -1000 and 1000 ";v: GO TO 710
721 REM If it's not OK ask for the number again and remind them that it has to be between -1000 and 1000
730 LET x=n*8
731 REM Calculate the number of spaces along we print the results
740 IF n<>1 THEN GO TO 770
741 REM If this is the first time through the loop print the table labels
750 PRINT AT y,0;"N": PRINT AT y+1,0;"--------------------------------"
760 PRINT AT y+2,0;"SQR N": PRINT AT y+3,0;"SGN N": PRINT AT y+4,0;"2*PI N": PRINT AT y+5,0;"ABS N": PRINT AT y+6,0;"INT N"
770 PRINT AT y,x;v: PRINT AT y+3,x;SGN (v): PRINT AT y+5,x;ABS (v): PRINT AT y+6,x;INT (v)
771 REM Print the results for SGN, ABS and INT
780 IF (v>=0) THEN GO TO 810
781 REM if the number is valid for SQR and PI jump to line 810
790 PRINT AT y+2,x;i$: PRINT AT y+4,x;i$
791 REM But if not valid then display the error message in place of the result
800 GO TO 830
810 LET w=SQR (v): GO SUB 860: PRINT AT y+2,x;w
811 REM Calculate the value for SQR and use our subroutine to round up the result
820 LET w=2*PI*v: GO SUB 860: PRINT AT y+4,x;w
821 REM Calculate the value for PI and use our subroutine to round up the result
830 NEXT n
831 REM Loop round until we have asked for 3 numbers
840 GO TO 871
841 REM Skip over our subroutine
860 LET w=INT ((w*1000)+0.5)/1000
861 REM Multiply the number by 1000, add 0.5, take the integer and divide by 1000
862 REM This rounds the number up to 3 decimal places
870 RETURN
871 PRINT AT y+8,0;"Press a key ": PAUSE 0
```