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.

In this lesson we will learn some more mathematical codewords. The first three of them are used in trigonometry calculations, and relate to angles (degrees and radians). The other two are related to logarithms and exponentials. As with the codewords in Lesson 7, if you have not yet learned about trigonometry, radians, exponentials or logarithms, don’t worry. Just ask your maths teacher what they mean, or if you prefer you can skip these few codewords and come back to them later when you’ve learned their meaning. The same applies if you have not yet been taught about radians.

**SIN **This codeword tells the computer to calculate the sine of an angle.

**COS **This codeword tells the computer to calculate the cosine of an angle.

**TAN **This codeword tells the computer to calculate the tangent of an angle.

**EXP **This codeword tells the computer to calculate the value of “e” raised to a given power. For example, e^{2 }means “e raised to the power 2″ (also known as “e squared”). And e^{3 }means “e raised to the power 3” (also known as “e cubed”). In mathematics “e” is known as the “exponential” or “natural” number, and its value is 2.7182818 . . . . . (never ending).

**LN **This codeword tells the computer to calculate what we call the “natural logarithm” of a number, also called the “logarithm to base”.

The SIN codeword is a function.

The SIN codeword gives the sine of an angle.

The SIN codeword must be followed by the angle or value for which you want the computer to determine its sine. This angle or value must be given in *radians*. You can convert an angle in degrees into radians by multiplying the number of degrees by PI and then dividing the result by 180. So an angle in radians = ( angle in degrees * PI / 180).

For example, the program line:

**59 LET x = SIN 4**

will return the sine of the angle 2.4 radians, and assign it to x

and the line:

**60 LET c = SIN (a - b)**

will assign to the variable c the value of the expression sine (a-b), where a and b are in radians. Note here that when we tell the computer to determine the value of the sine of an expression, rather than just the sine of a single variable, we must put that expression inside brackets as we have done in line 60.

Note that SIN returns a positive value for angles between 0 and 180 degrees (PI radians), and a negative value for angles between 180 and 360 degrees (PI and 2* PI radians).

The COS codeword is a function.

The COS codeword gives the cosine of an angle.

The COS codeword must be followed by the angle or value for which you want the computer to determine its cosine. This angle or value must be given in *radians*. You can convert an angle in degrees into radians by multiplying the number of degrees by PI and then dividing the result by 180. So an angle in radians = ( angle in degrees * PI / 180).

For example, the program line:

**61 LET x = COS 85**

will return the sine of the angle 1.85 radians, and assign it to x

and the line:

**62 LET c = COS (a - b)**

will assign to the variable c the value of the expression cosine (a-b), where a and b are in radians. Note here that when we tell the computer to determine the value of the cosine of an expression, rather than just the cosine of a single variable, we must put that expression inside brackets as we have done in line 62.

Note that COS returns a negative value for angles from 90 to 270 degrees and a positive value for angles from 0 to 90 degrees and from 270 to 360 degrees.

TAN gives the tangent of an angle.

The TAN codeword is a function.

The TAN codeword gives the tangent of an angle.

The TAN codeword must be followed by the angle or value for which you want the computer to determine its cosine. This angle or value must be given in *radians*. You can convert an angle in degrees into radians by multiplying the number of degrees by PI and then dividing the result by 180. So an angle in radians = ( angle in degrees * PI / 180).

For example, the program line:

**63 LET x = TAN 0.72**

will return the tangent of the angle 0.72 radians, and assign it to x

and the line:

**64 LET c = TAN (a - b)**

will assign to the variable c the value of the expression tangent (a-b), where a and b are in radians. Note here that when we tell the computer to determine the value of the tangent of an expression, rather than just the tangent of a single variable, we must put that expression inside brackets as we have done in line 64.

Note that TAN returns a positive value for angles between 0 and 90 degrees. For angles between 90 and 180 degrees, and for angles between 270 and 360 degrees, TAN returns a negative value.

The EXP codeword is a function.

The EXP codeword uses a number which is called “e” in mathematics. “e” is known as the “exponential” or “natural” number, and its value is 2.7182818 . . . . . (never ending).

The EXP codeword tells the computer to calculate the value of â€œeâ€ raised to a given power. For example, e

^{2 }means “e raised to the power 2” (also known as “e squared”). In BASIC this is written e**2.And e

^{3 }means “e raised to the power 3″ (also known as “e cubed”). In BASIC this is written e**3.

The EXP codeword must be followed by the number or value to the power of which you want the computer to raise the number *e*.

For example, the program line:

**65 LET x = EXP 4**

will return the value of *e *raised to the power 4, and assign it to x

Here is another example: The program line:

**70 PRINT EXP 1**

tells the computer to display 2.7182818 on the screen, which is the value of *e*. This is because EXP 1 means e^{1 }(in other words *e* raised to the power 1, which is the same as e itself).

Here is yet another example. The line:

**66 LET c = EXP (a - b)**

