in BASIC ROM:
|List of all BASIC keywords|
Typ: Numeric Function General Programming-Syntax: LOG(<Numeric>)
The mathematical function LOG is a natural logarithm with the basis e(E). If the term is 0 or negative the BASIC error ?ILLEGAL QUANTITY ERROR IN line occurs. When the number is over 1.7014118342E+38 the BASIC-error ?OVERFLOW ERROR IN line appears. When the numeric argument is absent, a ?SYNTAX ERROR IN line appears.
PRINT LOG(1) Result: 0 PRINT LOG(10) Result: 2.30258509 PRINT LOG(100)/LOG(10) Result: 2 because you can divide by LOG(10) and get a result with the base "10".
Let the parameter be X. The LOG function first separates the exponent of X from the significand. That is, it calculates two numbers, N and XF, such that:
- X = 2N * XF, N is an integer and 0.5 <= XF < 1.0
C programmers will recognize this separation as the frexp() function.
The natural logarithm is tricky to calculate accurately. The Taylor series converges too slowly to be useful. The BASIC implementation uses a faster approach. Given XF calculated above, it calculates:
- T = 1.0 - sqrt(2.0) / (XF + sqrt(0.5))
It then applies the polynomial approximation:
- 0.43425594189 * T7 + 0.57658454124 * T5 + 0.96180075919 * T3 + 2.8853900731 * T - 0.5
The result of this approximation is log2(XF).
The routine then adds N to that approximation, resulting in log2(X). Finally, it multiplies by LOG(2), giving the final result of LOG(X).
The derivation of the polynomial is not publicly known.