MySQL LOG() function

LOG() function

MySQL LOG() returns the natural logarithm of a number that is the base e logarithm of the number.

The return value will be NULL when the value of the number is less than or equal to 0.

The EXP() is the inverse of this LOG(). When LOG() execute with two parameters it returns the logarithm of the number to the base specified as a number in the first argument.

The return value will be NULL when the value of the number is less than or equal to 0 or the value of the number specified as the first argument is less than or equal to 1.

This function is useful in -

• The LOG() function is the inverse of exponential functions. It is used to solve equations where an exponent is in the form of a logarithm.
• In finance, the LOG() function can be used to calculate logarithmic returns that is important in investment analysis and portfolio management.
• In computer science and data compression, the LOG() function may be used in various algorithms, especially when dealing with logarithmic complexities or compression ratios.
• In information theory, the LOG() function is used to calculate the entropy of a random variable which is fundamental in measuring the information content of a message or data set.
• LOG() function is used in acoustics to calculate the decibel level, which is a logarithmic scale used to quantify the intensity of sound.
• The LOG() function is used in chemistry to calculate the pH of a solution, which measures its acidity or basicity.

Syntax:

```LOG (N), LOG(B, N);
```

Arguments:

Name Description
N A number.
B Base of the logarithm of N
N A number.

MySQL Version: 8.0

Note: LOG(B,N) is equivalent to LOG(N) / LOG(B).

Pictorial presentation of MySQL LOG() function

Example of MySQL LOG() function

Code:

``````SELECT LOG(3);
```
```

Explanation:

The above MySQL statement returns the natural logarithm of 3.

Output:

```mysql> SELECT LOG(3);
+------------------+
| LOG(3)           |
+------------------+
| 1.09861228866811 |
+------------------+
1 row in set (0.00 sec)
```

Example : LOG() function using negative value

Code:

``````SELECT LOG(-3);
```
```

Explanation:

The above MySQL statement returns NULL because the number specified in the argument is less than 0.

Output:

```mysql> SELECT LOG(-3);
+---------+
| LOG(-3) |
+---------+
|    NULL |
+---------+
1 row in set (0.00 sec)
```

Example: LOG() function using base of a number system

Code:

``````SELECT LOG(10,1000);
```
```

Explanation:

The above MySQL statement returns the natural logarithm of 1000 divided by the natural logarithm of 10.

Output:

```mysql> SELECT LOG(10,1000);
+--------------+
| LOG(10,1000) |
+--------------+
|            3 |
+--------------+
1 row in set (0.00 sec)
```

Example: LOG() with a smaller 1st argument

Code:

``````SELECT LOG(1,1000);
```
```

Explanation:

The above MySQL statement returns NULL because the value of the first number is 1.

Output:

```mysql> SELECT LOG(1,1000);
+-------------+
| LOG(1,1000) |
+-------------+
|        NULL |
+-------------+
1 row in set (0.00 sec)
```

All Mathematical Functions

Previous: LN()
Next: LOG2()

﻿