Q(uick)BASIC Function: TAN

Quick View

TAN Function

A math function that returns the tangent of an angle expressed 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
  • TAN(numeric-expression)
Description/Parameter(s)

TAN is calculated with single-precision accuracy, unless the argument x is a double-precision value; in that case TAN is calculated with double-precision accuracy.

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.

Differences from BASICA

  • In BASICA, if TAN overflows, the interpreter displays the "Overflow" error message, returns machine infinity as the result, and continues execution.
  • If TAN overflows, QuickBASIC does not display machine infinity, and execution halts (unless the program has an error-handling routine).
Example

The following example computes the height of an object using the distance from the object and the angle of elevation. The program draws the triangle produced by the base and the computed height.

SCREEN 2 INPUT "LENGTH OF BASE: ",Baselen INPUT "ANGLE OF ELEVATION (DEGREES,MINUTES): ",Deg,Min Ang = (3.141593/180)*(Deg + Min/60) 'Convert to radians. Height = Baselen*TAN(Ang) 'Calculate height. PRINT "HEIGHT =" Height H = 180 - Height B = 15 + Baselen LINE (15,180)-(B,180):LINE -(B,H) 'Draw triangle. LINE -(10,180) LOCATE 24,1 : PRINT "Press any key to continue..."; DO LOOP WHILE INKEY$=""
Syntax
  • TAN(x)
Description/Parameter(s)
  • The argument x can be of any numeric type.

Usage Notes

  • TAN is calculated in single precision if the argument x is an integer or single-precision value. If you use any other numeric data type, TAN 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.

Differences from BASICA

  • In BASICA, if TAN overflows, the interpreter generates the error message, "Overflow," returns machine infinity as the result, and continues execution. In the correct version of BASIC, if TAN overflows, machine infinity is not returned, and execution halts (unless the program has an error-handling routine).
Example

The following example uses the TAN function to compute the height of an object using the distance from the object and the angle of elevation. The program draws the triangle produced by the base and the computed height.

SCREEN 2 'CGA screen mode. INPUT "LENGTH OF BASE: ", Baselen INPUT "ANGLE OF ELEVATION (DEGREES,MINUTES): ", Deg, Min Ang = (3.1415928# / 180) * (Deg + Min / 60) 'Convert to radians. Height = Baselen * TAN(Ang) 'Calculate height. PRINT "HEIGHT ="; Height Aspect = 4 * (200 / 640) / 3 'Screen 2 is 640 x 200 pixels. H = 180 - Height B = 15 + (Baselen / Aspect) LINE (15, 180)-(B, 180) 'Draw triangle. LINE -(B, H) LINE -(15, 180) LOCATE 24, 1: PRINT "Press any key to continue..."; DO LOOP WHILE INKEY$ = ""