Reading time : 2 min

Last update: February 11th, 2022

The logarithm function is used to calculate large numbers.

To calculate a logarithm, we use a base. Therefore, to understand the principle, you have to refer to the powers.

For example 100 = 10×10 = 10². The 10 is multiplied two times. The 10 is the base, and the logarithm value is equal to 2: log10(1000) = 2.

The logarithm is only calculated with positive numbers and greater than 1.

Half off everything 2manning

The 3 types of logarithms

There are 3 logarithms:

  • the neperian logarithm (or natural logarithm), noted ln, with base e -> ln(e) = 1
  • the decimal logarithm, with base 10 -> log10(1000) = 100 = 10×10 = 10² = 2
  • the binary logarithm, base 2 -> log2(256) = 256 -> 128 -> 64 -> 32 -> 16 -> 8 -> 4 -> 2 = 8 (steps)

The steps (the result) correspond to the number of divisions needed with the base before arriving at number one. As a reminder, you can't divide by zero. So we are always sure to have a positive number greater than 1—an interesting calculation for Machine Learning, for example.

Amusement Park II

Here the speed is illustrated with this roller coaster. In the first comic strip, the roller coaster shows fast, then progressively slows down to arrive at a cruising speed in the last comic strip, which corresponds to the arrival.

Code

pythonpython
1x = np.linspace(-3,3,100)
2
3ln = np.log(x)
4decimal = np.log10(x)
5binary = np.log2(x)
6
7plt.plot(x, ln, label='$natural$')
8plt.plot(x, decimal, label='$decimal$')
9plt.plot(x, binary, label='$binary$')
10
11plt.title('Logarithms',fontsize=12)
12plt.savefig('logarithms.png', bbox_inches='tight')
13
14plt.xlabel('x')
15plt.ylabel('y')
16plt.grid()
17plt.legend()
18plt.show()
2 types of logarithms plot

Formula

Logarithm of a product

The logarithm of a product is the sum of the logarithms of its factors.

Code

pythonpython
1a = 3
2b = 4
3 
4log1 = np.log(a*b) # 2.48491
5log2 = np.log(a) + np.log(b) # 2.48491

Formula

Logarithm of a quotient

The logarithm of a quotient is the difference of the logarithms of its two terms.

Code

pythonpython
1a = 3
2b = 4
3
4log1 = np.log(a / b) # −0.287682
5log2 = res3 = np.log(a) - np.log(b) # −0.287682

Formula

Logarithm of a power

The logarithm of a power is the product of the exponent and the logarithm of the base.

Code

pythonpython
1a = 3
2
3log1 = np.log(a ** 3)
4log2 = 3 * np.log(a)
5
6print(log1) # 3.295836866004329
7print(log2) # 3.295836866004329

Formula

Print booksmanning

The logarithm allows for calculations of large numbers, it is the inverse of the exponential function.

Resources

Source code