util.math.huge

Shared

A constant representing positive infinity


Constant

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

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

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

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

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

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

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

On this page