# Mathematics - Logarithm Function (log)

$log_b(n)$ is the number (#) of times you divide n by b until you get down to 1.

If you see log, you must think trees

## Example

With the base 2 where $log_2 n$ is the # of times you divide n by 2 until you get down to 1.

You keep repeating dividing by two and you count how many times you divide by two until you get a number that drops below one

$$\begin{array}{rrl} log_2 (42) = 5.39 &&&\\ 1- & 42/2 & = & 21 \\ 2- & 21/2 & = & 10.5 \\ 3- & 10.5 / 2 & = & 5.25 \\ 4- & 5.25/2 & = & 2.625 \\ 5- & 2.625/2 & = & 1.3125 \\ 6- & 1.3125/2 & = & 0.65625 \\ \end{array}$$

## Property

The logarithm is much, much smaller than the input. log is running much, much, much slower than the identity function. $$\begin{array}{rrl} log_2 (10) & \approx & 3 \\ log_2 (100) & \approx & 7 \\ log_2 (1000) & \approx & 10 \\ \end{array}$$

## Formula

$$b^n = a$$

• The log is defined as:

$$n = log_b(a)$$

log x without an explicit base may also refer to the natural logarithm.

## Law

Logarithms permit to replace multiplication by addition.

$$log_b(x \times y) = log_b(x) + log_b(y)$$

It was more comfortable to carry at a time the calculator does not exist.

### Base modification

$$log_b(a) = \frac{log_2(a)}{log_2(b)} = \frac{log_e(a)}{log_e(b)} = \dots = \frac{log_x(a)}{log_x(b)}$$

### Log base 2 approximation

$$log_2(x) \approx log_e(x) + log_{10}(x)$$ where:

## Computer Language

In most of the computer language:

• the log function returns the natural logaritm
• there is also precomputed value in order to help to change base thanks to the division law.

For instance, in javascript, Math.log(x) returns the natural logarithm and to get the log on base 10, you would use the LN10 constant (ie ln(10))

function log10(val) {
return Math.log(val) / Math.LN10;
}