will assign to the variable c the value of the expression “*e* raised to the power (a-b)”. Note here that when we tell the computer to determine the value of *e* raised to a power which is given as an expression, rather than just to the power of a single variable, we must put that expression inside brackets as we have done in line 66.

The LN codeword is a function.

LN tells the computer to calculate what we call the natural logarithm of a value or number (this is the logarithm to base

e).

The LN codeword must be followed by the number or value whose natural logarithm you want the computer to calculate.

For example, the program line:

**67 LET x = LN 4**

will return the value of the natural logarithm of 4, and assign it to x

Here is another example. The line:

**68 LET c = LN (a - b)**

will assign to the variable c the value of the natural logarithm of (a-b). Note here that when we tell the computer to determine the natural logarithm of an expression, rather than the natural logarithm of a number or the value of a single variable, we must put that expression inside brackets as we have done in line 68.

It is important to remember that the number following LN must be greater than 0. And if it is a variable or an expression then its value must be greater than 0. If you try to tell the computer to return the natural logarithm of a negative number you will have a bug in your program.

This is a more complicated program to draw a clock.

Line 410 to 460 ask for the time to display.

Lines 710 to 720 draw the clock face. In Lines 740 to 820 we draw the hour marks calculating where they appear. Line 830 calculates the angle for the big hand and line 840 calculates the angle for the small hand.Lines 850 and 860 draw the hands using the subroutine from line 880 to 920

```
410 BRIGHT 0: INPUT "Enter the hour (1-12) 0=finish ";h
415 REM Ask for the hour
420 IF h<0 OR h>12 THEN GO TO 410
425 REM check if we are finished and if so GOTO 490
430 IF h=0 THEN GO TO 490
440 INPUT "Enter the minutes (0-59)";m
445 REM Ask for the minutes
450 IF m<0 OR m>60 THEN GO TO 440
455 REM check that it is not out of range and if so ask again
460 IF m=60 THEN GO TO 500
465 REM Check if we are finished
470 GO SUB 700
475 REM draw our clock
480 GO TO 410
485 REM Loop back to the beginning
490 BRIGHT 0: CLS : LET m$="You can see the listing for this program in the printed documentation": GO SUB 520
500 PAUSE 0
510 STOP
520 LET l=LEN (m$)
530 IF l>32 THEN GO TO 560
540 PRINT m$
550 RETURN
560 LET o=33
570 LET c$=m$(o TO o)
580 IF (c$=" ") OR ((o<32) AND ((c$=".") OR (c$=","))) THEN GO TO 620
590 LET o=o-1
600 IF o>0 THEN GO TO 570
610 LET o=32
620 IF m$(o TO o)=" " THEN GO TO 650
630 PRINT m$(1 TO o)
640 GO TO 660
650 PRINT m$(1 TO o-1)
660 LET m$=m$(o+1 TO )
670 GO TO 520
700 BRIGHT 0: PAPER 7: INK 1: CLS
710 LET r1=80: LET r2=65
715 REM Set the size of the clock
716 REM r1 is the radius of the clock face
717 REM r2 is the radius of the inner circle
720 CIRCLE 128,88,r1
725 REM Draw the clock face
730 BRIGHT 1
740 FOR n=0 TO 11
745 REM Here we draw the hours on the clock
750 LET a=2*PI*n/12
755 REM Calculate the angle of the hour
760 LET x1=INT (128+SIN (a)*r1): LET x2=INT (128+SIN (a)*r2)
770 LET y1=INT (88+COS (a)*r1): LET y2=INT (88+COS (a)*r2)
775 REM Calculate the positions of the hours for the markers
780 LET xc=INT (x2/8): LET yc=INT ((175-y2)/8)
785 REM Calculate the text position of where to print the numbers
790 PLOT x1,y1: DRAW x2-x1,y2-y1
795 REM Draw the hour mark
800 IF (n=0) THEN PRINT AT yc,xc-1;"12"
805 REM Draw the 12 hour
810 IF (n<>0) THEN PRINT AT yc,xc;n
815 REM Draw the other hours
820 NEXT N
825 REM Draw all the hour marks
830 LET b=m*360/60
835 REM Calculate the angle for the minute hand
840 LET s=h*360/12+(m/60*(369/12))
845 REM Calculate the angle for the hour hand
850 INK 1: LET a=b: LET r=62: GO SUB 880
855 REM Use our subroutine to draw the hour hand
860 INK 0: LET a=s: LET r=45: GO SUB 880
865 REM Use our subroutine to draw the minute hand
870 RETURN
880 REM This is a subroutine to draw a clock hand
881 REM a is the angle of the hand and r is the radius of the hand
885 LET a=2*PI*a/360
886 REM Calculate the angle of the hand in radians
890 LET x=INT (128+SIN (a)*r)
895 REM Calculate the x position of the hand
900 LET y=INT (88+COS (a)*r)
905 REM calculate the y position of the hand
910 PLOT 128,88: DRAW x-128,y-88
915 REM Draw the hand from the centre to the outer position
920 RETURN
```