In PostgreSQL LOG(), LN(), and LOG10() function are ,most commonly used functions to compute logarithms , although there are several functions to compute logarithms. The most commonly used are LOG(), LN(), and LOG10() function. Each function has its specific use case:
- LOG(base, numeric): Computes the logarithm of a number to a specified base.
- LN(numeric): Computes the natural logarithm (base e) of a number.
- LOG10(numeric): Computes the base 10 logarithm of a number.
PostgreSQL provides several functions to compute logarithms, allowing you to work with different bases and perform various mathematical and data analysis tasks. The primary functions are LOG() for custom bases, LN() for the natural logarithm, and LOG10() for base 10 logarithms. These functions can be applied to both constant values and column values in a table.
LOG() Function in PostgreSQL Syntax:
LOG(base, numeric):
LOG() function computes the logarithm of a number to a specified base.
Example:
SELECT LOG(2, 16) AS log_base2_of_16, LOG(10, 100) AS log_base10_of_100;
Result:
LN() Function in PostgreSQL – Natural Logarithm (base e):
Syntax:
LN(numeric)
LN() function computes the natural logarithm (base e) of a number.
Example:
SELECT LN(2.7182818284590453) AS natural_log_of_e, LN(10) AS natural_log_of_10;
Result:
LOG10() Function in PostgreSQL:
Syntax:
LOG10() function computes the logarithm of a number to the base of 10
Example:
SELECT LOG10(100) AS log10_of_100, LOG10(1000) AS log10_of_1000;
Result:
Get the Logarithm of column in PostgreSQL table:
We will be using measurements table.
LOG() Function of column in PostgreSQL:
In our example we will use log() function to compute Logarithm to base 2 of a column in PostgreSQL table
SELECT id, value, LOG(2, value) AS log_base2 FROM measurements;
Using log() To compute logarithms to base 2 for the values:
Output:
LN() Function of column in PostgreSQL – Natural logarithm (base e):
In our example we will use LN() function to compute Logarithm to base e of a column in PostgreSQL table
SELECT id, value, LN(value) AS natural_log FROM measurements;
Using LN() To compute natural logarithms to base e for the values:
Output:
LOG10() Function of column in PostgreSQL:
In our example we will use log10() function to compute Logarithm to base 10 of a column in PostgreSQL table
SELECT id, value, LOG10(value) AS log10 FROM measurements;
Using log10() To compute logarithms to base 10 for the values:
Output: