math.abs

Shared

Returns the absolute value of a number


Syntax

local result = math.abs(
    value
)

Parameters

TypeNameDescription
numbervalueNumber to get the absolute value of

Returns

TypeNameDescription
numberresultAbsolute (non-negative) value of the input

Examples

Absolute value of a negative number
local result = math.abs(-5)

engine.print("info", result) -- 5
Absolute value of a positive number
local result = math.abs(42)

engine.print("info", result) -- 42
Absolute value of zero
local result = math.abs(0)

engine.print("info", result) -- 0
Absolute value of a negative float
local result = math.abs(-3.14)

engine.print("info", result) -- 3.14
Calculate distance between two numbers
local a = 10
local b = 35
local distance = math.abs(a - b)

engine.print("info", distance) -- 25
Clamp a delta to avoid overshoot
local delta = -0.75
local speed = math.abs(delta) * 10

engine.print("info", speed) -- 7.5
Check if a value is within tolerance
local value = -0.0001
local tolerance = 0.001

if math.abs(value) < tolerance then
    engine.print("info", "Within tolerance")
end

On this page