Understanding Postgres Log Functions - ln(), log(), log10()

To perform mathematical computations, PostgreSQL offers many built-in functions. Log functions are among the most significant and commonly used functions. The log functions return the logarithm of the specified numbers. There are different variants of log functions that are differentiated from each other on the basis of the base they have. This article will provide you with the three basic log functions i.e. ln(), log(), and log10(). Let’s discover these functions together.

Understanding Postgres Log Functions - log(), log10(), ln()

The log() functions are used to compute the logarithm of the specified number. The difference between the log(), ln(), and log10() function is basically their base. Let’s discuss these functions one by one.

log() Function in PostgreSQL

The log() function is used to compute the logarithm of the base specified in the given number. The base and the number both are specified as parameters in the log() function. The basic syntax of the general log function is:

log(base, num)

The function takes the base and the number as arguments are returns the log of the base of that number. The log function returns NULL if the parameter is NULL.

Let’s consider some examples of the log() function.

Example: Understanding the Log() Function

Consider the following example to get a clear understanding of the log() function:

SELECT
  log(2, 6) AS "log_1",
  log(10, 9) AS "log_2";

The above queries will return the logarithms of specified numbers like this:

img

This is how the log() function works.

Log10() Function in PostgreSQL

We can also write the log(10, num) as log10(num). For the log10() function, the base is specified as 10. It means that the log10() function gives the logarithm of base 10 of a particular number.

Let’s have a glance at what a function will return, in case of certain input:

● The log function returns NULL if the argument is NULL.

● If the number specified is “zero”, the function will return an error that “ERROR: cannot take the logarithm of zero”.

● If the specified number is negative, it will again throw an error i.e. “ERROR: cannot take logarithm of a negative number”.

Let’s move towards some examples of the log10() function.

Example: Understanding the Log() Function

Consider the following queries to understand how the function works:

SELECT
  log10( 6) AS "log10_1",
  log10( 9) AS "log10_2";

The above queries return the logarithm of base 10 for the specified numbers. The output looks like this:

img

Now if we write the query to find the log10 of zero, it will return an error that looks like this:

SELECT log10(0);

The query will return an error as claimed above.

img

Now we will try finding the log10 of a negative number. The query will look like this:

SELECT log10(-8);

This also returns an error that is illustrated below:

img

So this is how log10() works.

ln() Function in PostgreSQL

The ln() function returns the natural log of a particular number. The basic syntax for the ln() function is given as:

ln(num)

The ln()function only takes in the specified number and returns the natural log of that number.

Let’s see the implementation of this function using an example.

Example: Understanding the ln() function

Consider the following queries to find the natural number of the specified numbers:

SELECT
  ln(6) AS "ln_1",
  ln(9) AS "ln_2";

The output of these queries is:

img

The ln() function also throws an error if we try to calculate the natural log of Zero or a negative number. Like this:

SELECT ln(0) AS "ln_1";
 SELECT ln(-9) AS "ln_2";

The output shows the errors as:

img

This is how the ln() function returns the natural log of the specified numbers except zero and negative numbers.

Conclusion

The log functions are used to find the logarithm of a particular number(provided in the function as a parameter). The log() function takes in two parameters; the base and the number whose logarithm is to be returned. The log10() and ln() functions are the variants of the log() function based on the base. The ln() function has base “e” while the log10() function has base “10” and accordingly they compute and return the logarithm of the number specified. In this post, we tried to understand the Basic Postgres log functions with the help of examples.