The math.atan() function in lua returns the arc tangent of y/x (in radians), using the signs of both arguments to determine the quadrant of the result. It also correctly handles the case when x is zero.

The default value for x is 1, so calling math.atan(y) returns the arc tangent of y.


Syntax

math.atan(y [, x])

Parameters

  • y
    A number representing the numerator in the tangent calculation.

  • x (optional)
    A number representing the denominator in the tangent calculation. If not provided, the default value is 1.

Return value

The inverse tangent (angle in radians) of y/x, which lies in the interval [-π/2, π/2]. If x is zero, the function returns π/2 if y is positive, and -π/2 if y is negative.


Description

Because math.atan() is a static function, it is always used as math.atan(), rather than as a method of a lua object. This function can be used to compute angles in the Cartesian coordinate system.


Examples

Using math.atan()

print(math.atan(1))        -- 0.78539816339745 (π/4)
print(math.atan(0))        -- 0
print(math.atan(-1))       -- -0.78539816339745 (-π/4)

-- Using two parameters
print(math.atan(1, 1))     -- 0.78539816339745 (π/4)
print(math.atan(1, 0))     -- 1.5707963267949 (π/2)
print(math.atan(-1, 0))    -- -1.5707963267949 (-π/2)

See also

  • math.acos()
  • math.asin()
  • math.atan2()
  • math.cos()
  • math.sin()
  • math.tan()

Was this page helpful?

Table of Contents