A Quick Look at the DEGREES() Function in SQLite

In SQLite, the degrees() function is used to convert an angle from radians to degrees. In trigonometry, angles are commonly measured in either radians or degrees, and this function helps to easily switch from one unit to the other.

Syntax

The syntax goes like this:

degrees(X)

Where X is the angle in radians that you want to convert to degrees.

The formula used by degrees() is: Degrees = X x 180 / π

Where:

  • X is the angle in radians.
  • π (pi) is approximately 3.14159.

This formula converts an angle from radians to degrees by multiplying the radian value by 180/π.

Example

Suppose you want to convert an angle of π radians (which is equivalent to 180 degrees) into degrees.

SELECT degrees(3.141592653589793);

Result:

180.0

Another Example

Here are some more conversions:

SELECT degrees(0) AS Degrees_0,
       degrees(0.5236) AS Degrees_pi_over_6,
       degrees(0.7854) AS Degrees_pi_over_4,
       degrees(1.0472) AS Degrees_pi_over_3,
       degrees(1.5708) AS Degrees_pi_over_2,
       degrees(3.1416) AS Degrees_pi,
       degrees(4.7124) AS Degrees_3pi_over_2,
       degrees(6.2832) AS Degrees_2pi;

Result (using Line output):

         Degrees_0 = 0.0
Degrees_pi_over_6 = 30.0000701530499
Degrees_pi_over_4 = 45.0001052295749
Degrees_pi_over_3 = 60.0001403060998
Degrees_pi_over_2 = 90.0002104591497
Degrees_pi = 180.000420918299
Degrees_3pi_over_2 = 270.000631377449
Degrees_2pi = 360.000841836599

Rounding the Results

The above results include a fractional component that shouldn’t really be there. For example, 30.0000701530499 should be just 30.

When using floating-point values, you might see small rounding errors like the above ones due to the limited precision of floating-point arithmetic. This is common when working with approximate values for π and can result in very small fractional components that are artifacts of the calculation.

To remove these unnecessary fractional components in SQLite, you can use the round() function to round the result to a specified number of decimal places.

Example with ROUND()

To round the values to a whole number (0 decimal places) or a few decimal places, we can modify the query like this:

SELECT round(degrees(0), 0) AS Degrees_0,
       round(degrees(0.5236), 0) AS Degrees_pi_over_6,
       round(degrees(0.7854), 0) AS Degrees_pi_over_4,
       round(degrees(1.0472), 0) AS Degrees_pi_over_3,
       round(degrees(1.5708), 0) AS Degrees_pi_over_2,
       round(degrees(3.1416), 0) AS Degrees_pi,
       round(degrees(4.7124), 0) AS Degrees_3pi_over_2,
       round(degrees(6.2832), 0) AS Degrees_2pi;

Result:

         Degrees_0 = 0.0
Degrees_pi_over_6 = 30.0
Degrees_pi_over_4 = 45.0
Degrees_pi_over_3 = 60.0
Degrees_pi_over_2 = 90.0
Degrees_pi = 180.0
Degrees_3pi_over_2 = 270.0
Degrees_2pi = 360.0

This query rounds each result to the nearest whole number, removing the fractional part.

The degrees() function is helpful when working with trigonometric functions, especially when you need to convert results back to a more commonly used angle measure like degrees.