Q(uick)BASIC Function: COS
Quick View
COS Function
A math function that returns the cosine of an angle given in radians
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- ATN(numeric-expression)
- COS(angle)
- SIN(angle)
- TAN(angle)
Description/Parameter(s)
numeric-expression | The ratio between the sides of a right triangle. |
angle | An angle expressed in radians. |
The ATN function returns an angle in radians.
To convert from degrees to radians, multiply degrees by (PI / 180).
Example
CONST PI=3.141592654
PRINT ATN(TAN(PI/4.0)), PI/4.0 'Output is: .7853981635 .7853981635
PRINT (COS(180 * (PI / 180))) 'Output is: -1
PRINT (SIN(90 * (PI / 180))) 'Output is: 1
PRINT (TAN(45 * (PI / 180))) 'Output is: 1.000000000205103
Syntax
- COS(numeric-expression)
Description/Parameter(s)
The expression, numeric-expression, can be any numeric type.
By default, the cosine is calculated in single precision. If numeric- expression is a double-precision value, the cosine is calculated in double precision.
You can convert an angle measurement from degrees to radians by multiplying the degrees by ã/180, where ã = 3.141593.
To convert a radian value to degrees, multiply it by 57.2958.
Example
The following program plots the graph of the polar equation r = né for values of n from 0.1-1.1. This program uses the conversion factors x = cos(0) and y = sin(0) to change polar coordinates to Cartesian x,y coordinates. The figure plotted is sometimes known as the Spiral of Archimedes.
CONST PI = 3.141593
'Gray background.
SCREEN 1 : COLOR 7
'Define window large enough for biggest spiral.
WINDOW (-4,-6)-(8,2)
'Draw line from origin to the right.
LINE (0,0)-(2.2*PI,0),1
'Draw ten spirals.
FOR N = 1.1 TO .1 STEP -.1
'Plot starting point.
PSET (0,0)
FOR Angle = 0 TO 2*PI STEP .04
'Polar equation for spiral.
R = N * Angle
'Convert polar coordinates to Cartesian coordinates.
X = R * COS(Angle)
Y = R * SIN(Angle)
'Draw line from previous point to new point.
LINE -(X,Y),1
NEXT
NEXT
Syntax
- COS(x)
Description/Parameter(s)
- The argument x can be of any numeric type.
Usage Notes
- The cosine of an angle in a right triangle is the ratio between the length of the side adjacent to the angle and the length of the hypotenuse.
- COS is calculated in single precision if the argument x is an integer or single-precision value. If you use any other numeric data type, COS is calculated in double-precision.
- To convert values from degrees to radians, multiply the angle (in degrees) times PI/180 (or .0174532925199433) where PI = 3.141593.
- To convert a radian value to degrees, multiply it by 180/PI (or 57.2957795130824) where PI = 3.141593.
Example
This program uses the COS function to plot the graph of the polar equation r = n * é for ten values of n from 1.1 to 0.1. The program uses the transformations x = r * cos(é) and y = r * sin(é) to change polar coordinates (r, é) to Cartesian coordinates (x, y). The figure plotted is sometimes known as the Spiral of Archimedes.
CONST PI = 3.141593
'Gray background.
SCREEN 1: COLOR 7
'Define window large enough for biggest spiral.
WINDOW (-4, -6)-(8, 2)
'Draw line from origin to the right.
LINE (0, 0)-(2.2 * PI, 0), 1
'Draw ten spirals.
FOR N = 1.1 TO .1 STEP -.1
'Plot starting point.
PSET (0, 0)
FOR Angle = 0 TO 2 * PI STEP .04
'Polar equation for spiral.
R = N * Angle
'Convert polar coordinates to Cartesian coordinates.
X = R * COS(Angle)
Y = R * SIN(Angle)
'Draw line from previous point to new point.
LINE -(X, Y), 1
NEXT
NEXT