math.ult()

Baseline

Available in lua 5.3 and later.

The math.ult() function returns a boolean value indicating whether one integer is less than another when treated as unsigned integers.

Syntax

math.ult(m, n)

Parameters

  • m: An integer to compare.
  • n: Another integer to compare against.

Return Value

  • Returns true if m is less than n when both are treated as unsigned integers.
  • Returns false otherwise.

Description

The math.ult function compares two integers, m and n, by treating them as unsigned values. This is particularly useful for comparing negative integers or large positive integers where overflow might occur if treated as signed integers.

Examples

Basic usage of math.ult()

print(math.ult(5, 10))        -- Output: true (5 is less than 10)
print(math.ult(10, 5))        -- Output: false (10 is not less than 5)
print(math.ult(-1, 0))        -- Output: false (unsigned comparison; -1 is treated as a large unsigned number)
print(math.ult(0, 1))         -- Output: true (0 is less than 1)
print(math.ult(255, 256))     -- Output: true (255 is less than 256)
print(math.ult(256, 255))     -- Output: false (256 is not less than 255)
print(math.ult(math.huge, 1)) -- Output: false (infinity is not less than 1)

See also

  • math.odd()
  • math.even()

Was this page helpful?

Table of Contents