The TAN()
function in SQLite calculates the tangent of an angle provided in radians. The tangent is the ratio of sine to cosine (or the ratio of the opposite side to the adjacent side in a right triangle).
Syntax
TAN(angle_in_radians)
Where angle_in_radians
is a numeric expression representing an angle in radians. If your angle is in degrees, you’ll need to convert it using the RADIANS()
function first.
Example 1
Here’s a simple example to demonstrate:
SELECT TAN(3);
Output:
+--------------------+
| TAN(3) |
+--------------------+
| -0.142546543074278 |
+--------------------+
Example 2
Here’s an example using TAN()
with data from a database:
-- Create a table with common angles in degrees
CREATE TABLE angles (
angle_id INTEGER PRIMARY KEY,
angle_degrees FLOAT
);
-- Insert common angles
INSERT INTO angles (angle_degrees) VALUES
(0),
(30),
(45),
(60),
(90),
(180),
(270);
-- Calculate tangent values
-- Note: We must convert degrees to radians first
SELECT
angle_degrees,
ROUND(TAN(RADIANS(angle_degrees)), 6) as tangent_value
FROM angles
ORDER BY angle_degrees;
Output:
+---------------+----------------------+
| angle_degrees | tangent_value |
+---------------+----------------------+
| 0.0 | 0.0 |
| 30.0 | 0.57735 |
| 45.0 | 1.0 |
| 60.0 | 1.732051 |
| 90.0 | 1.63312393531954e+16 |
| 180.0 | 0.0 |
| 270.0 | 5.44374645106512e+15 |
+---------------+----------------------+
Note that the mathematical tangent of 90 and 180 is actually undefined, but in our example we get an extremely large number instead, due to floating-point arithmetic limitations. For this reason we should be cautious when working with trigonometric functions near their undefined points, as computer implementations may not perfectly match mathematical theory.
Common Use Cases of TAN()
TAN()
can be useful in:
- Trigonometric calculations
- Engineering computations
- Navigation systems
- Construction and architecture
- Game development (calculating angles and slopes)
TAN()
‘s Relationship with SIN()
and COS()
TAN()
works alongside other trigonometric functions in SQLite like SIN()
and COS()
, and the relationship between them is:
TAN(x) = SIN(x) / COS(x).
The following example demonstrates this:
SELECT
TAN(3),
SIN(3) / COS(3);
Output:
+--------------------+--------------------+
| TAN(3) | SIN(3) / COS(3) |
+--------------------+--------------------+
| -0.142546543074278 | -0.142546543074278 |
+--------------------+--------------------+