MySQL LN() Function – Return the Natural Logarithm of a Number

In MySQL, the LN() function returns the natural logarithm of a specified value.

You provide the specified value as an argument when calling the function.

This function is synonymous with the single-argument syntax of the LOG() function.

Syntax

The syntax goes like this:

LN(X)

Where X is the value for which you want the natural logarithm returned.

If X is less than or equal to 0.0E0, the function returns NULL and a warning is generated.

Example 1 – Basic Usage

Here’s a basic example to demonstrate how this function works.

SELECT LN(2);

Result:

+--------------------+
| LN(2)              |
+--------------------+
| 0.6931471805599453 |
+--------------------+

And here’s another example using a different value.

SELECT LN(0.1);

Result:

+---------------------+
| LN(0.1)             |
+---------------------+
| -2.3025850929940455 |
+---------------------+

Example 2 – Negative Value

Here’s an example of passing in a negative value.

SELECT LN(-1);

Result:

+--------+
| LN(-1) |
+--------+
|   NULL |
+--------+

This example returns a NULL value because the argument provided is less than 0.0E0.

Example 3 – Zero

Here’s an example of passing in zero as the argument (we get the same result as the previous example).

SELECT LN(0);

Result:

+-------+
| LN(0) |
+-------+
|  NULL |
+-------+

Example 4 – Expressions

You can also pass in expressions like this:

SELECT LN(1+1);

Result:

+--------------------+
| LN(1+1)            |
+--------------------+
| 0.6931471805599453 |
+--------------------+