COS

f = COS (x)

Cosine of x. x is in radians.

Example 1

a = cos(0.5)
print a             ' Output: 0.87758256189037

b = acos(a)
print b             ' Output:  0.5

Example 2


'TRIG lesson one.bas  SmallBASIC 0.12.2 [B+=MGA] 2016-03-12
'note: the lines in the diagram have been thickened to help see and identify the color
const green=rgb(0,128,0) 'never liked QB color for green
sub drawTRIGtri
'3 points make a triangle, one point requires an x,y coordinate
xp1=xmax-100-.25*ymax : yp1=ymax-100 'bottom left angle of triangle
xp2=xmax-100 : yp2=ymax-100 'bottom right corner with right angle
xp3=xmax-100 : yp3=ymax-100-.433*ymax 'upper corner
'green botton line is called side adjacent the yellow angle at the base of this triangle (not the 90 degree right angle)
line xp1,yp1,xp2,yp2,green
line xp1-1,yp1+1,xp2+1,yp2+1,green
line xp1-2,yp1+2,xp2+2,yp2+2,green

'blue vertical line is call side opposite the yellow angle at the base of this triangle (not the 90 degree right angle)
line xp2,yp2,xp3,yp3,9
line xp2+1,yp2+1,xp3+1,yp3-1,9
line xp2+2,yp2+2,xp3+2,yp3-2,9   
'red diagonal line always the longest of a right triangle is called the hypotenuse
for i=0 to 6
  line xp1,yp1+.5*i,xp3,yp3+i,12
next
'mark the 90 degree angel
rect xp2-50,yp2-50 step 48,48,15 
at xp2-50+10,yp2-50+15:?"90"
'the yellow angle: ARC x,y,r, start radians, stop radians[, aspect [, color]] note: start clockwise to stop radians
arc xp1,yp1,.2*ymax/2,2*pi-pi/3,0,1,14
arc xp1,yp1,.18*ymax/2,2*pi-pi/3,0,1,14
'note: here 0 degrees is due east and the angle advances clockwise
end
while 1
cls
drawTRIGtri
at 0,0
?"A man put up a pole (blue) and attached a 100 foot wire (red)"
?"from top to ground. Now he needs to know the height of the"
?"pole. He doesn't have a ladder nearly that high but he"
?"can use a first lesson in trignometry to get the height of"
?"the pole."
?
?"Trignometry means right triangles. A right triangle is one
?"that contains a 90 degree angle, the same angle a proper 
?"pole makes with the ground."
?
?"Trig has made a study of right triangles and made one big
?"discovery: The RATIO of the sides of a right triangle are 
?"always the same if the ANGLES of the sides (AKA legs and 
?"hypotenuse) are the same." 
?"That is trig in a nutshell. Notice I said RATIO of lengths."
?
?"press any when ready...
pause
cls
drawTRIGtri
at 0,0
?"The two main RATIOs of Trig are SIN and COS."
?"COS (AKA COSINE) is the RATIO of the green side (next to"
?"the yellow ANGLE) to the hypotenuse (red diagonal) AKA"  
?"(adjacent leg) / (hypotenuse)  shortened to (adj/hyp)"
?"or here (green line length) / (red line length)."
?"That's it!  COS is the leg length next to the angle in"
?"question divided by the diagonal length."
?
?"So the man measures the distance from the pole to where"
?"the wire meets the gound and gets 50 feet. This is the"
?"green leg next to the angle we want to figure out for the"
?"COS RATIO (adj/hyp). In this case it is 50 ft along the" 
?"ground divided by 100 ft the diagonal red wire, 50/100=.5" 
?"So that means COS of the angle is .5 the man says."
?
?"So the man goes to some trig tables and finds what angle"
?"makes a COS ratio of .5, he is looking up the Arc COS of"
?"the ratio(.5) = the angle whose adj-leg/hypotenuse is .5" 
?"Oh! he says that is a 60 degree angle the wire is making"
?"with the ground, the yellow angle is 60 degrees."
?
?"Press any key when ready..."
Pause
cls
drawTRIGtri
at 0,0
?"Now that he knows the angle is 60 degrees he can use the"
?"SIN ratio to calculate the height of the pole. The SIN"
?"RATIO is opp/hyp, the (opposite leg length)/(hypotenuse)"
?"The pole/wire = SIN of 60 degrees = .8660"
?"or pole/wire=.866."
?
?"Multiply both sides of this equation by wire length and" 
?"get pole=.866*wire."
?"Since the wire is 100, pole = .866*100 = 86.6 feet."
?
?"press any for first screen again..."
pause
wend

Example 3

At an angle around a given point, SIN and COS and radius (or distance) can locate the absolute screen x,y coordinate.

' Diagrammed SIN and COS.bas  SmallBASIC 0.12.2 [B+=MGA] 2016-03-16
' ah finally got diagramed analysis built into single quote commented code!!!
' the color codes match original Trig lesson one
' reminder XMAX, YMAX and PI are built in constants
CONST green =RGB(0,128,0)
' a central point cx,cy around which to draw 
cx=XMAX/2:cy=YMAX/2 'again cx,cy can be anywhere on screen so dont make them constant
' highlight center
CIRCLE cx,cy,2,1,15
r=200 'the radius or distance in pixels from cx,cy desired
CIRCLE cx,cy,r,1,8  'we want a point somewhere on this circle, radius = constant distance away from point
' locate point 30 degrees and r distance from cx,cy       30 degrees = PI/6 radians
' here is 30 degrees in tiny purple circle around x=r*cos(rad(30),y=r*sin(rad(30)
CIRCLE cx + r*COS(RAD(30)), cy + r*SIN(RAD(30)), 3, 1, 13   'x,y,r=3,aspect=1,color 13
legend
LINE cx,cy,cx+r*COS(RAD(30)),cy,green 'adjacent leg in red
LINE cx+r*COS(RAD(30)),cy,cx+r*COS(RAD(30)),cy + r*SIN(RAD(30)),9 'leg opposite in blue
LINE cx,cy,cx+r*COS(RAD(30)),cy + r*SIN(RAD(30)),12
RECT cx+r*COS(RAD(30))-11,cy+2 step 9,9,15
' V there are 12, 30 degree angles in circle (2*PI) so 2*PI/12 or PI/6
ARC cx,cy,30,0,PI/6,1,14
ARC cx,cy,35,0,RAD(30),1,14

sub legend
  at 0,0
  ?"green = leg adj, blue =leg opp, red = hypotenuse = radius"
  ?"white square is where the right angle is, yellow arcs mark angle in question
  ?"COS(yellow) = adj/hyp = green/red, red is radius of circle
  ?"SIN(yellow) = opp/hyp =  blue/red, red is radius of circle
end
Math
If there is insufficient information on this page and you wish learn more about COS, please send an email to smallbasic@gmail.com. You can help to improve information about COS by submitting a pull request, click View Source for details. Note, an offline language reference text file is also available - see the Download section.