PostgreSQL degrees() Function

There is a huge list of mathematical functions offered by PostgreSQL. These mathematical functions assist the user in performing the mathematical computations. The degrees() function is used to convert the angle in radian value into degrees. The SI unit for the measurement of angles is radian. In this post, we’ll learn how the degrees() function works.

PostgreSQL degrees() Function

The degrees() function takes in a double precision as an angle in radians and also returns a double precision but as degrees. The degrees() function simply converts the angle in radians into degrees.

The syntax of this function looks like this:

degrees(angle_in_radian_dp)

The argument provided to the degrees() function is the angle in radians and the data type will be double precision. The function returns the angle in degrees.

We will go through some examples to get the concepts more clearly.

Example 1: Understanding degrees() Function

Consider the following query to understand the degrees() function:

SELECT
  degrees(0) AS "degrees:0",
  degrees(1.0) AS "degrees:1.0",
  degrees(9.45) AS "degrees:9.45",
  degrees(-11.56) AS "degrees:-11.56",
  degrees(17.79*2) AS "expression degrees";

The function will convert all the radian values, provided to it, into the degrees like this:

img


We can see that the degrees() function has converted all the radian values into degrees. Other than that we can notice the following things in the above case:

● The function also supports the negative values (-11.56).

● We can also specify any expression (17.79*2) in the degrees() function. The function will first solve the expression and then convert the value into degrees.

This is the basic working of the degrees() function.

Example 2: Using degrees() Function to Get Full Circle

The full circle in radians is equal to “6.283185307179586” radians. Now if we pass the stated radian value to the degrees() function, it will return the full circle in degrees.

SELECT
  degrees(6.283185307179586) AS "Full Circle";

The output for this query is:

img


We can observe that the radian value 6.283185307179586 has given the full circle i.e. 360 degrees.

Example 3: Using degrees() Function With Other Functions

We can also use the degrees() function with other functions. The PI() function returns the mathematical constant value of pi. So let’s use it in a query.

SELECT
degrees(PI()) AS "Half Circle";

The PI() function will provide the mathematical constant value of π i.e. 3.141592653589793. The degrees() function will then convert the radian value of π into degrees like this.

img


We can see that the degrees() function has returned 180 degrees which is the half circle. We can also get the full circle by passing the 2*PI() argument in the degrees() function like this:

SELECT
degrees(PI()*2) AS "Full Circle";

This will give the full circle as is equal to the full circle.

img


Using Radian() Function

There are many other functions with which we can use the degrees() function for example radian() is another function that converts the degrees into radians. Let’s observe what happens if we use the radians() function in the degrees function. Consider the below query for this:

SELECT
  degrees(radians(4*90)) AS "Full Circle";

● The query will first solve the expression i.e. 4*90 that gives 360.

● The 360 is in degrees that is given as input to the radians() function.

● The radians() function will convert the 360 degrees into radians.

● That radian value is then provided to the degrees() function to get the final output.

If we carefully understand the query, we will get to know that the degrees() and radians() functions counterbalance each other's effect. So we will get the 360 degrees as output. To verify this let’s see what the query has returned.

img


The query returned the same output as we expected.

Example 4: Using degrees() Function on Table’s Data

We can implement the degrees() function on the table data. For this particular function, consider the table named “circle”.

img


We will write the following query if we wish to convert every angle given in the column “angle_in_rad” into degrees:

SELECT angle_in_rad, degrees(angle_in_rad) AS "angle in degrees"
 FROM circle;

On executing this query, we will get the degree conversion of each angle_in_rad entry like this:

img


This is how we can implement the degrees() function on table data to get the radian values in degrees.

Conclusion

The mathematical degrees() function in PostgreSQL converts the radian values of angles into degrees. The function takes in the angle in radians with double precision data type, and it converts and returns the angle value in degrees also having double precision data type. We have implemented different use cases for the degrees() function in detail, to understand the concept clearly.