How the TAN() function works in Mariadb?
The TAN()
function in MariaDB is a trigonometric function that calculates the tangent of a given angle. It takes an angle value as input and returns the ratio of the opposite side to the adjacent side of a right-angled triangle with that angle.
Syntax
The syntax for the MariaDB TAN()
function is as follows:
TAN(X)
X
: A numeric value representing the angle in radians for which the tangent is to be calculated.
The TAN()
function returns a DOUBLE
value, which is the tangent of the given angle X
.
Examples
Example 1: Calculating the tangent of an angle
This example demonstrates how to use the TAN()
function to calculate the tangent of a given angle.
SELECT TAN(PI()/4);
The following is the output:
+--------------------+
| TAN(PI()/4) |
+--------------------+
| 0.9999999999999999 |
+--------------------+
In this example, the TAN()
function calculates the tangent of PI()/4
radians, which is approximately 0.785 radians or 45 degrees. The result is approximately 1, which is the expected tangent value for a 45-degree angle.
Example 2: Using TAN() in a calculation
This example shows how to use the TAN()
function in a more complex calculation.
SELECT TAN(RADIANS(60)) * 2;
The following is the output:
+----------------------+
| TAN(RADIANS(60)) * 2 |
+----------------------+
| 3.4641016151377535 |
+----------------------+
In this example, the RADIANS()
function is first used to convert the angle 60 degrees to radians (approximately 1.0472 radians). Then, the TAN()
function calculates the tangent of this angle, which is approximately 1.7320508075688772. Finally, the result is multiplied by 2 to get the final output of 3.4641016151377548.
Example 3: Using TAN() with NULL values
This example demonstrates how the TAN()
function handles NULL values.
SELECT TAN(NULL);
The following is the output:
+-----------+
| TAN(NULL) |
+-----------+
| NULL |
+-----------+
In this example, when passing a NULL value to the TAN()
function, it returns NULL as the result, indicating that the tangent cannot be calculated for a NULL input.
Example 4: Using TAN() in a query
This example shows how to use the TAN()
function in a query involving a table.
DROP TABLE IF EXISTS triangles;
CREATE TABLE triangles (
id INT PRIMARY KEY,
angle DECIMAL(5, 2),
opposite DECIMAL(5, 2),
adjacent DECIMAL(5, 2)
);
INSERT INTO triangles (id, angle, opposite, adjacent) VALUES
(1, 30.00, 0.50, 1.00),
(2, 45.00, 1.00, 1.00),
(3, 60.00, 1.73, 1.00);
SELECT id, angle, TAN(RADIANS(angle)) AS calculated_tan, opposite / adjacent AS actual_tan
FROM triangles;
The following is the output:
+----+-------+--------------------+------------+
| id | angle | calculated_tan | actual_tan |
+----+-------+--------------------+------------+
| 1 | 30.00 | 0.5773502691896256 | 0.500000 |
| 2 | 45.00 | 0.9999999999999999 | 1.000000 |
| 3 | 60.00 | 1.7320508075688767 | 1.730000 |
+----+-------+--------------------+------------+
In this example, a table triangles
is created, and data is inserted into it. The query then retrieves the id
, angle
, and calculates the tangent of the angle
using the TAN()
function with the RADIANS()
function to convert the angle to radians. It also calculates the actual tangent by dividing the opposite
side by the adjacent
side. The calculated and actual tangents are compared in the output.
Example 5: Using TAN() in a calculation with degrees
This example demonstrates how to use the TAN()
function with angles in degrees instead of radians.
SELECT TAN(RADIANS(45)) AS tan_radians, TAN(45 * PI() / 180) AS tan_degrees;
The following is the output:
+--------------------+--------------------+
| tan_radians | tan_degrees |
+--------------------+--------------------+
| 0.9999999999999999 | 0.9999999999999999 |
+--------------------+--------------------+
In this example, the TAN()
function is called twice: once with the angle in radians (using the RADIANS()
function to convert 45 degrees to radians), and once with the angle in degrees (by converting 45 degrees to radians using the PI()
function and a multiplication factor of PI() / 180
). Both expressions return the same result, which is the tangent of a 45-degree angle.
Related Functions
The following are a few functions related to the MariaDB TAN()
function:
- The
SIN()
function calculates the sine of a given angle. - The
COS()
function calculates the cosine of a given angle. - The
COT()
function calculates the cotangent of a given angle. - The
ATAN()
function calculates the arctangent (inverse tangent) of a given value. - The
RADIANS()
function converts an angle from degrees to radians. - The
DEGREES()
function converts an angle from radians to degrees.
Conclusion
The TAN()
function in MariaDB is a valuable tool for performing trigonometric calculations involving tangents. It takes an angle as input and returns the tangent of that angle. By understanding the syntax and usage of this function, along with related trigonometric functions like SIN()
and COS()
, developers can perform complex calculations and analyze data involving angles and triangles within their MariaDB applications.