How the LN() function works in Mariadb?

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

Posted on

The LN() function is a mathematical function that returns the natural logarithm of a given number. The natural logarithm is the logarithm to the base e, where e is the mathematical constant approximately equal to 2.71828. The LN() function is a synonym for the LOG() function and can be used interchangeably.

Syntax

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

LN(x)

The x is the number whose natural logarithm is to be calculated. It can be any valid numeric expression in Mariadb, such as column values, literal values, or variables. The x must be a positive number, otherwise the function returns NULL.

The LN() function returns a numeric value that represents the natural logarithm of the input number. If the input number is NULL, the function returns NULL.

Examples

In this section, we will show some examples of how to use the LN() function in Mariadb.

Example 1: Calculating the natural logarithm of a literal value

The following example shows how to use the LN() function to calculate the natural logarithm of a literal value.

SELECT LN(1);

The output is:

LN(1)
-----
0

As you can see, the LN() function returns 0, which is the natural logarithm of 1.

Example 2: Calculating the natural logarithm of a column value

The following example shows how to use the LN() function to calculate the natural logarithm of a column value. Suppose we have a table called numbers that stores some numeric values, such as x and y. The table has the following data:

id x y
1 2 3
2 4 5
3 6 7
4 8 9

We can use the LN() function to calculate the natural logarithm of the x column, as shown below:

SELECT id, x, LN(x) AS ln_x FROM numbers;

The output is:

id x ln_x
1 2 0.6931471805599453
2 4 1.3862943611198906
3 6 1.791759469228055
4 8 2.0794415416798357

As you can see, the LN() function returns the natural logarithm of the x column values.

Example 3: Calculating the natural logarithm of a variable value

The following example shows how to use the LN() function to calculate the natural logarithm of a variable value. Suppose we have a variable that stores a numeric value, such as @z. We can use the LN() function to calculate the natural logarithm of the variable value, as shown below:

SET @z = 10;
SELECT LN(@z);

The output is:

LN(@z)
------
2.302585092994046

As you can see, the LN() function returns the natural logarithm of the variable value.

Example 4: Calculating the natural logarithm of an invalid value

The following example shows how to use the LN() function to calculate the natural logarithm of an invalid value.

SELECT LN(-1);

The output is:

LN(-1)
------
NULL

As you can see, the LN() function returns NULL, because the input value is a negative number, which is not valid for the natural logarithm.

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

  • The LOG() function: This function is a synonym for the LN() function and can be used interchangeably. For example, LOG(1) is equivalent to LN(1).
  • The LOG10() function: This function returns the logarithm to the base 10 of a given number. For example, LOG10(100) returns 2.
  • The LOG2() function: This function returns the logarithm to the base 2 of a given number. For example, LOG2(8) returns 3.
  • The EXP() function: This function returns the exponential value of a given number. It is the inverse of the LN() function. For example, EXP(LN(2)) returns 2.

Conclusion

In this article, we have learned how the LN() function works in Mariadb. We have seen the syntax of the function, and some examples of how to use it with different types of numeric expressions. We have also learned about some related functions that can be used with the LN() function. The LN() function is a useful function that can help us calculate the natural logarithm of a number in Mariadb.