Guide to Game Development/Theory/Mathematics/Logarithms

About
A logarithm is the answer to the question: "What number do I need to raise this known number to to get this other known number?"

Example of the problem:

$$2^x=32$$

Rewritten as a logarithm:

$$\log_2 (32) = x$$

You'd pronounce this as, "log base 2 of 32."

As you can see from the graph, you can't have a negative logarithm. If you think about it, this makes sense because it's a reflection of an exponential graph. When you have a positive number and you raise it to any number, the result will always be positive, and never 0; because of this the domain of a logarithm is as follows: $$\log_a (x), x > 0$$.

Combining logarithms
There are 3 rules that you should know when combining logarithms:

$$\log_k a + \log_k b = \log_k ab$$

$$\log_k a - \log_k b = \log_k \frac{a}{b}$$

$$a\log_k (b) = \log_k (b^a)$$

Expanding logarithms
When expanding logarithms, you can just do the inverse of what you did to combine them.

Example 1:

$$\log_4 \bigg(\frac{x^2}{5^3y}\bigg) = \log_4 (x^2) - \log_4 (5^3y) = \log_4 (x^2) - \log_4 (5^3) + \log_4(y) = 2\log_4 (x) - 3\log_4 (5) + \log_4(y)$$

Example 2 (knowledge of exponentials needed):

$$\log_9 \bigg(\frac{1}{\sqrt[3]{5}}\bigg) = \log_9 \bigg(\frac{1}{5^{\frac{1}{3}}}\bigg) = \log_9 (5^{-\frac{1}{3}}) = -\frac{1}{3}\log_9 5$$

Cancelling logarithms with exponentials and vice versa
There are a few more rules you need to know:

$$\log_a a^x = x$$

$$a^{\log_a x} = x$$

$$\log_a b = \frac{\log_n a}{\log_n b}$$ where $$n$$ can be any number.

$$\log_a 1 = 0$$

''Note: If you're doing a complex algorithm with a computer, try to give your answer in terms of a base 2 logarithm, because whatever base you input, the computer will convert it to base 2. Doing this for the computer so that it doesn't need to convert it each time will make the algorithm run faster.''

Balancing questions with these rules
So when you have a normal equation, you can do rules like "multiply both sides by 2" or "add 3 to both sides". With logarithms and exponentials, you can do something similar.

You can now:
 * "Raise both sides as an exponential of a number"
 * Example use:
 * $$log_3 x = 5 \rightarrow 3^{log_3 x} = 3^5 \rightarrow x = 3^5$$


 * "Log both sides (with a common base)"
 * Example use:
 * $$4^x = 7 \rightarrow \log_4 (4^x) = log_4 7 \rightarrow x = log_4 7$$

Notation
The mathematical constant $$e$$ is often used with logarithms as it's known as the natural base for a logarithm.

$$e = 2.71828182845904523536028747135266249775724709369995\dots$$

Certain short-hand notation has been developed for logarithms as follows:

$$\log_e (x) = \ln (x)$$

$$\log_2 (x) = $$ lb  $$(x)$$

$$\log_{10} (x) = \log (x)$$

Scale of magnitude
For this scale of magnitude, we'll be using 10 as we work in base as humans.

$$\log_{10}1 = 0$$

$$\log_{10}10 = 1$$

$$\log_{10}100 = 2$$

$$\log_{10}1000 = 3$$

$$\log_{10}10000 = 4$$

Note: remember it's a logarithmic scale, this means that half way between 10 and 100 (55) doesn't produce a half way value ($$\log_{10}55 \neq 1.5, \log_{10}55 \approx 1.74$$).