EXP

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: EXP
Abbreviation: E Shift+X
Type: Function
Token code: 189/$BD
Handling routine
in BASIC ROM:
49133–57410
$BFED–E042
List of all BASIC keywords


Remark: This article describes the BASIC command EXP in BASIC V2 at the Commodore 64.

Type: Numeric Function 
General Programming-Syntax: EXP(<Number>)

EXP is a mathematical function that evaluates the inverse natural LOG of the argument. That is, the argument X is applied as an exponent to e(2.71828183). So EXP(X) = eX.

Arguments in excess of 89.0296919 result in the BASIC error ?OVERFLOW ERROR IN line. Arguments less than -89.0296919 evaluate to 0.

When the argument doesn't evaluate to a number, the BASIC error ?TYPE MISMATCH ERROR IN line occurs. When the numeric argument is absent, it displays a ?SYNTAX ERROR IN line.

Examples[edit | edit source]

PRINT EXP(0) Screen: 1
PRINT EXP(-1) Screen: 0.367879441
PRINT EXP(1) Screen: 2.71828183

Implementation[edit | edit source]

The EXP function uses the identity:

ex = 2x * log2(e)

Let the parameter be X. EXP first calculates T = X * log2(e). It then separates T into an integer N and a fraction TF, such that N + TF = T and 0 <= TF < 1.

It then calculates 2TF by the polynomial approximation:

2.1498763701*10-5 * TF7 + 1.4352314037*10-4 * TF6 + 1.3422634825*10-3 * TF5 + 9.6140170135*10-3 * TF4 + 5.5505126860*10-2 * TF3 + 0.24022638460 * TF2 + 0.69314718618 * TF + 1.0

It then adds the integer N, calculated above, to the exponent of the result, effectively multiplying it by 2N. The result is 2T, which is equal to eX.

The polynomial resembles the seventh-degree Maclaurin polynomial expansion[1][2] Special case of 2x:

1 + x log(2) + 1/2 log2(2) x2 + 1/6 log3(2) x3 + 1/24 log4(2) x4 + 1/120 log5(2) x5 + 1/720 log6(2) x6 + 1/5040 log7(2) x7

but is most likely derived via the Remez algorithm. See POLY1#Notes_on_derivation for more details.

Accuracy[edit | edit source]

Accuracy of EXP function

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

At least for the plotted domain of [-5,+5], EXP matches the true value to 30 bits. No part of the domain proves to be problematic.

EXP calls POLY2, which calls MLTPLY ($BA59), and consequently the multiply bug affects its results. A small number of input values give results that match to only 26 bits.

References[edit | edit source]


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