math.cos

Shared

Returns the cosine of an angle given in radians


Syntax

local result = math.cos(
    angle
)

Parameters

TypeNameDescription
numberangleAngle in radians

Returns

TypeNameDescription
numberresultCosine of angle, in the range [-1, 1]

Examples

Cosine of 0 is 1
local result = math.cos(0)

engine.print("info", result) -- 1.0
Cosine of pi is -1
local result = math.cos(math.pi)

engine.print("info", result) -- -1.0
Cosine of a 60 degree angle
local result = math.cos(math.rad(60))

engine.print("info", result) -- 0.5
Cosine of a 90 degree angle
local result = math.cos(math.rad(90))

engine.print("info", result) -- 0.0
Generate a point on a unit circle
local angle = math.rad(45)
local pos_x = math.cos(angle)
local pos_y = math.sin(angle)

engine.print("info", pos_x, pos_y) -- 0.70711  0.70711
Oscillate a value back and forth
local elapsed = 1.0
local value = math.cos(elapsed * math.pi)

engine.print("info", value) -- -1.0
Compute cosine for each cardinal angle
for _, deg in ipairs({0, 90, 180, 270, 360}) do
    engine.print("info", deg, math.cos(math.rad(deg)))
end

On this page