The math.tan() function returns the tangent of a specified angle, which is assumed to be in radians.

Syntax

math.tan(x)

Parameters

  • x: A number representing an angle in radians.

Return Value

Returns the tangent of x. If x is Infinity, -Infinity, or NaN, it returns NaN.

Description

The math.tan function computes the tangent of the given angle x in radians. Since tan is a static method of the math library, it is always used in the form math.tan() and not as a method of a math object.

Examples

Basic usage of math.tan()

print(math.tan(0))              -- Output: 0
print(math.tan(math.pi / 4))    -- Output: 0.9999999999999999 (Floating point error)
print(math.tan(math.pi / 2))     -- Output: NaN (Approaches infinity)
print(math.tan(math.pi / 3))     -- Output: 1.7320508075688774
print(math.tan(-math.pi / 2))    -- Output: NaN (Approaches negative infinity)
print(math.tan(math.pi))          -- Output: 0

Using math.tan() with degree values

To work with degree values, you can create a function to convert degrees to radians before calculating the tangent:

function getTanDeg(deg)
    local rad = (deg * math.pi) / 180
    return math.tan(rad)
end

print(getTanDeg(45))  -- Output: 0.9999999999999999
print(getTanDeg(90))  -- Output: NaN (Approaches infinity)

See also

  • math.sin(): Returns the sine of an angle.
  • math.cos(): Returns the cosine of an angle.
  • math.asin(): Returns the arcsine of a number.
  • math.acos(): Returns the arccosine of a number.
  • math.atan(): Returns the arctangent of a number.
  • math.atan2(): Returns the arctangent of the quotient of its arguments.

Was this page helpful?

Table of Contents