In SQLite, the LN() function returns the natural logarithm (logarithm to the base e) of a given positive number.
This function is helpful in mathematical calculations involving growth rates, exponential functions, and natural logarithmic transformations.
Syntax
LN(X)
Where X is the numeric expression (a positive number) for which you want the natural logarithm. Note that X must be greater than zero; otherwise, the function will return NULL.
Example
Here’s an example where we calculate the natural logarithm of several numbers:
SELECT LN(1) AS ln_result1,
LN(2.71828) AS ln_result2,
LN(10) AS ln_result3;
Result:
+------------+-------------------+------------------+
| ln_result1 | ln_result2 | ln_result3 |
+------------+-------------------+------------------+
| 0.0 | 0.999999327347282 | 2.30258509299405 |
+------------+-------------------+------------------+
Explanation:
LN(1)returns0because the natural logarithm of1is0.LN(2.71828)returns0.999999327347282, which is close to1, as2.71828is close to Euler’s numbere.LN(10)returns approximately2.30258509299405, which is the natural logarithm of10.
Rounding the Results
We can use the ROUND() function to round the results if required:
SELECT ROUND(LN(1), 2) AS ln_result1,
ROUND(LN(2.71828), 2) AS ln_result2,
ROUND(LN(10), 2) AS ln_result3;
Result:
+------------+------------+------------+
| ln_result1 | ln_result2 | ln_result3 |
+------------+------------+------------+
| 0.0 | 1.0 | 2.3 |
+------------+------------+------------+
Missing Arguments
Calling LN() without an argument results in an error:
SELECT LN();
Result:
Parse error: wrong number of arguments to function LN()
SELECT LN();
^--- error here
The LN() function provides a straightforward way to work with logarithmic transformations in SQLite for mathematical and data processing needs.