math.sqrt

Shared

Returns the square root of a number


Syntax

local result = math.sqrt(
    value
)

Parameters

TypeNameDescription
numbervalueNon-negative number to take the square root of

Returns

TypeNameDescription
numberresultSquare root of value

Examples

Square root of a perfect square
local result = math.sqrt(9)

engine.print("info", result) -- 3.0
Square root of a non-perfect square
local result = math.sqrt(2)

engine.print("info", result) -- 1.4142135623731
Square root of zero
local result = math.sqrt(0)

engine.print("info", result) -- 0.0
Square root of a large number
local result = math.sqrt(1000000)

engine.print("info", result) -- 1000.0
Calculate 2D distance between two points
local dx = 3
local dy = 4
local distance = math.sqrt(dx * dx + dy * dy)

engine.print("info", distance) -- 5.0
Normalize a 2D vector
local vx, vy = 6, 8
local length = math.sqrt(vx * vx + vy * vy)
local nx, ny = vx / length, vy / length

engine.print("info", nx, ny) -- 0.6  0.8
Check if a point is within a radius
local px, py = 3, 3
local radius = 5
local dist = math.sqrt(px * px + py * py)

if dist <= radius then
    engine.print("info", "Inside radius")
end

On this page