math.huge

Shared

A constant representing positive infinity


Constant

math.huge -- inf

Represents positive infinity. Useful as a sentinel initial value for minimum-finding loops, or for comparisons involving unbounded ranges.


Examples

Print the value of huge
engine.print("info", math.huge) -- inf
huge is greater than any number
engine.print("info", math.huge > 1e308) -- true
Negative huge is less than any number
engine.print("info", -math.huge < -1e308) -- true
Use as initial value in a minimum search
local values = {42, 7, 99, 3, 56}
local smallest = math.huge

for _, v in ipairs(values) do
    if v < smallest then
        smallest = v
    end
end

engine.print("info", smallest) -- 3
Use as initial value in a maximum search
local values = {42, 7, 99, 3, 56}
local largest = -math.huge

for _, v in ipairs(values) do
    if v > largest then
        largest = v
    end
end

engine.print("info", largest) -- 99
Check if a number is finite
local function is_finite(value)
    return value > -math.huge and value < math.huge
end

engine.print("info", is_finite(42))        -- true
engine.print("info", is_finite(math.huge)) -- false
Division by zero produces huge
local result = 1 / 0

engine.print("info", result == math.huge) -- true

On this page