TAN

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: TAN
Abbreviation:
Type: Function
Token code: 192/$C0
Handling routine
in BASIC ROM:
58036–58079
$E2B4–E2DF
List of all BASIC keywords


Remark: This article describes the BASIC command TAN in BASIC V2 of the Commodore 64.

Type: Numeric Function
General Programming-Syntax: TAN(<numeric-expression>)

The numerical function TAN is a mathematical function which evaluates to the tangent for a given angle, a number regarded as being in radians.

If the term given in the parentheses is non-numerical, the BASIC error ?TYPE MISMATCH ERROR occurs. Being a floating point argument, the term must be in the range of ±2.93873588·10−38 to ±1.70141183·1038. Exceeding these limits yields the BASIC error ?OVERFLOW ERROR. Special values like +/-π/2 (+/- 90 degree) according to the definition of the function lead to the BASIC error ?DIVISION BY ZERO. Omitting the numeric argument or to many arguments results in a ?SYNTAX ERROR.

Accuracy[edit | edit source]

Accuracy of TAN function

The graph to the right was obtained by comparing results from the BASIC TAN function to values computed on a modern x86-type PC.

For most inputs, TAN is accurate to about 29 bits. Values for which SIN and COS lose accuracy also lose accuracy for TAN. The accuracy close to X = 0 could be improved by patching SIN to return SIN(X) = X for |X| less than 2-11.

TAN calls SIN and COS, which call POLY1, which calls MLTPLY ($BA59), and consequently the multiply bug affects its results. A small number of input values give results that match to only 25 bits.


Implementation details[edit | edit source]

TAN(X) is implemented simply as SIN(X)/COS(X).

Examples[edit | edit source]

PRINT TAN(1)

Output: 1.55740772

10 DEF FN CT(X) = 1/TAN(X)
20 PRINT FN CT(45*π/180)

Defines the function of cotangent (tangent complement) as user defined function FN CT(), called with an angle of 45°.

PRINT TAN(π/2)

Provokes the error ?DIVISION BY ZERO ERROR for an argument representing the angle of 90°.

PRINT TAN(π)

On screen the value -7.3145904E-10 will be shown, a very small value near to 0 (also for all multiples of π), but not exactly reached caused by the inaccuratness of the internal implementation of the function.


BASIC V2.0 (second release) Commands

ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT