util.math.sqrt

Shared

Returns the square root of a number


Syntax

local result = util.math.sqrt(
    value
)

Parameters

TypeNameDescription
floatvalueNon-negative number to take the square root of

Returns

TypeNameDescription
floatresultSquare root of value

Examples

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

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

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

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

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

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

core.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 = util.math.sqrt(px*px + py*py)

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

On this page