log
log Function
Syntax:
log number &optional base → logarithm
Arguments and Values:
number—a non-zero number .
base—a number .
logarithm—a number .
Description:
log returns the logarithm of number in base base. If base is not supplied its value is e, the base of the natural logarithms.
loglog may return a complex when given a real negative number.
(log -1.0) ≡ (complex 0.0 (float pi 0.0))
If base is zero, log returns zero.
The result of (log 8 2) may be either 3 or 3.0, depending on the implementation. An implementation can use floating-point calculations even if an exact integer result is possible.
The branch cut for the logarithm function of one argument (natural logarithm) lies along the negative real axis, continuous with quadrant II. The domain excludes the origin.
The mathematical definition of a complex logarithm is as follows, whether or not minus zero is supported by the implementation:
(log x) ≡ (complex (log (abs x)) (phase x))
Therefore the range of the one-argument logarithm function is that strip of the complex plane containing numbers with imaginary parts between −π (exclusive) and π (inclusive) if minus zero is not supported, or −π (inclusive) and π (inclusive) if minus zero is supported.
The two-argument logarithm function is defined as
(log base number)
≡ (/ (log number) (log base))
This defines the principal values precisely. The range of the two-argument logarithm function is the entire complex plane.
Examples:
(log 100 10)
→ 2.0
→ 2
(log 100.0 10) → 2.0
(log #c(0 1) #c(0 -1))
→ #C(-1.0 0.0)
<i><sup>or</sup>→</i> #C(-1 0)
(log 8.0 2) → 3.0
(log #c(-16 16) #c(2 2)) → 3 or approximately #c(3.0 0.0)
or approximately 3.0 (unlikely)
Affected By:
The implementation.
See Also:
exp, expt, Section 12.1.3.3 (Rule of Float Substitutability)
Expanded Reference: log
Natural logarithm
With one argument, log returns the natural logarithm (base e).
(log 1)
=> 0.0
(log 2.718282)
=> 1.0
(log (exp 1))
=> 0.99999994
Logarithm with a specified base
The optional second argument specifies the base.
(log 100 10)
=> 2.0
(log 8 2)
=> 3.0
(log 100.0 10)
=> 2.0
(log 81 3)
=> 4.0
Logarithm of negative numbers
Taking the logarithm of a negative number returns a complex result.
(log -1)
=> #C(0.0 3.1415927)
(log -1.0)
=> #C(0.0 3.1415927)
Inverse of exp and expt
log is the inverse of exp for natural logarithms.
(log (exp 5))
=> 5.0
(exp (log 10))
=> 10.0
Practical use: computing number of digits
The logarithm base 10 tells you the number of digits in a positive integer.
(defun num-digits (n)
(1+ (floor (log n 10))))
(num-digits 1)
=> 1
(num-digits 99)
=> 2
(num-digits 1000)
=> 4