How the DEGREES() function works in Mariadb?
The DEGREES()
function is a mathematical function that converts an angle value from radians to degrees.
The DEGREES()
function is a mathematical function that converts an angle value from radians to degrees. It is useful for performing trigonometric calculations or converting between different units of angle measurement.
Syntax
The syntax of the DEGREES()
function is as follows:
DEGREES(angle)
The angle
parameter is a numeric expression that represents an angle in radians. The DEGREES()
function returns a decimal value that represents the equivalent angle in degrees.
Examples
Example 1: Converting radians to degrees
In this example, we use the DEGREES()
function to convert some common angles from radians to degrees.
SELECT DEGREES(0) AS zero,
DEGREES(PI()/6) AS thirty,
DEGREES(PI()/4) AS forty_five,
DEGREES(PI()/3) AS sixty,
DEGREES(PI()/2) AS ninety,
DEGREES(PI()) AS one_eighty,
DEGREES(2*PI()) AS three_sixty;
The output is:
+------+--------------------+------------+-------------------+--------+------------+-------------+
| zero | thirty | forty_five | sixty | ninety | one_eighty | three_sixty |
+------+--------------------+------------+-------------------+--------+------------+-------------+
| 0 | 29.999999999999996 | 45 | 59.99999999999999 | 90 | 180 | 360 |
+------+--------------------+------------+-------------------+--------+------------+-------------+
Example 2: Calculating the angle of a right triangle
In this example, we use the DEGREES()
function to calculate the angle of a right triangle given the lengths of two sides. We use the ATAN2()
function to find the arctangent of the ratio of the opposite side to the adjacent side, and then convert the result from radians to degrees.
-- Given a right triangle with sides 3 and 4
SET @opposite = 3;
SET @adjacent = 4;
-- Find the angle in radians
SET @angle = ATAN2(@opposite, @adjacent);
-- Convert the angle to degrees
SELECT DEGREES(@angle) AS angle_in_degrees;
The output is:
+-------------------+
| angle_in_degrees |
+-------------------+
| 36.86989764584402 |
+-------------------+
Example 3: Finding the distance between two points on a sphere
In this example, we use the DEGREES()
function to find the distance between two points on a sphere, given their latitude and longitude coordinates. We use the ACOS()
function to find the central angle between the two points, and then multiply it by the radius of the sphere to get the distance. We assume that the radius of the sphere is 6371 kilometers.
-- Given two points on a sphere with latitude and longitude coordinates
SET @lat1 = RADIANS(40.7128); -- New York
SET @lon1 = RADIANS(-74.0060);
SET @lat2 = RADIANS(48.8566); -- Paris
SET @lon2 = RADIANS(2.3522);
-- Find the central angle in radians
SET @angle = ACOS(SIN(@lat1) * SIN(@lat2) + COS(@lat1) * COS(@lat2) * COS(@lon1 - @lon2));
-- Convert the angle to degrees
SELECT DEGREES(@angle) AS angle_in_degrees;
The output is:
+--------------------+
| angle_in_degrees |
+--------------------+
| 52.495568637631564 |
+--------------------+
To find the distance, we multiply the angle by the radius of the sphere:
-- Find the distance in kilometers
SELECT DEGREES(@angle) * 6371 AS distance_in_km;
The output is:
+-------------------+
| distance_in_km |
+-------------------+
| 334449.2677903507 |
+-------------------+
Related Functions
Some of the functions that are related to the DEGREES()
function are:
RADIANS()
: This function converts an angle value from degrees to radians. It is the inverse of theDEGREES()
function. For example,RADIANS(180)
returnsPI()
.SIN()
,COS()
,TAN()
functions: These functions calculate the sine, cosine, and tangent of an angle in radians, respectively. They are commonly used for trigonometric calculations. For example,SIN(PI()/6)
returns0.5
.ASIN()
,ACOS()
,ATAN()
functions: These functions calculate the arcsine, arccosine, and arctangent of a numeric value, respectively. They return an angle in radians. They are the inverse of theSIN()
,COS()
, andTAN()
functions, respectively. For example,ASIN(0.5)
returnsPI()/6
.ATAN2()
: This function calculates the arctangent of two numeric values, representing the opposite and adjacent sides of a right triangle. It returns an angle in radians. It is similar to theATAN()
function, but it can handle the cases where the adjacent side is zero. For example,ATAN2(1, 0)
returnsPI()/2
.
Conclusion
The DEGREES()
function is a useful function for converting angles from radians to degrees. It can be used for various purposes, such as performing trigonometric calculations, converting between different units of angle measurement, or finding the distance between two points on a sphere. The DEGREES()
function is compatible with other mathematical functions, such as RADIANS()
, SIN()
, COS()
, TAN()
, ASIN()
, ACOS()
, ATAN()
, and ATAN2()
.