Skip to main content
Documents
Basic SyntaxMath FunctionsDate FunctionsJSON FunctionsDatabasesTables & Schema ManagementString FunctionsTriggersIndexes

PostgreSQL TAN

The `TAN` function in PostgreSQL is a mathematical function that returns the tangent of a specified angle, which is provided in radians. It is commonly used in trigonometric calculations to determine the ratio of the opposite side to the adjacent side of a right-angled triangle.

Usage

The `TAN` function is used when you need to calculate the tangent of an angle, particularly in mathematical, engineering, and scientific computations. It takes a single argument, the angle in radians, and returns the tangent value.

sql
TAN(numeric)

In this syntax, `numeric` is the angle in radians for which you want to calculate the tangent.

Examples

1. Basic Tangent Calculation

sql
SELECT TAN(0.7853981633974483);

This example calculates the tangent of 0.7853981633974483 radians (approximately 45 degrees), which results in approximately 1.

2. Tangent of Pi/4

sql
SELECT TAN(pi() / 4);

Here, the tangent of π/4 radians (or 45 degrees) is calculated. The expected result is 1, as the tangent of 45 degrees is 1.

3. Using TAN with Table Data

sql
CREATE TABLE angles (angle_in_radians NUMERIC);
INSERT INTO angles VALUES (0.5235987755982988), (1.0471975511965976);

SELECT angle_in_radians, TAN(angle_in_radians) AS tangent_value
FROM angles;

In this example, a table `angles` is created with angle values, and the tangent for each angle is calculated using the `TAN` function.

Tips and Best Practices

  • Ensure angles are in radians. The `TAN` function requires angles to be in radians, not degrees. Use conversion if necessary. For example, to convert degrees to radians, use the formula `radians = degrees * (pi()/180)`.
  • Consider floating-point precision. Be cautious with very large or very small input values, as floating-point precision might affect the results.
  • Understand domain and range. Be aware of the function's domain and range to avoid unexpected results, especially near discontinuities such as π/2 + kπ, where the function is undefined.
  • Use with trigonometric identities. Leverage mathematical identities to simplify or optimize complex expressions involving tangents.
  • Combine with other trigonometric functions. Use `TAN` in conjunction with other trigonometric functions like `SIN` and `COS` for comprehensive calculations.