How the LOG() function works in Mariadb?

The LOG() function is a mathematical function that returns the natural logarithm of a number.

Posted on

The LOG() function is a mathematical function that returns the natural logarithm of a number. The natural logarithm is the logarithm to the base e, where e is the mathematical constant that is approximately equal to 2.71828. The LOG() function can also accept an optional second argument that specifies a different base for the logarithm. The LOG() function is useful for performing various calculations, such as scaling, growth rate, exponential decay, etc.

Syntax

The syntax of the LOG() function is as follows:

LOG(number, [base])
LOG(base, number)

The number argument is the number whose natural logarithm is to be returned. The number argument must be a positive value, otherwise the function returns NULL. The base argument is an optional number that specifies the base of the logarithm. If the base argument is omitted or NULL, it is treated as e. If the base argument is 0 or negative, the function returns NULL.

The LOG() function returns a decimal value that represents the logarithm of the number to the base. If either the number or the base argument is NULL, the function returns NULL.

Examples

Example 1: Basic usage of the LOG() function

The following example shows how to use the LOG() function without the base argument. It returns the natural logarithm of the number 10.

SELECT LOG(10);

The output is:

2.302585092994046

This means that the natural logarithm of 10 is approximately 2.302585092994046.

Example 2: Using the base argument

The following example shows how to use the LOG() function with the base argument. It returns the logarithm of the number 100 to the base 10.

SELECT LOG(10, 100);

The output is:

2

This means that the logarithm of 100 to the base 10 is 2.

Example 3: Using the base argument with a value of e

The following example shows what happens when the LOG() function is used with the base argument that is equal to e. It returns the same result as the LOG() function without the base argument.

SELECT LOG(EXP(1), 10);

The output is:

2.302585092994046

This means that the logarithm of 10 to the base e is the same as the natural logarithm of 10.

There are some other functions that are related to the LOG() function in Mariadb. They are:

  • LN(): This function is a synonym for the LOG() function. It has the same syntax and behavior as the LOG() function.
  • LOG2(): This function returns the logarithm of a number to the base 2. It is equivalent to the LOG() function with the base argument of 2.
  • LOG10(): This function returns the logarithm of a number to the base 10. It is equivalent to the LOG() function with the base argument of 10.
  • EXP(): This function returns the exponential value of a number. It is the inverse of the LOG() function, meaning that EXP(LOG(x)) = x for any positive x.

Here are some examples of using these related functions:

-- Get the natural logarithm of 10
SELECT LN(10);

-- Get the logarithm of 16 to the base 2
SELECT LOG2(16);

-- Get the logarithm of 1000 to the base 10
SELECT LOG10(1000);

-- Get the exponential value of 2
SELECT EXP(2);

Conclusion

In this article, we have learned how the LOG() function works in Mariadb. We have seen its syntax, examples, and related functions. We have also learned how to use the optional base argument to specify a different base for the logarithm. The LOG() function is a useful function to calculate the logarithm of a number. It can be used for various purposes, such as scaling, growth rate, exponential decay, etc.