In PostgreSQL, ln()
is a mathematical function that returns the natural logarithm of its argument.
Syntax
The official syntax goes like this:
ln(dp or numeric)
Where dp
is a double precision value.
Example
Here’s an example of how it works.
SELECT ln(10);
Result:
2.302585092994046
Fractions
This example includes a fractional part in the argument.
SELECT ln(32.53);
Result:
3.4821627404852601
Expressions
You can include expressions, such as this.
SELECT ln(12 * 2);
Result:
3.1780538303479458
Ln() vs Exp()
The ln()
function returns the natural logarithm, which is the inverse of exp()
, which returns the exponential value of its argument.
The exponential of the natural logarithm of a number is the number itself.
And the natural logarithm of the exponential of a number is the number itself.
Here’s an example that verifies this.
SELECT
ln(exp(1)),
exp(ln(1));
Result:
ln | exp ----+----- 1 | 1